Java tutorial
/** * Copyright (c) 2005-2012 https://github.com/zhangkaitao * * Licensed under the Apache License, Version 2.0 (the "License"); */ package cn.guoyukun.spring.web.controller; import cn.guoyukun.spring.jpa.Constants; import cn.guoyukun.spring.utils.ImagesUtils; import cn.guoyukun.spring.utils.LogUtils; import cn.guoyukun.spring.utils.MessageUtils; import cn.guoyukun.spring.web.entity.AjaxUploadResponse; import cn.guoyukun.spring.web.upload.FileUploadUtils; import cn.guoyukun.spring.web.upload.exception.FileNameLengthLimitExceededException; import cn.guoyukun.spring.web.upload.exception.InvalidExtensionException; import org.apache.commons.fileupload.FileUploadBase; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.net.URLDecoder; import java.net.URLEncoder; /** * ajax/ * <p>User: * <p>Date: 13-2-11 ?8:46 * <p>Version: 1.0 */ @Controller public class AjaxUploadController { //? ?? private long maxSize = FileUploadUtils.DEFAULT_MAX_SIZE; //? private String[] allowedExtension = FileUploadUtils.DEFAULT_ALLOWED_EXTENSION; // private String baseDir = FileUploadUtils.getDefaultBaseDir(); /** * @param request * @param files * @return */ @RequestMapping(value = "ajaxUpload", method = RequestMethod.POST) @ResponseBody public AjaxUploadResponse ajaxUpload(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "files[]", required = false) MultipartFile[] files) { //The file upload plugin makes use of an Iframe Transport module for browsers like Microsoft Internet Explorer and Opera, which do not yet support XMLHTTPRequest file uploads. response.setContentType("text/plain"); AjaxUploadResponse ajaxUploadResponse = new AjaxUploadResponse(); if (ArrayUtils.isEmpty(files)) { return ajaxUploadResponse; } for (MultipartFile file : files) { String filename = file.getOriginalFilename(); long size = file.getSize(); try { String url = FileUploadUtils.upload(request, baseDir, file, allowedExtension, maxSize, true); String deleteURL = "/ajaxUpload/delete?filename=" + URLEncoder.encode(url, Constants.ENCODING); if (ImagesUtils.isImage(filename)) { ajaxUploadResponse.add(filename, size, url, url, deleteURL); } else { ajaxUploadResponse.add(filename, size, url, deleteURL); } continue; } catch (IOException e) { LogUtils.logError("file upload error", e); ajaxUploadResponse.add(filename, size, MessageUtils.message("upload.server.error")); continue; } catch (InvalidExtensionException e) { ajaxUploadResponse.add(filename, size, MessageUtils.message("upload.not.allow.extension")); continue; } catch (FileUploadBase.FileSizeLimitExceededException e) { ajaxUploadResponse.add(filename, size, MessageUtils.message("upload.exceed.maxSize")); continue; } catch (FileNameLengthLimitExceededException e) { ajaxUploadResponse.add(filename, size, MessageUtils.message("upload.filename.exceed.length")); continue; } } return ajaxUploadResponse; } @RequestMapping(value = "ajaxUpload/delete", method = RequestMethod.POST) @ResponseBody public String ajaxUploadDelete(HttpServletRequest request, @RequestParam(value = "filename") String filename) throws Exception { if (StringUtils.isEmpty(filename) || filename.contains("\\.\\.")) { return ""; } filename = URLDecoder.decode(filename, Constants.ENCODING); String filePath = FileUploadUtils.extractUploadDir(request) + "/" + filename; File file = new File(filePath); file.deleteOnExit(); return ""; } }