web项目附件针对格式不同的文件进行处理
2017-09-01 00:00
399 查看
/** * 管理附件的方法,pdf采用在线打开方式。office直接下载 * 2017.7.26 sp * @param mapping * @param form * @param request * @param response * @return * @throws Exception */ public ActionForward readFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ISfileAttachService iSfileAttachService = (ISfileAttachService) this.getService("sfileAttachService"); HttpSession session = request.getSession(); String path = session.getServletContext().getRealPath(""); //项目路径 String pathUrl = session.getServletContext().getRealPath(Constants.getProperty("uploadPath")); // 上传目录 String file_id = request.getParameter("FileId"); // 根据附件id进行文件查询 String filePath=""; File file = null; if (!C.isNull(file_id) && !"".equals(file_id)) { SfileAttach isfileattach = (SfileAttach) iSfileAttachService.findByPrimaryKey(file_id); if (!C.isNull(isfileattach)) { if (isfileattach.getFileNameO().contains(".pdf") || isfileattach.getFileNameO().contains(".PDF")) { // 附件类型为pdf的直接在线打开 response.setContentType("application/pdf"); // 标识反馈的页面为pdf file = new File(path + "/" + isfileattach.getFilePath()); if (file.exists() && file.isFile()) { filePath = path + "/" + isfileattach.getFilePath(); }else{ filePath = pathUrl + "/notfile.pdf"; // 文件没找到默认打开文件不存在的那个pdf文件 } }else{ // 其他文件采用下载的方式 response.setHeader("Content-Disposition", "attachment; filename="+ new String(isfileattach.getFileNameO().getBytes("GBK"),"ISO8859-1")); file = new File(path + "/" + isfileattach.getFilePath()); if (file.exists() && file.isFile()) { filePath = path + "/" + isfileattach.getFilePath(); // 附件存在 }else{ filePath = ""; //附件不存在 } } } ServletOutputStream sos = response.getOutputStream(); if (!C.isNull(filePath) && !"".equals(filePath)) { // 路径不为空文件存在时再进行输出 FileInputStream in = new FileInputStream(filePath); byte data[] = new byte[1024]; int len = 0; while ((len = in.read(data)) != -1) { sos.write(data, 0, len); } sos.flush(); // 关闭流操作 in.close(); sos.close(); } } return null; }
相关文章推荐
- NPOI导出Excel2007-xlsx格式文件,用于web时需要注意的问题-XSSFWorkbook处理问题
- 从不同架构的角度进行web项目的需求分析和系统设计
- 项目中在Android.mk文件中根据项目名称选择不同的AndroidManifest.xml文件 && 如何使用系统设定的日期显示格式
- C# web项目添加*.ashx文件后报错处理
- iis7针对不同域名配置不同运行权限,所有共用web.config产生的问题均可以修改根配置文件解决
- 【转】matlab针对不同格式文件的读写
- matlab处理txt文本文件---数据格式要有规律性,否则要用编写特定方式进行读取
- Web项目中 Tomcat 与 资源文件的一些处理
- nginx的web目录下处理中文文件和文件夹以及编码格式问题
- Web项目中对配置文件中的密码进行加密(数据库连接密码)
- NPOI导出Excel2007-xlsx格式文件,用于web时需要注意的问题-XSSFWorkbook处理问题
- Spring 管理下的web项目,对Properties文件重要参数的加密解密处理
- Spring 管理下的web项目,对Properties文件重要参数的加密解密处理(续)
- 三层结构中解决同一解决方案不同项目中访问Web.config文件的问题
- 第17周项目6-文件读取成绩进行处理
- C# winform端 通过HttpWebRequest进行post和get请求,数据格式为json,后台java端接收,其中有关传输特殊字符(\t,\r,',\n,n)等处理
- python处理不同编码格式的文件
- web项目中过滤器不对某些指定文件进行过滤
- 节点文件将两个不同格式的XML文件,进行节点对照,并生成一个用于对照功能的XML
- 基于Proguard软件基础上,针对Web项目xml配置文件修改的java项目。