Home >Java >JavaBase >Solution to Java download garbled code

Solution to Java download garbled code

尚
Original
2019-12-21 15:07:542648browse

Solution to Java download garbled code

Download file garbled solution code:

  String userAgent = request.getHeader("User-Agent");  
    String formFileName = file.getFileName();  
        
    // 针对IE或者以IE为内核的浏览器:  
    if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {  
        formFileName = java.net.URLEncoder.encode(formFileName, "UTF-8");  
    } else {  
        // 非IE浏览器的处理:  
        formFileName = new String(formFileName.getBytes("UTF-8"), "ISO-8859-1");  
    }  
    response.setHeader("Content-disposition",String.format("attachment; filename=\"%s\"", formFileName));  
    response.setContentType("multipart/form-data");   
    response.setCharacterEncoding("UTF-8");

URLEncoder.encode(String s, String enc): Use the specified encoding mechanism to convert the string into application/x- www-form-urlencoded format.

The new String(byte[],decode) method uses the specified encoding decode to parse byte[] into a string.

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

The above is the detailed content of Solution to Java download garbled code. 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