Home > Java > JavaBase > Solution to garbled code in java file download

Solution to garbled code in java file download

Release: 2019-11-20 10:36:37
Original
3852 people have browsed it

Solution to garbled code in java file download

Solution to Chinese garbled characters when downloading java files:

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

encode() method encodes a string in the specified encoding format.

String's getBytes() method is to obtain a byte array in the operating system's default encoding format.

String.getBytes(String decode) method will return the byte array representation of a string under the specified decode encoding.

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

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