您好,登錄后才能下訂單哦!
java 文件大數據Excel下載實例代碼
excel可以用xml表示。故可以以此來實現邊寫邊下載文件
package com.tydic.qop.controller; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.tydic.qop.vo.param.RealTimeReportParamVo; @Controller @RequestMapping(value = "/exportStream") public class testExportByStream { /* * 導出文件通過流 */ @RequestMapping(value = "/exportStream.html") @ResponseBody public String exportByStream(RealTimeReportParamVo params, HttpServletResponse response) throws Exception{ String fileName="接口統計分析"; response.reset(); response.setContentType("application/octet-stream;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".txt").getBytes(), "iso-8859-1")); ServletOutputStream out = response.getOutputStream(); BufferedInputStream bis = null; BufferedOutputStream bos = null; for(int i=0;i<1000000;i++){ String contentStr="aaa自己寫的controller"+i+"\n"; System.out.println(contentStr); byte[] contentByte=(contentStr).getBytes(); InputStream is = new ByteArrayInputStream(contentByte); readWrite(is,out,bis,bos); } if (bis != null) bis.close(); if (bos != null) bos.close(); return null; } public void readWrite(InputStream is,ServletOutputStream out,BufferedInputStream bis,BufferedOutputStream bos){ try { bis = new BufferedInputStream(is); bos = new BufferedOutputStream(out); byte[] buff = new byte[2048]; int bytesRead; // Simple read/write loop. while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } bos.flush(); } catch (final IOException e) { e.printStackTrace(); } } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。