论坛首页 Java版 Struts

struts文件下载的问题?

浏览 3668 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2006-11-29

使用的是struts1.2.6版本,应用服务器是Tomcat 5.5.17

需要下载的文件,在页面生成一个链接标记href="ioAction.do?method=download&path=..."调用IOAction

为什么如果路径path中有中文就不能下载,异常是找不到文件,而英文的可以下载,我已经进行编码转换了?

请,谢谢:)

java 代码
  1. public ActionForward download(ActionMapping mapping, ActionForm form,   
  2.             HttpServletRequest request, HttpServletResponse response) {   
  3.         // TODO Auto-generated method stub   
  4.         try  
  5.         {   
  6.             String path = new String(rootFolder + new String(request.getParameter("path").getBytes("ISO8859-1"),"UTF-8"));   
  7.             File file = new File(path);   
  8.             String filename = file.getName();   
  9.                
  10.             //取得文件的扩展名ext   
  11.             String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();   
  12.                
  13.             InputStream fis = new BufferedInputStream(new FileInputStream(path));   
  14.             byte[] buffer = new byte[fis.available()];   
  15.             fis.read(buffer);   
  16.             fis.close();   
  17.                
  18.             response.reset();   
  19.             response.addHeader("Content-Disposition""attachment;filename="+new String(filename.getBytes()));   
  20.             response.addHeader("Content-Length"""+file.length()); //设置返回的文件类型   
  21.             OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); //得到向客户端输出二进制数据的对象   
  22.             //根据扩展名声称客户端浏览器mime类型   
  23.             if(ext.equals("DOC"))   
  24.                 response.setContentType("application/msword");   
  25.             else  
  26.                 response.setContentType("application/octet-stream"); //设置返回的文件类型   
  27.             toClient.write(buffer); //输出数据   
  28.             toClient.flush();   
  29.             toClient.close();   
  30.             }   
  31.             catch(IOException ex){   
  32.                 ex.printStackTrace();   
  33.             }   
  34.                
  35.             return null;   
  36.         }  
   
时间:2006-11-29
String path = new String(rootFolder + new String(request.getParameter("path").getBytes("ISO8859-1"),"GBK"));

转成GBK的试试
   
0 请登录后投票
时间:2006-12-04
tomcat 配置文件里要改成uft-8
   
0 请登录后投票
时间:2006-12-04
URL encoding 也要做一下
   
0 请登录后投票
时间:2006-12-23
URL中尽量不要使用中文为好
   
0 请登录后投票
时间:2007-01-01
为什么不用
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>cn.edu.ustc.common.EncodingFilter</filter-class>
<init-param>
<param-name>targetEncoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
   
0 请登录后投票
论坛首页 Java版 Struts

跳转论坛:
JavaEye推荐