Home > Java > JavaBase > body text

Introduction to solutions to garbled file names in Java downloads

Release: 2019-12-03 10:53:54
Original
1926 people have browsed it

Introduction to solutions to garbled file names in Java downloads

java 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. (Recommendation: java video tutorial)

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;
Copy after login

Option 1: Encode the file name to Unicode

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

The file name will be converted to Unicode encoding (test file.doc), The test is normal under the IE browser, but the file name obtained when tested under the Firefox browser is undecoded Unicode.

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

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

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");
Copy after login

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, UTF8 encode the filename through URLEncoder.

Other browsers (firefox, chrome, safari, opera) need to convert bytes to 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");
}
Copy after login

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Introduction to solutions to garbled file names in Java downloads. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template