天天看點

springboot下載下傳中文檔案名亂碼問題解決

@RequestMapping(value = "/download")
@ResponseBody
public void download(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
    //設定中文檔案名與字尾
    String filename= URLEncoder.encode("中文檔案名稱") + ".docx","utf-8").replaceAll("\\+", "%20");
    // 清除buffer緩存
    response.reset();
    // 指定下載下傳的檔案名
    response.setHeader("Content-Disposition",
            "attachment;filename="+filename+"");
    response.setContentType("application/vnd.ms-excel;charset=UTF-8");
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
   ...........
    } catch (Exception e) {
        e.printStackTrace();
    }
}