search
HomeJavaJavaBaseThe file name is garbled when java downloads the file

The file name is garbled when java downloads the file

Nov 23, 2019 pm 03:04 PM
javadownloadGarbled charactersdocument

The file name is garbled when java downloads the file

Web development file download function (code is as follows), if the file name contains Chinese characters, it will often appear garbled and needs to be encoded.

String fileName = "测试文件.doc";
try {
    HttpServletResponse response = 
    (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
    response.setHeader("Content-disposition", "attachment; filename=" + fileName);
    response.setContentLength((int) file.length());
    response.setContentType("application/octet-stream;charset=UTF-8");
    byte[] b = new byte[1024];
    int i = 0;
    FileInputStream fis = new FileInputStream(file);
    ServletOutputStream out = response.getOutputStream();
    while ((i = fis.read(b)) > 0) {
        out.write(b, 0, i);
    }
    out.flush();
    out.close;
    fis.close();
} catch (IOException e) {
    e.printStackTrace();
}
FacesContext.getCurrentInstance().responseComplete();
return null;

Option 1: Encode the file name to Unicode

fileName = URLEncoder.encode(fileName, "UTF-8");

The file name will be converted to Unicode encoding. The test is normal under the IE browser, but under the Firefox browser The file name obtained by the test is Unicode without decoding.

Option 2: Encode the file name to ISO-8859-1

fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");

This method can obtain files with Chinese file names under both IE and Firefox. But if there are spaces in the file name, Firefox will truncate the file name from the spaces, and you need to replace the spaces in the file name with visible characters such as underscores before encoding.

fileName = new String(fileName.replace(" ", "_").getBytes("UTF-8"), "ISO-8859-1");

Option 3: Browser incompatibility

In Java web development, the problem of garbled file names in file download functions is often encountered. Different browsers have different solutions to this problem.

For IE, filename is UTF8-encoded through URLEncoder, while other browsers (firefox, chrome, safari, opera) need to convert bytes into ISO8859-1.

if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
    filename = URLEncoder.encode(filename, "UTF-8");
} else {
    filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");
}

Recommended tutorial: Getting started with java development

The above is the detailed content of The file name is garbled when java downloads the file. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.