Home >Java >JavaBase >Introduction to solutions to garbled file names in Java downloads

Introduction to solutions to garbled file names in Java downloads

尚
Original
2019-12-03 10:53:541986browse

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;

Option 1: Encode the file name to Unicode

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

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");

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, 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");
}

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!

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