檔案下載檔名亂碼處理

bei_fang_du_lang發表於2017-04-21

1、jsp檔案

    response.setContentType("application/x-download");
    String filedisplay = bean.getATTACH_TITLE();
    String userAgent = request.getHeader("User-Agent");
    if (userAgent.contains("MSIE")||userAgent.contains("Trident")) {
        filedisplay = java.net.URLEncoder.encode(filedisplay, "UTF-8");
    } else {
        //非IE瀏覽器的處理:
        filedisplay = new String(filedisplay.getBytes("UTF-8"),"ISO-8859-1");
    }
    response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);
    java.io.OutputStream outp = response.getOutputStream();
    outp.write(content, 0, content.length);
    outp.flush();
    outp.close();

2、rest檔案

            byte[] content = bean.getATTACH_CONTENT();
            InputStream is = new ByteArrayInputStream(content);
            Disposition disposition = new Disposition(Disposition.TYPE_ATTACHMENT);
            String userAgent = getRequest().getClientInfo().getAgent();
            if (userAgent.contains("MSIE")||userAgent.contains("Trident")) {
                fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
            } else {
                //非IE瀏覽器的處理:
                fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
            }
            disposition.setFilename(fileName);
            Representation rep = new InputRepresentation(is);
            rep.setDisposition(disposition);

3、js檔案

encodeURI(fileName)

相關文章