The article directories

  • preface
  • steps
    • In the code

preface

Recently wrote a upload compression package, the compression package of pictures to save the interface, so I turned to the online file stream operation blog, summed up a method without decompression, directly read the file

steps

In the code

@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();

			// Determine whether the file to be uploaded must be zip or RAR
			String[] filename = originalFilename.split("\ \.");
			if(! filename[1].equals("zip") && !filename[1].equals("rar")) {return;
			}

			When creating temporary files, the name must be longer than 2
			file = File.createTempFile(filename[0].length() <= 2 ? "Compressed package" + filename[0] : filename[0], filename[1]);
			multipartFile.transferTo(file);
			file.deleteOnExit();
			ZipFile zf = newZipFile(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);
							/// Call the upload file method here to upload the image
						}
					}
				}
			}
		} catch (Exception e) {
			logger.error("packageUpload_error:", e); }}Copy the code