ExtJS中使用jspSmartUpload实现文件下载

 

项目需求:使用Ext的GridPanel实现文件下载功能!如 

首先,在gridpanel中加入链接style的button。在gridpanel的ColumnModel中

Js代码 
  1. renderer:function(){return "<p><button type='submit' class='link' οnclick='downloadfile("+v+")'><span>下载</span></button></p>"}  


加入button style。

Html代码 
  1. button.link {  
  2.             font-family: "Verdana" sans-serif;  
  3.             font-size: 1em;  
  4.             text-align: left;  
  5.             color: blue;  
  6.             background: none;  
  7.             margin: 0;  
  8.             padding: 0;  
  9.             border: none;  
  10.             cursor: pointer;  
  11.         }  


其下载任务,都在downloadfile(v)这个方法中实现,代码

Js代码 
  1. downloadfile = function(v){  
  2.                 Ext.Ajax.request({  
  3.                     url:"getFilePath_fileIO",  
  4.                     params:{"fileId":v},  
  5.                     success:function(response){  
  6.                         var fileJson = Ext.decode(response.responseText);  
  7.   
  8.                         var filepath = encodeURIComponent(fileJson.filepath);  
  9.                         var filename = encodeURIComponent(fileJson.filename);  
  10.                         window.open("downloadfile.jsp?filepath="+filepath+"&filename="+filename,"_self");  
  11.                     },  
  12.                     failure:function(response){  
  13.                       
  14.                     }  
  15.                 });  
  16.             }  


在downloadfile.jsp页面中,使用jspSmartUpload提供的API实现下载。

Java代码 
  1.     String filepath = request.getParameter("filepath");  
  2.     String filename = request.getParameter("filename");  
  3.     filepath = filepath.replace("/","//");  
  4.       
  5.     SmartUpload su = new SmartUpload();  
  6.     //初始化  
  7.     su.init(config);  
  8.     su.service(request,response);  
  9.     //禁止浏览器自动打开文件  
  10.     su.setContentDisposition(null);  
  11.       
  12.     //下载文件  
  13.     String path = new String(filepath.getBytes("ISO8859-1"),"UTF-8");  
  14.     String name = new String(filename.getBytes("ISO8859-1"),"UTF-8");  
  15. //  System.out.print(name);   
  16.     su.downloadFile(path,null,name);  
  17.     out.clear();  
  18.     out = pageContext.pushBody();  



ps:jspSmartUpload在默认的情况,并不支持下载文件名为中文的。这里我们需要做一些编码的转换,在SmartUpload中加入toUtf8String(String)这个方法,如

Java代码 
  1. public static String toUtf8String(String s)  
  2. {  
  3.   StringBuffer sb = new StringBuffer();  
  4.   for (int i = 0; i < s.length(); i++) {  
  5.     char c = s.charAt(i);  
  6.     if ((c >= 0) && (c <= 'ÿ')) {  
  7.       sb.append(c); } else {  
  8.       byte[] b;  
  9.       try {  
  10.         b = Character.toString(c).getBytes("utf-8");  
  11.       }  
  12.       catch (Exception ex)  
  13.       {  
  14.         byte[] b;  
  15.         System.out.println(ex);  
  16.         b = new byte[0];  
  17.       }  
  18.       for (int j = 0; j < b.length; j++) {  
  19.         int k = b[j];  
  20.         if (k < 0)  
  21.           k += 256;  
  22.         sb.append("%" + Integer.toHexString(k).toUpperCase());  
  23.       }  
  24.     }  
  25.   }  
  26.   return sb.toString();  
  27. }  


修改SmartUpload中downloadFile(String s, String s1, String s2, int i)这个方法,如

Java代码 
  1. public void downloadFile(String s, String s1, String s2, int i)  
  2.     throws ServletException, IOException, SmartUploadException  
  3.   {  
  4.     if (s == null)  
  5.       throw new IllegalArgumentException("File '" + s +   
  6.         "' not found (1040).");  
  7.     if (s.equals(""))  
  8.       throw new IllegalArgumentException("File '" + s +   
  9.         "' not found (1040).");  
  10.     if ((!isVirtual(s)) && (this.m_denyPhysicalPath))  
  11.       throw new SecurityException("Physical path is denied (1035).");  
  12.     if (isVirtual(s))  
  13.       s = this.m_application.getRealPath(s);  
  14.     java.io.File file = new java.io.File(s);  
  15.     FileInputStream fileinputstream = new FileInputStream(file);  
  16.     long l = file.length();  
  17.     boolean flag = false;  
  18.     int k = 0;  
  19.     byte[] abyte0 = new byte[i];  
  20.     if (s1 == null)  
  21.       this.m_response.setContentType("application/x-msdownload");  
  22.     else if (s1.length() == 0)  
  23.       this.m_response.setContentType("application/x-msdownload");  
  24.     else  
  25.       this.m_response.setContentType(s1);  
  26.     this.m_response.setContentLength((int)l);  
  27.     this.m_contentDisposition = (this.m_contentDisposition != null ? this.m_contentDisposition :   
  28.       "attachment;");  
  29.     if (s2 == null)  
  30.       this.m_response.setHeader("Content-Disposition"this.m_contentDisposition +   
  31.         " filename=" + toUtf8String(getFileName(s)));  
  32.     else if (s2.length() == 0)  
  33.       this.m_response.setHeader("Content-Disposition"this.m_contentDisposition);  
  34.     else  
  35.       this.m_response.setHeader("Content-Disposition"this.m_contentDisposition +   
  36.         " filename=" + toUtf8String(s2));  
  37.     while (k < l) {  
  38.       int j = fileinputstream.read(abyte0, 0, i);  
  39.       k += j;  
  40.       this.m_response.getOutputStream().write(abyte0, 0, j);  
  41.     }  
  42.     fileinputstream.close();  
  43.   }  


这样,基本上能够实现对中文名文件的下载! 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值