Java Web response csv file settings (download)

The following is provided in response to excel file format csv

String filename = "掃描全能王" + System.currentTimeMillis() + ".csv";
// 支持文件名带繁体字
byte[] fileNameByte = (filename ).getBytes("UTF-8");
String filename = new String(fileNameByte, "ISO8859-1");
// 弹出对话框让用户下载
response.setHeader("Content-Disposition", "attachment;filename=" + filename);

OutputStream out = null;
BufferedWriter writer= null;
// 设置响应内容,gbk支持繁体字,gb2312不支持
response.setContentType("application/csv;charset=GBK");
response.setCharacterEncoding("GBK");
try {
    out = response.getOutputStream();
    writer= new BufferedWriter(new OutputStreamWriter(out, "GB2312"), 1024);
    // 写入文件内容
    ....
    writer.flush();
} catch (Exception e) {
    log.error("导出异常:{} {}", e.getMessage(), e);
} finally {
//  writer\out 流关闭
    try {
        if(writer!= null) {
            writer.close();
        }
    } catch (IOException e) {
        log.warn("close stream csvWtriter error!");
    }
    try{
        if(out != null){
            out.close();
        }
    }catch (IOException e){
        log.warn("close stream out error!");
    }
}

Explanation

Content-Disposition There are two types of property

  • inline: The contents of the file displayed directly on the page
  • attachment: pop-up dialog box allows users to download

Several coding Definitions
GBK: Character GB spreading code, using the original GB2312-80 substantially all the characters and the code bits, and covers all of the original 20,902 characters in Unicode, contains a total of 883 symbols, characters and provided 21,003 a 1894 coinage code bits. Simplified Chinese version of Microsoft Windows 95 is to the GBK within code, and because GBK also covers all Unicode CJK characters, and Unicode so it can be done one by one.

GB code, stands GB2312-80 "information exchange with Chinese character set encoding basic set", released in 1980, is the national standard Chinese information processing, area (such as Singapore) use of simplified Chinese mainland and overseas are forced to use The only Chinese encoding. P-Windows3.2 and Apple OS that is basic to the GB2312 Chinese character encoding, Windows 95/98 GBK places as the basic Chinese character coding, but compatible support GB2312. GB code Total 6763 simplified Chinese characters, symbols 682, wherein characters parts: a word 3755 to alphabetical order, two word 3008 to sort radical. The development and application of standards to regulate, promote played an important role Chinese information process.

GBK coding is developed in mainland China, equivalent to UCS coding extension of the new Chinese national standards. GBK Working Group in October 1995, the same year in December to complete the GBK specification. The coding standard is compatible GB2312, contains a total of 21,003 characters, symbols 883, and provides code bits 1894 created characters, simple, complex characters and into a library.

Guess you like

Origin blog.csdn.net/sndayYU/article/details/92974400