码迷,mamicode.com
首页 > Web开发 > 详细

jsp 页面打开文件

时间:2014-06-18 09:58:35      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:cWeb   style   class   blog   code   java   

 1 package tcweb.handler.report;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 
 6 import javax.servlet.ServletContext;
 7 import javax.servlet.ServletOutputStream;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 import tcweb.Constants;
12 import tcweb.EventHandler;
13 import tcweb.Option;
14 import tcweb.PathMap;
15 import tcweb.WCException;
16 
17 public class OpenReportHandler implements EventHandler {
18 
19     public String process(ServletContext sc, HttpServletRequest request, HttpServletResponse response) throws WCException {
20 
21         String filepath = request.getParameter("filepath");
22         try {
23             filepath = new String(filepath.getBytes("ISO-8859-1"), "utf-8");
24             int fileTypePos = filepath.lastIndexOf(".");
25             String fileType = filepath.substring(fileTypePos+1, filepath.length());
26             String contentType ="application/octet-stream";
27             if(fileType.equalsIgnoreCase("txt")){
28                 //contentType = "text/xml";
29                 contentType = Constants.PAGE_CONTENT_TYPE_TEXT;
30                 //contentType = "text/html";
31             }else if(fileType.equalsIgnoreCase("xml")){
32                 contentType = Constants.PAGE_CONTENT_TYPE_XML;
33             }else if(fileType.equalsIgnoreCase("pdf")){
34                 contentType = "application/pdf";
35             }
36             else if(fileType.equalsIgnoreCase("xls")){
37                 contentType = "application/vnd.ms-excel";
38             }
39             else if(fileType.equalsIgnoreCase("xlsx")){
40                 contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";                
41             }
42 //            else if(fileType.equalsIgnoreCase("accdb") || fileType.equalsIgnoreCase("mdb")){
43 //                contentType = "application/msaccess";                
44 //            }else if(fileType.equalsIgnoreCase("zip")){
45 //                contentType = "application/zip";                
46 //            }
47             
48             filepath = PathMap.path2SystemSeparator(filepath);
49             
50             String fileP = PathMap.trimEndSeparator(Option.getStoragePath()) + filepath;
51             File resultFile = new File(fileP);
52             if (resultFile.exists()==true){
53                 filepath = fileP;
54             }else{
55                 filepath = PathMap.trimEndSeparator(PathMap.getRootTemporaryFolder())+filepath;
56             }
57             
58             FileInputStream is=new FileInputStream(filepath);
59               // response.reset(); 
60                //response.setHeader("Content-Disposition","inline; filename=Here is the Amazing PDF");
61                response.setCharacterEncoding("UTF-8");
62                
63                response.setContentType(contentType);  
64                //response.setHeader("Content-disposition","attachment;filename="+new String("aaa.pdf".getBytes("gb2312"),"iso8859-1"));
65                
66                //response.setHeader("Content-Disposition","attachment;filename="+filename); 
67                
68                ServletOutputStream sos = response.getOutputStream();      
69                byte[] buffer = new byte[1024];
70                int len=0;
71                while((len=is.read(buffer))>0){
72                  sos.write(buffer,0,len); 
73                }  
74                  is.close();
75                  sos.flush();      
76                  sos.close(); 
77 
78         } catch (Exception e1) {
79             e1.printStackTrace();
80         }
81         
82         return null;
83     }
84 
85 }

 

jsp 页面打开文件,布布扣,bubuko.com

jsp 页面打开文件

标签:cWeb   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/lightning95/p/3793142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!