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

什么网站做电子章做得好建设工程 法律 网站

什么网站做电子章做得好,建设工程 法律 网站,2021软件公司排名,环保网站案例文章目录 设想场景实现流程小结 设想场景 为方便老师录入大量学生图片信息#xff0c;在添加照片时#xff0c;学生的相关资料以身份证号码图片描述命名如 #xff08;1231231234567一寸照片.jpg#xff09; #xff08;1231231234567身份证正面照片.jpg#xff09; 在添加照片时学生的相关资料以身份证号码图片描述命名如 1231231234567一寸照片.jpg 1231231234567身份证正面照片.jpg 1231231234567身份证背面照片.jpg 1231231234567毕业证照片.jpg 1231231233123学位证照片.jpg 压缩zip后上传保存学生资料 并实现后续可下载指定学生的资料包。 实现流程 就是一读写操作。 下面是代码实现 PostMapping(/importZip) public void importZip(MultipartFile file) {studentService.importZip(file); }import org.springframework.mock.web.MockMultipartFile;public void importZip(MultipartFile file) {MapString, MultipartFile fileMap new HashMap();try {// 获取zip文件输入流ZipInputStream zipInputStream new ZipInputStream(file.getInputStream(), Charset.forName(GBK));// 转成 名字对应文件流的mapfileMap convertToMultipartFile(zipInputStream);for (Map.EntryString, MultipartFile stringFileEntry : fileMap.entrySet()) {// 上传并返回新文件名称// 上传这块走自己的接口咯、目的是上传图片后获取url保存起来String url FileUploadUtils.upload(filePath, stringFileEntry.getValue());}}catch (Exception e){e.printStackTrace();}}public static MapString, MultipartFile convertToMultipartFile(ZipInputStream zipInputStream) throws IOException {MapString, MultipartFile result new HashMap();// 读取zip文件中的条目ZipEntry zipEntry zipInputStream.getNextEntry();while (zipEntry ! null) {if (!zipEntry.isDirectory()) {byte[] bytes readAllBytesFromZipInputStream(zipInputStream);// 创建MockMultipartFile并返回result.put(zipEntry.getName(), new MockMultipartFile(zipEntry.getName(), zipEntry.getName(),, new ByteArrayInputStream(bytes)));zipInputStream.closeEntry();}zipEntry zipInputStream.getNextEntry();}zipInputStream.close();return result;}public static byte[] readAllBytesFromZipInputStream(ZipInputStream zipInputStream) throws IOException {ByteArrayOutputStream byteArrayOutputStream new ByteArrayOutputStream();byte[] buffer new byte[1024];int bytesRead;while ((bytesRead zipInputStream.read(buffer)) ! -1) {byteArrayOutputStream.write(buffer, 0, bytesRead);}return byteArrayOutputStream.toByteArray();}好的、上面的代码可以实现导入zip并且把压缩包里的文件名和文件流一一返回给你了。 你只需要把文件流调上传你的接口、并把文件名和url存下来后面会用到。 下面我们来说说、正面把上传的文件、压缩成zip 导出。 public void download(HttpServletResponse response, String studentId) {//把你的资料获取出来ListString imageUrls null;//把你的资料获取出来// 创建一个临时文件夹用于存放下载的图片File tempFolder new File(/newFile);tempFolder.mkdirs();try {// 遍历图片URL列表下载并压缩图片for (String imageUrl : imageUrls) {try (BufferedInputStream in new BufferedInputStream(bufferedReader(imageUrl));FileOutputStream fileOutputStream new FileOutputStream(tempFolder.getPath() / imageUrl.substring(imageUrl.lastIndexOf(/) 1))) {byte[] dataBuffer new byte[1024];int bytesRead;while ((bytesRead in.read(dataBuffer, 0, 1024)) ! -1) {fileOutputStream.write(dataBuffer, 0, bytesRead);}} catch (IOException e) {e.printStackTrace();}}// 创建临时压缩文件File zipFile File.createTempFile(相关资料, .zip);FileOutputStream fos new FileOutputStream(zipFile);ZipOutputStream zos new ZipOutputStream(fos);// 压缩文件夹内的文件File folder new File(tempFolder.getPath());compressFolder(folder, zos);// 关闭流zos.close();fos.close();// 设置响应头response.setContentType(application/zip);response.setHeader(Content-Disposition, attachment; filename\ zipFile.getName() .zip\);// 将压缩文件写入响应输出流FileInputStream fis new FileInputStream(zipFile);BufferedInputStream bis new BufferedInputStream(fis);OutputStream os response.getOutputStream();byte[] buffer new byte[1024];int bytesRead;while ((bytesRead bis.read(buffer)) ! -1) {os.write(buffer, 0, bytesRead);}bis.close();fis.close();os.flush();os.close();// 删除临时压缩文件zipFile.delete();} catch (IOException e) {e.printStackTrace();} finally {//删除临时文件夹及其文件deleteFolder(tempFolder);}}public InputStream bufferedReader(String url) throws IOException {//设置超时时间、不然遇到偶发性的url失效找不到图片就会直接报错、后面都不走了URL downloadUrl new URL(urlEncodeChinese(url));HttpURLConnection httpURLConnection (HttpURLConnection) downloadUrl.openConnection();httpURLConnection.setRequestMethod(GET);httpURLConnection.setDoOutput(true);httpURLConnection.setDoInput(true);httpURLConnection.setUseCaches(false);//设置超时时间httpURLConnection.setConnectTimeout(1000);httpURLConnection.setReadTimeout(1000);return httpURLConnection.getInputStream();}// 递归删除文件夹及其文件private void deleteFolder(File folder) {File[] files folder.listFiles();if (files ! null) {for (File file : files) {if (file.isDirectory()) {deleteFolder(file);} else {file.delete();}}}folder.delete();} 小结 本篇文章 就是解决 在Java项目中实现资料包的导入读取并导出压缩包加强文件读写的能力 冲。
http://www.sadfv.cn/news/57898/

相关文章:

  • 响水县住房和城乡建设局网站伊宁市做网站
  • 响应式网站建设模板下载小型企业网络配置实例
  • 潍坊高端网站设计深圳网站哪家强
  • 备案价公示网站erp系统登录入口
  • 网站开发的步骤过程网站备案帐号找回密码
  • 建设景区网站要有的内容seo挂机赚钱
  • 昆明网站推广哪家好平凉有做企业网站的吗
  • 合肥网站建设百姓网北京到信阳
  • 自己做的网站如何兼容黄强会见韩国忠清南道知事金泰钦
  • 网站建设需要哪些条件软件最全的网站
  • 网站开发类appdw网页制作破解版
  • 建筑网站搜图西湖专业网站设计公司
  • 怎么更改网站栏目id产品外观设计收费标准
  • 灰色网站模板网站建设平台
  • 商城网站如何搭建小手工
  • 58网站建设网站配色风格有哪些
  • 网站后台编辑器源码做网站的费用入什么科目
  • 常州网站制作公司排名微信小说分销平台
  • 网站开发公司哪里济南兴田德润实惠吗重庆网网站建设公司
  • 企业网站优化的方式wordpress 微信抓取
  • 网站备案需要多少时间wordpress春节
  • 哪里的郑州网站建设网站管理功能图
  • 温州建站平台网络传播营销策划方案
  • 山西集团网站建设网站建设客户分析调查表
  • 网站建设好后怎么制作网页做服装网站需要什么
  • 微信网站制作平台网站建设制作深圳
  • 安徽质量工程建设网站58同城本地网页版
  • 住房城乡建设门户网站网站建设html
  • 网站地链接结构提高网站订单转化率
  • 建设网站怎样赚钱免费发布的网站