本程序的作用是读取上传压缩包中的图片并保存,以下是接口代码:
0 i% j1 N4 s% B9 F d
- @RequestMapping(value = "packageUpload")
- public void packageUpload(HttpServletRequest request,
- HttpServletResponse response) {
- File file = null;
- try {
- MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
- MultipartFile multipartFile = multipartRequest.getFile("file");
-
- if (null == multipartFile){
- return;
- }
- String originalFilename = multipartFile.getOriginalFilename();
-
- //判断上传文件必须是zip或者是rar否则不允许上传
- String[] filename = originalFilename.split("\\.");
- if (!filename[1].equals("zip") && !filename[1].equals("rar")){
- return;
- }
-
- //创建临时文件时,名称长度必须大于2
- file = File.createTempFile(filename[0].length() <= 2 ? "压缩包" + filename[0] : filename[0], filename[1]);
- multipartFile.transferTo(file);
- file.deleteOnExit();
- ZipFile zf = new ZipFile(file);
- Enumeration<?> zipEnum = zf.entries();
- ZipEntry ze;
- while (zipEnum.hasMoreElements()) {
- ze = (ZipEntry) zipEnum.nextElement();
- if (!ze.isDirectory()) {
- long size = ze.getSize();
- if (size > 0) {
- String[] name = ze.getName().split("\\.");
- String emojiName = name[0];
- String type = name[1];
-
- if (type.equals("jpg") || type.equals("png") || type.equals("gif") || type.equals("jpeg")){
- String[] split = emojiName.split("/");
- String picName = split[1] + "." + type;
-
- InputStream is = zf.getInputStream(ze);
- logger.info("picName:" + picName + ",size:" + size + ",inputStream:" + is);
- ///到这里调用上传文件的方法上传这个图片就可以了
- }
- }
- }
- }
- } catch (Exception e) {
- logger.error("packageUpload_error:" , e);
- }
- }
, C- e6 o, O. C+ D. |2 h# l- [. j9 z; |& c) x
|