poi 檔案匯出
阿新 • • 發佈:2018-12-20
public void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName) throws IOException{ //設定響應檔案型別 MINI 可以在tomcat下的cof/web.xml 可以查到 response.setContentType("application/octet-stream;charset=utf-8"); //儲存的檔名,必須和頁面編碼一致,否則亂碼 returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1")); response.addHeader("Content-Disposition", "attachment;filename=" + returnName); response.setContentLength(byteArrayOutputStream.size()); ServletOutputStream outputstream = response.getOutputStream(); //取得輸出流 byteArrayOutputStream.writeTo(outputstream); //寫到輸出流 byteArrayOutputStream.close(); //關閉 outputstream.flush(); //刷資料 }