class="java" name="code">public void downloadAttachment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws TSDBException, IOException { // 附件Id Long fileId = new Long(request.getParameter("id")); // 根据id取得相应的附件 PicAttachment picAttachment = getAttachment(fileId); String realName = picAttachment.getRealFilename(); byte[] downloadFile = picAttachment.getUploadFile(); response.reset(); // 设置为下载application/x-download response.setContentType("application/x-download"); /* * 这个属性设置的是下载工具下载文件时显示的文件名,要想正确的显示中文文件名,我们需要对fileName再次编码 * 否则中文名文件将出现乱码,或无法下载的情况 */ realName = URLEncoder.encode(realName, "utf-8"); // 下载文件时,显示的文件名 response.addHeader("Content-Disposition", "attachment;filename=" + realName); OutputStream output = response.getOutputStream(); output.write(downloadFile); output.flush(); output.close(); }
?