压缩包上传及解压

来源:互联网 发布:小米5怎么卸载软件 编辑:程序博客网 时间:2024/06/05 00:35
public final class FileUtil{<span style="white-space:pre"></span>/* * 解压zip包 * zipPath:解压包路径 * descDir:压缩指定位置 * */public static void decompressionZIP(File zipPath,String descDir) throws IOException{ File pathFile = new File(descDir);          if(!pathFile.exists()){              pathFile.mkdirs();          }          ZipFile zip = new ZipFile(zipPath,"GBK");          for(Enumeration entries = zip.getEntries();entries.hasMoreElements();){              ZipEntry entry = (ZipEntry)entries.nextElement();              String zipEntryName = entry.getName();              InputStream in = zip.getInputStream(entry);              String outPath = (descDir+"/"+zipEntryName).replaceAll("\\*", "/");;              //判断路径是否存在,不存在则创建文件路径              File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));              if(!file.exists()){                  file.mkdirs();              }              //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压              if(new File(outPath).isDirectory()){                  continue;              }              OutputStream out = new FileOutputStream(outPath);              byte[] buf1 = new byte[1024];              int len;              while((len=in.read(buf1))>0){                  out.write(buf1,0,len);              }              in.close();              out.close();          }  }/* * 上传压缩包 * */public static File uploadZip(MultipartFile multifile, File outputPath) throws IOException{        if (!outputPath.exists()) {          outputPath.mkdir();          }          // 读取文件流并保持在指定路径          InputStream inputStream = multifile.getInputStream();          String output = outputPath.getPath() + "/"+multifile.getOriginalFilename();        OutputStream outputStream = new FileOutputStream(output);          byte[] buffer = multifile.getBytes();          int byteread = 0;          while ((byteread = inputStream.read(buffer)) != -1) {              outputStream.write(buffer, 0, byteread);              outputStream.flush();          }          outputStream.close();          inputStream.close();          return new File(output);}}


SpringMVC配置

<!-- 文件上传 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>


Controller得到MultipartFile

@RequestMapping("/upload")public String upload( @RequestParam("file") MultipartFile multifile){ // 创建目录  <span style="white-space:pre"></span>try {cnmlService.persisted(multifile);} catch (Exception e) {}return "suc";}





0 0
原创粉丝点击