首页 > 编程知识 正文

JAVA图片(java压缩多个文件)

时间:2023-05-06 06:11:18 阅读:74246 作者:3232

要求:选择多个订单数据,并本地下载与每个订单数据对应的图像

要求:为每个订单数据创建一个文件夹,将与该数据对应的图像批量下载到文件夹中,最后将所有文件夹打包为zip文件并本地下载

代码实现如下:

@getmapping(/downloadiamges ) ) API操作) value='批量下载图像并生成zip文件) ) apimplicitparam ) name='ids ', value=' required=true (publicvoiddounloadimage ) requestparam ) value='ids ' ) String ids,HttpServletRequest request, 对象列表=tibaoservice.get images ) ) ids; //2 .批量下载功能try { simpledateformatsdf=newsimpledateformat (' yyyy-mm-DD ' ); //zip文件的名称,根据情况在此String downloadFilename='战报战斗集' SDF.format(newdate ().zip ); //如果不转换中文,可能会乱码为download filename=urlencoder.encode (download filename,' UTF-8 ' )。 响应返回位置为文件流response.set content type (application/octet-stream ); //默认显示在下拉框中的文件名response.setheader (content-disposition )、) attachment; 文件名称r ' download filename; zipoutputstreamzos=newzipoutputstream (response.get output stream (); for (映射,对象e :列表) string[]s=e.get ) ' fileurl ' ).toString ).split ) )/) ); try { URL URL=newurl (e.get (' fileurl ' ).toString ) ); //连接fileurlurlconnectioncon=URL.open connection (打开; //设置请求超时为5scon.setconnecttimeout(5*1080 ); //输出文件的流程在此设置文件夹名称(如果适用,此处的fileSF=newfile(e.get ) tiba ocode (.tostring ) ).-e.get () EMPmame ) ) 根据具体情况修改此处的zos.putnextentry(newzipentry ) SF.getpath ) (((() s.length-1 ) ) ) )。 inputstream fis=URL.open connection ().getInputStream ); byte[] buffer=new byte[1024]; int r=0; wile((r=fis.read ) ) buffer )!=-1 ) Zos.write(buffer,0,r ); (} fis.close ); }catch(ioexceptionioexception ) { zos.flush ); zos.close (; }}catch(exceptione ) system.out.println ) e; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。