zoukankan      html  css  js  c++  java
  • servlet下载,解决文件名中有中文下载路径出现乱码不能正常下载问题

    方法很多种,我只试用了两种。


    主页面JSP中引入下载功能所需的js文件。引入的时候设置编码格式例如

    <script type="text/javascript" charset="UTF-8" src="${rsc}/pages/communicate/message/receiveMessage.js"></script>


    然后下载方法需改进。下面改进的下载方法是基于本博客里的另一篇文章serlvet下载中的下载方法,下载路径中拼接的参数不要作为方法中带的参数传递。直接用request获取。这样就不会自行转码。


        @RequestMapping("/download")
        @RolesAllowed(AuthorityDefine.ROLE_USER)
        public void download(HttpServletRequest request,
                HttpServletResponse response){
            try{
                String path=request.getParameter("path");
                String cat=request.getParameter("cat");
                File file = new File(Global.BASE_UPLOAD_FOLDER+"/"+ cat+"/"+path);
                String filename = file.getName();
                InputStream fis = new BufferedInputStream(new FileInputStream(file));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                response.reset();
                response.addHeader("Content-Disposition","attachment;filename="+ filename);
                response.addHeader("Content-Length","" + file.length());
                response.setContentType("application/octet-stream");
                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                toClient.write(buffer);
                toClient.flush();
                toClient.close();
            }     
            catch (IOException ex){
                ex.printStackTrace();
            }
        }



    还有一种方式 是直接修改

    配置时需要在tomcat(即相应的Servers)中的server.xml文件中的 <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"
        
        
        />加入useBodyEncodingForURI="true"

  • 相关阅读:
    常用数据库的驱动类/URL/默认端口
    设备驱动程序
    linux内存管理解析1----linux物理,线性内存布局及页表的初始化
    UVA 10564
    ARM GCC CodeSourcery 下载地址
    Linux Shell编程入门
    Flume研究心得
    Bluetooth in Android 4.2 and 4.3(三):Enable Bluetooth
    <机器学习实战>读书笔记--logistic回归
    <机器学习实战>读书笔记--朴素贝叶斯
  • 原文地址:https://www.cnblogs.com/pangblog/p/3341602.html
Copyright © 2011-2022 走看看