【瀏覽器開啟匯出的excel】

我是太陽啦啦啦發表於2018-08-19
public void setResponseHeader(HttpServletResponse response,String filePath) {

        try {
            File file = new File(filePath);
            String filename = file.getName();
            if(  !file.exists()){
                return;
            }
            response.reset();
            FileInputStream fileInput = new  FileInputStream(file);
            OutputStream outPut = response.getOutputStream();

            byte[] buffer = new byte[1024];
            int len;

            while ((len = fileInput.read(buffer)) > 0){
                outPut.write(buffer, 0, len);
            }
            fileInput.close();
            response.addHeader("Content-Disposition", "attachment;filename=" +new String(filename.getBytes("UTF-8"), "utf-8"));
            response.addHeader("Content-Length", "" + file.length());
            response.setContentType("application/x-msdownload");
        } catch (IOException ex) {
            ex.printStackTrace();
            return;
        }
    }

相關文章