全类型_文件下载_处理jsp传参到后端乱码问题_后端到jsp中文名为空问题

全类型_文件下载_处理jsp传参到后端乱码问题_后端到jsp中文名为空问题

首先是文件下载jsp
这里使用简单的input按钮

<td>
   <input type="button" value="下载" onclick="doDownLoades('${item.uploadUrl}')"/>
 </td>
<script type="text/javascript">
    var downloadUrl = baseHref + "/xxxx/download.do";
    function doDownLoades(uploadUrl) {
        window.open(downloadUrl + '?fileName=' + uploadUrl);
        return false;
    }
</script>  

controller(存储采用的时间戳+"_"+文件后缀)

// 后端接口
@RequestMapping(value = "download.do")
    public String templateDownload(String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.debug("---------------------------------fileName2"+fileName);
        String filePath = null;
        String fileNamees="";
        String fileother="";
        if(fileName.contains("_")){
            fileNamees= fileName.indexOf("_") != -1 ? fileName.substring(0,fileName.lastIndexOf("_")) : null;
            fileother = fileName.indexOf("_") != -1 ? fileName.substring(fileName.lastIndexOf("_") + 1, fileName.length()) : null;
        }else{
           return   null;
        }
        System.out.println("++++++++++++++++++++++++++++++++++++++++++fileName3:"+fileNamees);
        //设置响应头和客户端保存文件名
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + new String(fileNamees .getBytes(), "ISO-8859-1") + "." + fileother);
        System.out.println("++++++++++++++++++++++++++++++++++++++++++fileName4:"+fileNamees);
        //用于记录以完成的下载的数据量,单位是byte
        long downloadedLength = 0l;
        try {
            filePath = request.getSession().getServletContext().getRealPath("/");
            filePath += "/resources/file/" + fileNamees + "." + fileother;
            logger.debug("-------------------------------filePath--"+filePath);
            //打开本地文件流
            InputStream inputStream = new FileInputStream(filePath);
            //激活下载操作
            OutputStream os = response.getOutputStream();

            //循环写入输出流
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
                downloadedLength += b.length;
            }
            // 这里主要关闭。
            os.close();
            inputStream.close();
        } catch (Exception e) {
            throw e;
        }
        return null;
    }

我的是本地不乱码,到测试环境乱码(jsp弹框内容不乱吗说明jsp也没有乱码,只有到controller才乱码)

jsp传到前段乱码有几种方式:

(后边的几种方式都试过了,第一种最有效,后边的都没什么用)

  1. Tomcat 看的配置:
    在里面加这个 URIEncoding=“UTF-8” useBodyEncodingForURI=“true” (最主要是这个,先设置utf-8试试 有用的话第二个就不用加了)
  2. jsp: &xxx=’+encodeURI(xxx)+’&cname=’+encodeURI(cname)+’&yyy=’+encodeURI(yyy); (没多大用)
  3. 后台:
    new String(request.getParameter(“xxx”).getBytes(“ISO-8859-1”),“utf-8”);
    request.setCharacterEncoding(“utf-8”);//这个要写在最前面,不然没用;

后端到jsp中文名为空问题
解决方案:在设置fileName的时候进行转码(一定要转为"ISO-8859-1")

filename = new String(filename .getBytes(), "ISO-8859-1");

因为Header中只支持ASCII,传输的文件名必须是ASCII。

原因

ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCII一致,0x80-0x9F之间是控制字符,0xA0-0xFF之间是文字符号。

希望对你们有帮助~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值