admin 管理员组文章数量: 1184232
java解析zip格式压缩包
做项目时遇到需要将zip格式的压缩包解析里面的图片 将里面的图片保存到文件夹 并且保存到数据库中关联起来
在上传时判断是否为zip格式的文件
@RequestMapping(value ="/file", method =RequestMethod.POST)@ApiOperation(value ="文件上传")publicResult<Object>upload(@RequestParam(required =false)MultipartFile file,@RequestParam(required =false)String base64,@RequestParam(required =false)String pid,HttpServletRequest request){Setting setting = settingService.get(SettingConstant.OSS_USED);if(setting ==null||StrUtil.isBlank(setting.getValue())){returnnewResultUtil<Object>().setErrorMsg(501,"您还未配置OSS存储服务");}// IP限流 在线Demo所需 5分钟限1个请求// String token = redisRaterLimiter.acquireTokenFromBucket("upload:"+ipInfoUtil.getIpAddr(request), 1, 300000);// if (StrUtil.isBlank(token)) {// throw new XbootException("上传那么多干嘛,等等再传吧");// }if(StrUtil.isNotBlank(base64)){// base64上传
file =Base64DecodeMultipartFile.base64Convert(base64);}String result ="";String fKey =CommonUtil.renamePic(file.getOriginalFilename());File f =newFile();try{InputStream inputStream = file.getInputStream();// 上传至第三方云服务或服务器
result = fileManageFactory.getFileManage(null).inputStreamUpload(inputStream, fKey, file);if(StrUtil.isNotBlank(pid)){
f.setProjectId(pid);}
f.setLocation(getType(setting.getValue()));// 保存数据信息至数据库
f.setName(file.getOriginalFilename());
f.setSizes(file.getSize());
f.setType(file.getContentType());
f.setFKey(fKey);
f.setUrl(result);
fileService.save(f);// 上传的是zip格式文件时调用zip解析方法将里面的图片保存到文件夹并且保存到数据库关联if(f.getType().equals("application/x-zip-compressed")){System.out.println("哈哈哈哈");
companyEmissionController.fileImg(result);}}catch(Exception e){
log.error(e.toString());returnnewResultUtil<Object>().setErrorMsg(e.toString());}if(setting.getValue().equals(SettingConstant.LOCAL_OSS)){OssSetting os =newGson().fromJson(settingService.get(SettingConstant.LOCAL_OSS).getValue(),OssSetting.class);
result = os.getHttp()+ os.getEndpoint()+"/"+ f.getId();}returnnewResultUtil<Object>().setData(result);}@RequestMapping(value ="/fileImg", method =RequestMethod.GET)@ApiOperation(value ="文件上传")publicResult<Object>fileImg(@RequestParam(required =false)String fileAddress)throwsIOException{//zip文件解压路径String unZipAddress ="E:\\upload\\dcqy";//去目录下寻找文件java.io.File file =newjava.io.File(fileAddress);ZipFile zipFile =null;try{
zipFile =newZipFile(file,"GBK");//设置编码格式}catch(IOException exception){
exception.printStackTrace();returnnewResultUtil<Object>().setErrorMsg("解压文件不存在!");}Enumeration e = zipFile.getEntries();while(e.hasMoreElements()){org.apache.tools.zip.ZipEntry zipEntry =(ZipEntry) e.nextElement();String str = zipEntry.getName().substring(0, zipEntry.getName().indexOf("/"));String name = zipEntry.getName().substring(str.length()+1, zipEntry.getName().length());CompanyEmission ce =newCompanyEmission();String uid = UUID.randomUUID().toString();if(StringUtils.isNotBlank(name)){cn.jcs.xboot.modules.file.entity.File fe =newcn.jcs.xboot.modules.file.entity.File();String cName = name.substring(0, name.indexOf("-"));
ce = companyEmissionService.findName(cName);if(StringUtils.isNotBlank(ce.getId())){
fe.setName(name);
fe.setLocation(0);
fe.setUrl("E:/upload/dcqy"+"/"+ uid +".jpg");
fe.setProjectId(ce.getId());
fe.setFKey(uid +".jpg");
fe.setSizes(zipEntry.getSize());
fe.setType("image/jpeg");
fileService.save(fe);}}if(zipEntry.isDirectory()){File f =newFile(unZipAddress +"/"+ uid +".jpg");
f.mkdirs();}else{File f =newFile(unZipAddress +"/"+ uid +".jpg");
f.getParentFile().mkdirs();
f.createNewFile();InputStream is = zipFile.getInputStream(zipEntry);FileOutputStream fos =newFileOutputStream(f);int length =0;byte[] b =newbyte[1024];while((length = is.read(b,0,1024))!=-1){
fos.write(b,0, length);}
is.close();
fos.close();}}if(zipFile !=null){
zipFile.close();}// file.deleteOnExit();//解压完以后将压缩包删除returnnewResultUtil<Object>().setData(null);}版权声明:本文标题:防患于未然:利用Java检测ZIP包内的非法文件,比如Adobe Flash Player相关风险! 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1770942239a3539318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论