当前位置: 首页 > news >正文

景安企业网站建设有什么网站招聘做危险高薪工作的

景安企业网站建设,有什么网站招聘做危险高薪工作的,运动健身类网站开发,北海网站建设服务商我的需求是将minio中存储的文件按照查询条件查询出来统一压成一个zip包然后下载下来。 思路#xff1a;针对这个需求#xff0c;其实可以有多个思路#xff0c;不过也大同小异#xff0c;一般都是后端返回流文件前端再处理下载#xff0c;也有少数是压缩成zip包之后直接给…我的需求是将minio中存储的文件按照查询条件查询出来统一压成一个zip包然后下载下来。 思路针对这个需求其实可以有多个思路不过也大同小异一般都是后端返回流文件前端再处理下载也有少数是压缩成zip包之后直接给下载链接返回到前端前端收到链接url直接window.open()进行下载不过这种下载zip包的路径要确保是在网站下否则访问不到还有一个缺点就是文件没法删除占用存储空间后期需人为动作清理选择哪种思路就可以看具体需求啦我选择的是第一种思路以下就针对第一种后端返回流方式进行具体介绍。 首先说第一种方法将需要下载的文件找到minio中有查询方法将文件转成inputStream这里就不多说了拿到一组InputStream我们就可以写入一个zip包里了创建临时zip路径将流遍历写入文件读取临时zip文件再写入response中的outputStream最后删除临时文件。 前端处理方法最后统一介绍后端核心代码如下 public void downloadZip(String name, ListMediaFileEntity filePaths,HttpServletResponse response){File zipFile compressedFileToZip(name,filePaths);ByteArrayOutputStream os new ByteArrayOutputStream();try {FileInputStream ins new FileInputStream(zipFile);WritableByteChannel writableByteChannel Channels.newChannel(os);FileChannel fileChannel ins.getChannel();fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);fileChannel.close();response.setCharacterEncoding(UTF-8);name URLEncoder.encode(name, UTF-8);response.setContentType(application/octet-stream);response.addHeader(Content-Disposition, attachment;filename new String(name.getBytes(iso8859-1)));response.setContentLength(os.size());response.setHeader(filename, name);response.addHeader(Content-Length, os.size());var outputstream response.getOutputStream();os.writeTo(outputstream);os.flush();os.close();outputstream.flush();outputstream.close();writableByteChannel.close();if(zipFile.exists()){//删除临时文件zipFile.delete();}} catch (IOException e) {e.printStackTrace();} finally {try {os.close();} catch (IOException e) {e.printStackTrace();}}}/*** 构建临时zip文件**/public File compressedFileToZip(String name, ListMediaFileEntity mediaFileEntityList) {String zipName name.concat(LocalDateTime.now().format(DateTimeFormatter.ofPattern(yyyyMMddHHmmss))).concat(.zip);//临时zip路径String fileZipPath System.getProperty(user.dir).concat(/).concat(zipName);OutputStream os null;ZipOutputStream zos null;File file new File(fileZipPath);try {if(!file.exists()){file.createNewFile();}os new FileOutputStream(file);zos new ZipOutputStream(os) ;for (MediaFileEntity entity:mediaFileEntityList) {zos.putNextEntry(new ZipEntry(entity.getFileName()));//minio 获取流InputStream ins ossService.getObject(OssConfiguration.bucket,entity.getObjectKey());FileInputStream insfconvertToFileInputStream(ins);WritableByteChannel writableByteChannel Channels.newChannel(zos);FileChannel fileChannel insf.getChannel();fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);zos.closeEntry();fileChannel.close();ins.close();}} catch (IOException e) {e.printStackTrace();}finally {if(zos ! null){try {zos.close();} catch (IOException e) {e.printStackTrace();}}if(os ! null){try {os.close();} catch (IOException e) {e.printStackTrace();}}}return file;} 第二种方法安装hutool依赖调用hutool包中的ZipUtil工具类中的zip方法进行下载。此方法需要有3个参数分别是OutoutStream每个流对应的文件名字符串数组文件的InputStream数组。 首先将需要下载的文件找到拿到一组InputStream也就是zip方法中的第3个参数第一个参数顾名思义就是你想要输出的地方我们是返回给前端所以就是response.getOutputStream()第二个参数我们遍历文件时也可以拿到废话不多说了上代码看吧。 在项目下安装hutool依赖 dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.5.7/version /dependency /*** 下载多个文件转zip压缩包** param mediaFileEntityList* param response* throws Exception*/public void dowloadToZip(ListMediaFileEntity mediaFileEntityList, HttpServletResponse response) throws Exception {int i 0;//如果有附件 进行zip处理if (mediaFileEntityList ! null mediaFileEntityList.size() 0) {try {//被压缩文件流集合InputStream[] srcFiles new InputStream[mediaFileEntityList.size()];//被压缩文件名称String[] srcFileNames new String[mediaFileEntityList.size()];for (MediaFileEntity entity : mediaFileEntityList) {//以下代码为获取图片inputStreamInputStream ins ossService.getObject(OssConfiguration.bucket,entity.getObjectKey());if (ins null) {continue;}//塞入流数组中srcFiles[i] ins;srcFileNames[i] entity.getFileName();i;}response.setCharacterEncoding(UTF-8);response.setHeader(Content-Disposition, attachment;filename URLEncoder.encode(下载.zip, UTF-8));//多个文件压缩成压缩包返回ZipUtil.zip(response.getOutputStream(), srcFileNames, srcFiles);} catch (IOException e) {e.printStackTrace();}}} Controller这边可以直接写成没有返回值的接口我的例子如下仅供参考 GetMapping(/{workspace_id}/fileDownList)ApiOperation(value 查询文件的下载地址)public void getFileStreamList(PathVariable(name workspace_id) String workspaceId,RequestParam(name ids) String ids, HttpServletResponse response) throws Exception {ListInteger fileIds Arrays.stream(ids.split(,)).collect(Collectors.toList()).stream().map(Integer::parseInt).collect(Collectors.toList());ListMediaFileEntity mediaFileEntityList fileService.getMediaListById(workspaceId, fileIds); // fileUtil.downloadZip(111,mediaFileEntityList,response);//第一种方法fileUtil.dowloadToZip(mediaFileEntityList,response);//第二种方法} 到此后端zip下载就完毕了下面我们说说前端如何处理 网上查询前端处理大致都是如下但是我自己使用的时候下载总是提示损坏后找了一个工具类直接调用就可以了示例代码请求是get如需调整可根据情况自行调整 核心代码如下 import { saveAs } from file-saver; const baseURL (window as any).config.VITE_APP_BASE_API; //import.meta.env.VITE_APP_BASE_API;export default { zip(url: string, name: string) {url baseURL url;axios({method: get,url: url,responseType: blob,headers: { Authorization: Bearer getToken() },}).then(res {const isBlob blobValidate(res.data);if (isBlob) {const blob new Blob([res.data], { type: application/zip });this.saveAs(blob, name);} else {this.printErrMsg(res.data);}});},saveAs(text: any, name: string, opts?: any) {saveAs(text, name, opts);},async printErrMsg(data: any) {const resText await data.text();const rspObj JSON.parse(resText);const errMsg errorCode[rspObj.code] || rspObj.msg || errorCode[default];// ElMessage.error(errMsg);},blobValidate(data: any) {return data.type ! application/json;} }; 按钮绑定方法直接调用zip下载方法传参为url和要导出zip的名称示例如下 import download from /plugins/download;function batchDownload(){ElMessage.success(文件下载中请勿重复点击);download.zip(/media/api/v1/files/${workspaceId}/fileDownList?ids${selectlist.value.join(,)},MediaFilesnew Date().toLocaleDateString().zip)}
http://www.pierceye.com/news/881668/

相关文章:

  • 做排版的网站绍兴网站建设专业的公司
  • 深圳专业网站建设公司济南建设网站
  • 广西建设职业技术学院网站做的比较好的二手交易网站有哪些
  • 北辰网站建设公司wordpress79元主题
  • 网站怎么绑定织梦广东网站建设968
  • ps扩展插件网站业务平台
  • 富阳设计网站广州网站设计公司
  • 演讲网站开发背景wordpress 1h 1g
  • 广州十度网络网站开发最好网站被挂马怎么办
  • 网站建设及维护费算业务宣传费网站如何做触屏滑动效果
  • 如何查询一个网站所属的主机免费可以绑定域名网站空间
  • asp.net网站开发教程品牌型网站建设
  • 海南网站网络推广建设网站需要注意的事项
  • 营销外包网站网站类别标签文本
  • 企业概况简介解答网站内容优化策略
  • ImQQ网站是怎么做的做网站把自己做死
  • 房产网站制作流程php网站开发教程网
  • 小程序商城名字谷歌优化技巧
  • 备案的时候需要网站吗seo搜索引擎优化公司
  • 网站 空间转移wordpress后台点击菜单没反应应
  • 企业网站可以自己做国外域名交易网站
  • 龙岗网站建设费用明细国外的服务器做的网站在国外能打开在国内打不开是什么原因
  • 个人网站的设计与实现摘要东莞学校网站建设
  • 深圳建设局招标网站网站空间pdf下载不了
  • 中国网站建设服务中心百度搜索风云榜电脑版
  • 开发网站性能监控网站开发常见技术问题
  • wordpress 手风琴插件长沙网站优化联系方式
  • 上海松江水处理网站建设做网站项目
  • 长沙快速建站模板仿牌网站怎么做301跳转
  • 网站建设与管理和计算机网络技术网站运行速度慢的原因