java文件流的处理方式是什么
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章将为大家详细讲解有关java文件流的处理方式是什么,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。java 文件流的处理 文件打包成zip1、下载文件到本地p
千家信息网最后更新 2026年11月03日java文件流的处理方式是什么
这篇文章将为大家详细讲解有关java文件流的处理方式是什么,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
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() { @Override public void doWithRequest(ClientHttpRequest request) throws IOException { request.getHeaders().add("accept", MediaType.APPLICATION_OCTET_STREAM_VALUE); } }; ResponseExtractor responseExtractor = new ResponseExtractor() { @Override public 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; // 创建 ZipOutputStream ZipOutputStream 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{ //记得删除文件 } }后台多文件打包成zip返回流 前台提供按钮一键下载
项目pom文件添加二维码操作,和文件打包的maven支持:
net.glxn.qrgen javase 2.0 org.apache.commons commons-compress 1.12
前台代码:
js(我用了thymeleaf模板)代码:
后台代码:
/** * @Author Ni Klaus * @Description //TODO 门店总代生成打包产品二维码zip * @Date 上午 10:38 2019/8/20 0020 * @Param [params,response] * @return void **/ @RequestMapping({"getStreamZip"}) @ResponseBody public void findList(@RequestParam Map params,HttpServletResponse response) throws Exception { String agencyId = (String) params.get("agencyId"); AgencyAccount agencyAccount = agencyAccountService.getAccountByAgencyId(agencyId); //这里设置打包后的zip文件名 String downloadName = agencyAccount.getName()+".zip"; try{ response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName=" + new String(downloadName.getBytes(),"ISO8859-1")); }catch(UnsupportedEncodingException e){ log.error("----------下载文件名编码时出现错误------"+e.getMessage()); } OutputStream outputStream = response.getOutputStream(); ZipArchiveOutputStream zous = new ZipArchiveOutputStream(outputStream); zous.setUseZip64(Zip64Mode.AsNeeded); zous.setEncoding("utf-8"); try{ //我这里是通过不同产品类型生成不同产品的二维码图片流 //具体你想生成什么类型的多个文件打包,只需要循环创建ArchiveEntry 然后zous.putArchiveEntry(entry)就可以了 StoreProductType[] storeProductTypes = StoreProductType.values(); for (StoreProductType storeProductType : storeProductTypes) { String url = "http://m.xxx.cn/goods/pay/xxx.html?productid="+storeProductType.getProductId() + "&agencycode="+ agencyId +"&channelid=def&appfrom=sqjk&isstore=1"; //打包文件里的每个文件的名字 String imgName = storeProductType.getDescription()+".png"; ByteArrayOutputStream out = QRCode.from(url).to(ImageType.PNG).withSize(300,300).stream(); byte[] bytes = out.toByteArray(); ArchiveEntry entry = new ZipArchiveEntry(imgName); zous.putArchiveEntry(entry); zous.write(bytes); zous.closeArchiveEntry(); if (out != null) { out.close(); } } }catch (Exception e){ e.printStackTrace(); log.error("---------------门店总代生成二维码打包流出错----------------"+e.getMessage()); }finally{ if(outputStream != null){ outputStream.close(); } if(zous != null){ zous.close(); } } }最后效果:
关于"java文件流的处理方式是什么"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
文件
对象
二维
二维码
生成
方式
处理
产品
代码
实例
篇文章
门店
不同
前台
名称
后台
总代
文件名
更多
类型
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技公司排名
云之行互联网科技
重生之互联网科技帝国
互联网科技官网
互联网科技开发公司
互联网科技英文
上游互联网科技
郑州华宇互联网科技
互联网科技网站
互联网科技汛
温州喔噻互联网科技有限公司
华服互联网科技官网
黑科技闪耀互联网之光
智腾互联网科技有限公司
华夏恒生科技与互联网科技
长沙互联网科技
互联网科技实习生岗位
产业互联网科技服务公司
互联网科技博主张洲
互联网科技创新方面的资料
互联网和科技创新
金融科技还是互联网
互联网公司科技股
重庆互联网医院沙龙医盟信息科技
宜昌元轩互联网科技有限公司
搜狐科技互联网
元气玛特互联网科技公司是外包吗
皮皮虾互联网科技有限公司
厦门江牛科技互联网
互联网企业和网络科技有限公司