直接上代码吧


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;import com.jingren.jing.school.entrysystem.bean.entryinfo.EntryInfo;
import com.jingren.jing.school.entrysystem.service.entryinfo.EntryInfoService;
import com.jingren.jing.util.DeleteFile;
import com.jingren.jing.util.ResponseUtils;
import com.jingren.jing.util.UploadAddress;/**
* @Title: UploadZipController.java
* @Package com.jingren.jing.educational.controller.uploadpic
* @Description: TODO 解压并读取zip文件
* @author  MR.Lu
* @date 2017年1月6日 上午11:40:21 */
@Controller
@RequestMapping("uploadzip")
public class UploadZipController {@Resourceprivate EntryInfoService entryInfoService;private static final String UP_FRONT_FILE ="images/entry";private static final String UP_ZIP_FILE ="zip";/*** @Title: UploadZipController.java * @Package com.jingren.jing.educational.controller.uploadpic * @Description: TODO 导入图片弹窗* @author  MR.Lu   * @date 2017年1月6日 上午11:42:02 */@RequestMapping("/to_upload_zip.jr")public String to_upload_zip(){return "/educational/upload/uploadentryinfouserpic";}/*** @Title: UploadZipController.java * @Package com.jingren.jing.educational.controller.uploadpic * @Description: TODO 导入图片* @author  MR.Lu   * @date 2017年1月6日 下午1:25:26 */@RequestMapping("/upload_zip_pic.jr")public void upload_zip_pic(HttpSession session,HttpServletResponse response,HttpServletRequest request,@RequestParam(value="upfile",required=false) MultipartFile upfile){String filePath=request.getServletContext().getRealPath("/");String zippath=UploadAddress.getUploadDate(upfile, request, UP_ZIP_FILE);Map<String, Object> map=zipFileRead(request,request.getServletContext().getRealPath("")+zippath, filePath+UP_FRONT_FILE+"/");Integer suc=(Integer) map.get("suc");Integer fail=(Integer) map.get("fail");ResponseUtils.renderText(response, "<p style='color: orange;'>恭喜您导入成功,共计成功导入"+suc+"张,失败"+fail+"张,失败原因命名格式错误!</p>");System.out.println(zippath);DeleteFile.deleteFile1(zippath+"", request);//删除压缩包}/*** @Title: UploadZipController.java * @Package com.jingren.jing.educational.controller.uploadpic * @Description: TODO 读取压缩包里面的文件* @author 鲁晓飞 MR.Lu   * @date 2017年1月6日 下午2:29:36 * @version 网校+CRM系统 V1.0*/private  Map<String, Object> zipFileRead(HttpServletRequest request,String file,String saveRootDirectory) {int suc=0;int fail=0;try {ZipFile zipFile = new ZipFile(file);@SuppressWarnings("unchecked")Enumeration<ZipEntry> enu = (Enumeration<ZipEntry>) zipFile.entries();while (enu.hasMoreElements()) {ZipEntry zipElement = (ZipEntry) enu.nextElement();InputStream read = zipFile.getInputStream(zipElement);String fileName = zipElement.getName();if (fileName != null && fileName.indexOf(".") != -1) {//是否为文件 (文件带有路径如:/images/a.jpg)Map<String, Object> map_info= execute(request,zipElement,read,saveRootDirectory);suc+=(Integer) map_info.get("suc");fail+=(Integer) map_info.get("fail");}}zipFile.close();//必须关闭流文件否则无法删除临时zip包} catch (Exception e) {e.printStackTrace();}Map<String, Object> map=new HashMap<>();map.put("suc", suc);map.put("fail", fail);return map;}private  Map<String, Object> execute(HttpServletRequest request,ZipEntry ze, InputStream read,String saveRootDirectory)throws FileNotFoundException, IOException {DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");String format1 = df.format(new Date());Random t = new Random();for (int i = 0; i < 3; i++) {format1 += t.nextInt(10);}Integer fail=0;Integer suc=0;String prefix = ze.getName().substring(ze.getName().lastIndexOf("."));//如果只读取图片,自行判断就OK.//String fileName = ze.getName();
//          if(fileName.lastIndexOf(".jpg")!= -1 || fileName.lastIndexOf(".bmp")!= -1
//              || fileName.lastIndexOf(".jpeg")!= -1){//指定要解压出来的文件格式(这些格式可抽取放置在集合或String数组通过参数传递进来,方法更通用)File file = new File(saveRootDirectory + format1+prefix);//File afile = new File(Path + filePath + "/" + filename);/************************保存证件照片开始*******************************/EntryInfo entryInfo=new EntryInfo();String fileName = ze.getName().substring(0,ze.getName().lastIndexOf("."));String card_type=fileName.substring(0, 3);//获得图片类型String carnumber=fileName.substring(3,fileName.length());entryInfo.setDocumentNumber(carnumber);String addr = "/" + UP_FRONT_FILE + "/" + format1 + prefix;switch (card_type) {case "XLZ":entryInfo.setCertificatePic(addr);suc=1;break;case "SFZ":entryInfo.setUserCardPositive(addr);suc=1;break;case "SFB":entryInfo.setUserCardOpposite(addr);suc=1;break;case "XYP":entryInfo.setEntryUserPhoto(addr);suc=1;break;case "QTZ":entryInfo.setDocument_photo(addr);suc=1;break;default:fail=1;break;}/************************保存证件照片结束*******************************/if (!file.exists()) {File rootDirectoryFile = new File(file.getParent());//创建目录if (!rootDirectoryFile.exists()) {boolean ifSuccess = rootDirectoryFile.mkdirs();if (ifSuccess) {System.out.println("文件夹创建成功!");} else {System.out.println("文件创建失败!");}}//创建文件try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}//写入文件BufferedOutputStream write = new BufferedOutputStream(new FileOutputStream(file));int cha = 0;while ((cha = read.read()) != -1) {write.write(cha);}//要注意IO流关闭的先后顺序write.flush();write.close();read.close();
//          }try {entryInfoService.updateEntryInfoPic(entryInfo);suc=1;} catch (Exception e) {DeleteFile.deleteFile1(addr, request);fail=1;}Map<String, Object> map=new HashMap<>();map.put("suc", suc);map.put("fail", fail);return map;}
}

本功能实现了导入压缩包内的图片,并且将图片的名字按照命名规则进行匹配并且存入数据库

一个图片压缩并导入服务器的小功能相关推荐

  1. 用Kotlin撸一个图片压缩插件ImageSlimming-导学篇(一)

    简述: 很久没有发布Kotlin的实战相关的内容,这段时间在折腾Intellij IDEA的插件开发,折腾出了几个小插件,因为最近公司业务分离,原来堆在基础业务那边模块,都以模块的形式抽离出来,独立仓 ...

  2. 用Kotlin撸一个图片压缩插件-插件基础篇(二)

    简述: 前两天写了篇用Kotlin撸一个图片压缩插件-导学篇,现在迎来了插件基础篇,没错这篇文章就是教你如何一步一步从零开始写一个插件,包括插件项目构建,运行,调试到最后的上线发布整个流程.如果你是插 ...

  3. 用Kotlin撸一个图片压缩插件-实战篇(三)

    简述: 由于个人原因,已经有很长一段时间没有写过文章,有句话是那么说的只要开始就不会太晚,所以我们开始<用Kotlin撸一个图片压缩插件>系列文章最后一篇实战篇.实际上我已经把源码发布到了 ...

  4. php 图片压缩 保留exif,Android Bitmap小技巧 - 压缩时保留图片的Exif信息

    大家都知道,相机在照相时是会为照片生成Exif,里面包含有拍照时间.图片尺寸.旋转角度.GPS定位等信息,有时候,我们的APP在展示图片的时候需要获得并展现这些信息. 但是在android中,当我们将 ...

  5. Web安全 文件上传漏洞的 测试和利用.(上传一个图片或文件 拿下服务器最高权限.)

    文件上传漏洞的概括 现在大多的网站和Web应用系统都会有上传功能(比如:文档,图片,头像,视频上传等.),而程序员在开发文件上传功能时,没有对代码做严格校验上传文件的后缀和文件类型. 此时攻击者就可以 ...

  6. python图片压缩限定大小_Python练习小工具——照片压缩及自定义尺寸更改

    功能及使用如下: 1.点击按钮<选择照片路径>,在弹出的窗口中,按着ctrl多选照片,或者直接ctrl+a全选照片. 2.在界面的输入框中,输入要压缩的百分比数值或 修改尺寸数值(例如高1 ...

  7. 一个图片压缩 免费 api接口

    https://www.showapi.com/api/lookPoint/895

  8. Android学习之图片压缩,压缩程度高且失真度小

    曾经在做手机上传图片的时候.直接获取相机拍摄的原图上传,原图大小一般1~2M.因此上传一张都比較浪费资源,有些场景还须要图片多张上传,所以近期查看了好多前辈写的关于图片处理的资料.然后试着改了一个图片 ...

  9. (原创)介绍一个优秀的图片压缩库Compressor

    我们在做项目的时候,有时候需要在界面展示一张较大的图片 这时候我们应该想到两点 1图片是否能够缓存 2图片是否能够压缩 做到了缓存和压缩,才能尽可能低减少内存的负荷,增强app的流畅度 最近在了解这方 ...

最新文章

  1. C++:随笔5---this指针和类的继承
  2. php 图片 投稿 源码,php图片上传,审核,显示源码(转载)
  3. cocos2d-x一些核心概念介绍
  4. win10 安装microsoft.net framework3.5
  5. Day07-函数(2)
  6. html5距离底部的距离代码,如何使距离为HTML5
  7. MYSQL学习(一) - 数据结构
  8. 一文看尽Google I/O大会:AI打电话以假乱真,TPU 3.0正式发布
  9. 拓展卡尔曼滤波器(EKF)的数学推导
  10. Python:hashlib加密,flask模块写接口
  11. 以拯救之因 强制恢复导致ORA-600 4000错误案例
  12. HDU4578 Transformation(多标记线段树)题解
  13. IE、Firefox和 Chrome长时间打开后内存都会变很大。
  14. 《测绘综合能力》——地籍测绘
  15. 【JavaScript学习-红宝书】1.语言基础
  16. 彻底与流氓软件“2345好压全家桶”说再见【忠告:不要安装2345系列的任何东西--否则你会后悔的】
  17. 更改Windows桌面位置为D盘
  18. 站内搜寻引擎 php mysql_迅搜(xunsearch) - 开源免费中文全文搜索引擎|PHP全文检索|mysql全文检索|站内搜索...
  19. 植物大战僵尸(1):实现无限阳光
  20. gdb调试程序时跳进函数和跳出函数

热门文章

  1. 投资:SEC Form总结
  2. Unity,最炙手可热的游戏开发引擎
  3. SMC气动制图及气动元件配置工具
  4. 2022年武汉江汉区第二批区级科技企业孵化器和众创空间申报(附申报时间+要求+材料)
  5. 胆酸(Cholate)乳清白蛋白纳米粒Cholate-whey protein|保存条件
  6. Elsevier投稿
  7. 重磅直播|ORB-SLAM3经典单目初始化模块原理及实现
  8. 熔化极氩弧焊(MIG,MAG)
  9. ubantu18.04下安装teamviewer
  10. Swift中实现用户输入防抖动的两种方法