Bootstrap

【JavaEE】JavaWeb的HttpServletResponse下载功能的关键代码

//通过Response把数据以Excel格式保存
String filename="下载.xlsx";
response.reset();
response.setContentType("application/octet-stream;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
//输入流 根据自己业务形成的输入流
FileInputStream is = new FileInputStream("某路径");
try(OutputStream out = response.getOutputStream()){
    response.addHeader("Content-Disposition","attachment;"+ "filename="+ new String(fileName.getBytes("GBK"),"ISO8859_1"));
    //Apache commons包下的IOUtils
    IOUtils.copy(is,out);
} catch (Exception e) {
    e.printStackTrace();
}
;