有个空间

有个标题

Struts文件下载

  1 public void filedown(HttpServletRequest request, HttpServletResponse response,
             Member objMember)
throws Exception{
  2         FileInputStream in = null;
  3         ServletOutputStream os = null;
  4         BufferedOutputStream bos = null;
  5         String fromPageId = (String) request.getAttribute("frompageid");
  6         if(fromPageId == null) fromPageId = request.getParameter("frompageid");
  7         if(fromPageId != null) {
  8             AccessLogWriter.getInstance().writeLockLogFile(
  9                 objMember.getMemberID(), request.getSession().getId(),0,"90",fromPageId,"");
 10         }
 11         
 12         String sfilename = "";
 13         String stargetFileName = "";
 14         String submit = request.getParameter("download");                
 15         String projectid = (String) request.getAttribute("projectid");                
 16         String fileManagerId = (String) request.getAttribute("fileManagerId");
 17         String fileid = (String) request.getAttribute("fileid");
 18         
 19         if (submit != null) {
 20             sfilename = request.getParameter("downloadFileName");
 21             stargetFileName = request.getParameter("targetFileName");
 22         }                 
 23         
 24         if (sfilename==null||sfilename.equals("")) {
 25             //点击URL的情况
 26             sfilename = (String) request.getAttribute("downloadFileName");
 27             stargetFileName = (String) request.getAttribute("targetFileName");
 28         }
 29         if(projectid == null){
 30             projectid = request.getParameter("projectid");
 31         }
 32         
 33         if(fileManagerId == null){
 34             fileManagerId = request.getParameter("fileManagerId");
 35         }
 36         if(fileid == null){
 37             fileid = request.getParameter("fileid");
 38         }
 39         //文字转换
 40         String ufilename = new String(sfilename.getBytes("iso-8859-1"), "Shift_JIS");
 41         if (ufilename.indexOf("?"== -1) {
 42             sfilename = ufilename;
 43         }
 44         String utargetFileName = new String(stargetFileName.getBytes("iso-8859-1"), "Shift_JIS");
 45         if (utargetFileName.indexOf("?"== -1) {
 46             stargetFileName = utargetFileName;
 47         }
 48         
 49         String saveFileName = new String(stargetFileName.getBytes("Shift_JIS"), "ISO-8859-1");
 50         
 51         //& ,%26 符号转换
 52         saveFileName =  toIEParamaterString0to127(saveFileName);
 53         
 54         File existFile = new File(sfilename.replace('\\''/'));
 55         if (!existFile.exists()) {
 56             response.setContentType("text/html; charset=SHIFT_JIS");
 57             PrintWriter out = response.getWriter();
 58             out.println(
 59             "<HTML><HEAD></HEAD><BODY>file isn't exist!</BODY></HTML>");
 60             out.close();
 61             return;
 62         }
 63         
 64         response.setHeader("Cache-Control"""); //HTTP 1.1
 65         response.setHeader("Pragma""");
 66         response.setDateHeader("Expires"3600 * 1000);
 67         
 68         response.setContentType("application/octet-stream");
 69         response.setHeader("Content-Disposition""attachment;filename=" + saveFileName);
 70         
 71         File downloadFile = new File(sfilename);
 72         response.setContentLength((int) downloadFile.length());
 73         
 74         in = new FileInputStream(sfilename);
 75         
 76         os = response.getOutputStream();
 77         bos = new BufferedOutputStream(os);
 78         
 79         byte[] abyte0 = new byte[2048];
 80         int j;
 81         int off = 0, len = abyte0.length;
 82         
 83         while ((j = in.read(abyte0, off, len)) != -1) {
 84             bos.write(abyte0, 0, j);
 85         }
 86         in.close();
 87         bos.close();
 88         os.close();
 89         if(fromPageId != null) {
 90             AccessLogWriter.getInstance().writeLockLogFile(
 91                     objMember.getMemberID(), request.getSession().getId(),1,"90",fromPageId,"");
 92         }
 93         
 94         return;            
 95     }
 96     
 97     /**
 98      * 
 99      * @param s
100      * @return
101      */
102     public String toIEParamaterString0to127(String s) {
103         StringBuffer sb = new StringBuffer();
104         for (int i = 0; i < s.length(); i++) {
105             char c = s.charAt(i);
106             if (c >= 0 && c <= 127) {
107                 try{
108                     if(c == 32){
109                         sb.append("%20");
110                     } else {
111                         sb.append(java.net.URLEncoder.encode(s.substring(i, i+1),"Shift_JIS"));
112                     }
113                 }
114                 catch(Exception e){
115                     sb.append(c);
116                 }
117             } else {
118                 if(c >= 160 && c <= 223){
119                     //半角kana 0xA0-0xDF
120                     sb.append(s.substring(i, i+1));
121                 } else {
122                     //日语汉字 1个字节:0x80-0x9F、0xE0-0xFC
123                     if((( c >= 128 && c <= 159|| (c >= 224 && c <= 252)) && (i +2 <= s.length())){
124                         //取两行
125                         sb.append(s.substring(i, i+2));
126                         i++;
127                     } else {
128                         //已经是最后一行
129                         sb.append(s.substring(i, i+1));
130                     }
131                 }
132             }
133         }
134         return sb.toString();
135     }

posted on 2009-12-24 14:14 游雯 阅读(229) 评论(0)  编辑  收藏 所属分类: Java技术


只有注册用户登录后才能发表评论。


网站导航: