日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 文件流的处理 文件打包成zip

發布時間:2024/1/17 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 文件流的处理 文件打包成zip 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、下載文件到本地

public void download(HttpServletResponse response){String filePath ="";//文件路徑String fileName ="";//文件名稱// 讀到流中InputStream inStream = new FileInputStream(filePath);// 設置輸出的格式response.reset();response.setContentType("bin");response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");IOUtils.copy(inStream, response.getOutputStream()); }

2、java后端下載

方式一: new URL(fileUrl + item.getcBhFileserver()).openStream()方法二:public Boolean addFile(String url, String id, String fileName) {RequestCallback requestCallBack = new RequestCallback() {@Overridepublic void doWithRequest(ClientHttpRequest request) throws IOException {request.getHeaders().add("accept", MediaType.APPLICATION_OCTET_STREAM_VALUE);}};ResponseExtractor<Boolean> responseExtractor = new ResponseExtractor<Boolean>() {@Overridepublic Boolean extractData(ClientHttpResponse response) throws IOException {if (response.getStatusCode() == HttpStatus.OK) {//得到文件流InputStream input = response.getBody();return true;}return false;}};return restTemplate.execute(url, HttpMethod.GET, requestCallBack, responseExtractor, id);}

?

3、文件打包成zip

public void zipFilesAll() throws Exception {String zipPath = "";//zip包路徑String zipFileName = "";//zip包名稱File zipFile = new File(zipFileName .toString());// 創建 FileOutputStream 對象FileOutputStream fileOutputStream = null;// 創建 ZipOutputStreamZipOutputStream zipOutputStream = null;try {//創建文件夾zipFile = new File(zipPath );FileUtils.forceMkdir(zipFile);//創建文件zipFile = new File(zipFileName .toString());if (!zipFile.exists()) {zipFile.createNewFile();}// 實例化 FileOutputStream 對象fileOutputStream = new FileOutputStream(zipFileName.toString());// 實例化 ZipOutputStream 對象zipOutputStream = new ZipOutputStream(fileOutputStream);// 創建 ZipEntry 對象ZipEntry zipEntry = null;for (CL cl: ClList) {// 實例化 ZipEntry 對象,源文件數組中的當前文件zipEntry = new ZipEntry(tCltjjl.getcClmc() + ".zip");zipOutputStream.putNextEntry(zipEntry);IOUtils.copy(new FileInputStream(cl.getcPath(), zipOutputStream);}} catch (Exception e) {}finally{//記得刪除文件}}

  

轉載于:https://www.cnblogs.com/jiehanshi/p/11533465.html

總結

以上是生活随笔為你收集整理的java 文件流的处理 文件打包成zip的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。