@Bean(name = "multipartResolver") public CommonsMultipartResolver getResolver() throws IOException{ CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setMaxUploadSizePerFile(5242880); resolver.setMaxUploadSize(52428807); resolver.setDefaultEncoding("UTF-8"); return resolver; }
@Bean public MultipartResolver multipartResolver(WikiConfig wikiConfig) { final String UPLOAD_TEMP_DIR = wikiConfig.getUploadTempDir(); // CommonsMultipartResolver mr = new CommonsMultipartResolver(); mr.setMaxUploadSize(MAX_UPLOAD_SIZE); mr.setDefaultEncoding(DEFAULT_ENCODING); try { Resource uploadTempDir = resourceLoader .getResource(UPLOAD_TEMP_DIR); mr.setUploadTempDir(uploadTempDir); LOG.debug(String.format("UPLOAD TEMP DIR = %s", uploadTempDir)); } catch (IOException e) { LOG.error(String.format("#getResource(%s) FAIL, FALLBACK.", UPLOAD_TEMP_DIR), e); } return mr; }
/** * 压测文件上传 * * @param request * @param response * @return */ @RequestMapping(value = "/fileUpload") @ResponseBody public ComponentResult<FileUploadBO> fileUpload(HttpServletRequest request,HttpServletResponse response) { ComponentResult<FileUploadBO> componentResult = new ComponentResult<FileUploadBO>(); //1、获取上传参数 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); if (multipartResolver.isMultipart(request)) { MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator<String> iterator = multiRequest.getFileNames(); while (iterator.hasNext()) { MultipartFile file = multiRequest.getFile(iterator.next().toString()); if (null != file) { //2、上传 String filename = new SimpleDateFormat("yyyyMMddhhmmssSSS").format(new Date()) + ".xls"; try { File targetFile = new File(pressureDataFilepath + filename); file.transferTo(targetFile); //3、将本地文件上传到FTP boolean uploadResult = ftpUtils.uploadFile(targetFile); if(uploadResult){ componentResult.setData(new FileUploadBO(filename)); return ResultUtil.success(componentResult); }else{ logger.error("文件上传FTP失败,filename:{}",filename); return ResultUtil.fail(ErrorCode.FAIL,"文件上传FTP失败",componentResult); } } catch (IllegalStateException | IOException e) { logger.error("上传文件异常,filename->" + filename,e); } }else{ logger.error("压测文件上传,file为NULL"); } } }else{ logger.error("压测文件上传,request校验失败"); } //3、返回 return ResultUtil.fail(componentResult); }
/** 上传文件处理(支持批量) */ public static List<String> uploadFile(HttpServletRequest request) { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); List<String> fileNames = InstanceUtil.newArrayList(); if (multipartResolver.isMultipart(request)) { MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; String pathDir = getUploadDir(request); File dirFile = new File(pathDir); if (!dirFile.isDirectory()) { dirFile.mkdirs(); } for (Iterator<String> iterator = multiRequest.getFileNames(); iterator.hasNext();) { String key = iterator.next(); MultipartFile multipartFile = multiRequest.getFile(key); if (multipartFile != null) { String name = multipartFile.getOriginalFilename(); String uuid = UUID.randomUUID().toString(); String postFix = name.substring(name.lastIndexOf(".")).toLowerCase(); String fileName = uuid + postFix; String filePath = pathDir + File.separator + fileName; File file = new File(filePath); file.setWritable(true, false); try { multipartFile.transferTo(file); fileNames.add(fileName); } catch (Exception e) { logger.error(name + "保存失败", e); } } } } return fileNames; }
@Bean(name = "multipartResolver") public MultipartResolver multipartResolver() { CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setDefaultEncoding("UTF-8"); resolver.setMaxInMemorySize(40960); resolver.setMaxUploadSize(50 * 1024 * 1024);//上传文件大小 50M 50*1024*1024 return resolver; }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { // Check that we have a file upload request boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (!isMultipart) { return; } try { MultipartHttpServletRequest multiRequest = new CommonsMultipartResolver().resolveMultipart(request); MultiValueMap<String, MultipartFile> fileMap = multiRequest.getMultiFileMap(); BlobstoreService blobstoreService = AppFactory.get().getBlobstoreService(); Map<String, String> names = new HashMap<>(1); for (String fieldName : fileMap.keySet()) { MultipartFile file = fileMap.getFirst(fieldName); String fileName = file.getOriginalFilename(); String contentType = file.getContentType(); long sizeInBytes = file.getSize(); String blobKey = blobstoreService.store(fileName, contentType, sizeInBytes, file.getInputStream()); names.put(fieldName, blobKey); } request.getSession().setAttribute("blobs", names); RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(request.getParameter("ru")); dispatcher.forward(multiRequest, response); } catch (Exception ex) { _logger.severe("Upload failed", ex); } }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { // Check that we have a file upload request boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (!isMultipart) { return; } try { MultipartHttpServletRequest multiRequest = new CommonsMultipartResolver().resolveMultipart(request); MultiValueMap<String, MultipartFile> fileMap = multiRequest.getMultiFileMap(); BlobstoreService blobstoreService = AppFactory.get().getBlobstoreService(); Map<String, String> names = new HashMap<>(1); for (String fieldName : fileMap.keySet()) { MultipartFile file = fileMap.getFirst(fieldName); String fileName = file.getOriginalFilename(); String contentType = file.getContentType(); long sizeInBytes = file.getSize(); String blobKey = blobstoreService.store(fileName, contentType, sizeInBytes, file.getInputStream()); names.put(fieldName, blobKey); names.put("file", fileName); } response.getWriter().write(new Gson().toJson(names)); response.setStatus(HttpStatus.SC_CREATED); } catch (Exception ex) { _logger.severe("Upload failed", ex); } }
/** * 上传文件 * @param request * @return */ public File uploadFile(HttpServletRequest request) { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); try { if (multipartResolver.isMultipart(request)) { MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator<String> iterator = multiRequest.getFileNames(); while (iterator.hasNext()) { String key = iterator.next(); MultipartFile multipartFile = multiRequest.getFile(key); if (multipartFile != null) { String name = multipartFile.getOriginalFilename(); String pathDir = request.getSession().getServletContext().getRealPath("/upload/" + DateUtils.currentTime()); File dirFile = new File(pathDir); if (!dirFile.isDirectory()) { dirFile.mkdirs(); } String filePath = pathDir+File.separator+name; File file = new File(filePath); file.setWritable(true, false); multipartFile.transferTo(file); return file; } } } } catch (Exception e) { e.printStackTrace(); } return null; }
/** * 上传文件处理(支持批量) * @param request * @param pathDir 上传文件保存路径 * @return * @throws IllegalStateException * @throws IOException */ public static List<String> uploadFile(HttpServletRequest request,String pathDir) throws IllegalStateException, IOException { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); List<String> fileNames = InstanceUtil.newArrayList(); if (multipartResolver.isMultipart(request)) { MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator<String> iterator = multiRequest.getFileNames(); if(pathDir==null|| pathDir.equals("")){ pathDir = request.getSession().getServletContext().getRealPath(uploadFileDir + DateUtils.currentTime()); } File dirFile = new File(pathDir); if (!dirFile.isDirectory()) { dirFile.mkdirs(); } while (iterator.hasNext()) { String key = iterator.next(); MultipartFile multipartFile = multiRequest.getFile(key); if (multipartFile != null) { String uuid = UUID.randomUUID().toString().replace("-", ""); String name = multipartFile.getOriginalFilename(); int lastIndexOf = name.lastIndexOf("."); String postFix=""; if(lastIndexOf!=-1){ postFix = name.substring(lastIndexOf).toLowerCase(); } String fileName = uuid + postFix; String filePath = pathDir + File.separator + fileName; File file = new File(filePath); file.setWritable(true, false); multipartFile.transferTo(file); fileNames.add(file.getAbsolutePath()); } } } return fileNames; }
@Bean public CommonsMultipartResolver multipartResolver() { CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setDefaultEncoding("utf-8"); return resolver; }
@Bean public CommonsMultipartResolver multipartResolver() { final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); commonsMultipartResolver.setMaxUploadSize(-1); commonsMultipartResolver.setDefaultEncoding("UTF-8"); return commonsMultipartResolver; }
/** * 上传文件处理(支持批量) * @param request * @param pathDir 上传文件保存路径 * @return * @throws IllegalStateException * @throws IOException */ public static List<String> uploadFile(HttpServletRequest request,String pathDir) throws IllegalStateException, IOException { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); List<String> fileNames = new ArrayList<String>(); if (multipartResolver.isMultipart(request)) { MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator<String> iterator = multiRequest.getFileNames(); if(pathDir==null|| pathDir.equals("")){ pathDir = request.getSession().getServletContext().getRealPath(uploadFileDir + DateUtils.currentTime()); } File dirFile = new File(pathDir); if (!dirFile.isDirectory()) { dirFile.mkdirs(); } while (iterator.hasNext()) { String key = iterator.next(); MultipartFile multipartFile = multiRequest.getFile(key); if (multipartFile != null) { String uuid = UUID.randomUUID().toString().replace("-", ""); String name = multipartFile.getOriginalFilename(); int lastIndexOf = name.lastIndexOf("."); String postFix=""; if(lastIndexOf!=-1){ postFix = name.substring(lastIndexOf).toLowerCase(); } String fileName = uuid + postFix; String filePath = pathDir + File.separator + fileName; File file = new File(filePath); file.setWritable(true, false); multipartFile.transferTo(file); fileNames.add(file.getAbsolutePath()); } } } return fileNames; }
/** * 上传附件 * @param request * @return * @throws IllegalStateException * @throws IOException */ public String upload(HttpServletRequest request) throws IllegalStateException, IOException { ResourceBundle projectConfig = ResourceBundle.getBundle("config"); String filePath=projectConfig.getString("osframe.file.path"); //创建一个通用的多部分解析器 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); //判断 request 是否有文件上传,即多部分请求 if(multipartResolver.isMultipart(request)){ //转换成多部分request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request; //取得request中的所有文件名 Iterator<String> iter = multiRequest.getFileNames(); while(iter.hasNext()){ //记录上传过程起始时的时间,用来计算上传时间 int pre = (int) System.currentTimeMillis(); //取得上传文件 MultipartFile file = multiRequest.getFile(iter.next()); if(file != null){ //取得当前上传文件的文件名称 String myFileName = file.getOriginalFilename(); //如果名称不为“”,说明该文件存在,否则说明该文件不存在 if(myFileName.trim() !=""){ System.out.println(myFileName); //重命名上传后的文件名 String fileName = "demoUpload\\" + file.getOriginalFilename(); //定义上传路径 String path = filePath+ fileName;//"E:\\upload\\" File localFile = new File(path); file.transferTo(localFile); } } //记录上传该文件后的时间 int finaltime = (int) System.currentTimeMillis(); System.out.println(finaltime - pre); } } return "/success"; }
/** * 上传文件 * @param request * @param dir * @return * @throws IllegalStateException * @throws IOException */ public List<File> upload(File dir) throws IllegalStateException, IOException { List<File> result = new ArrayList<File>(); HttpServletRequest request = getRequest(); // 创建一个通用的多部分解析器 CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext()); // 判断 request 是否有文件上传,即多部分请求 if (multipartResolver.isMultipart(request)) { // 转换成多部分request MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; // 取得request中的所有文件名 Iterator<String> iter = multiRequest.getFileNames(); while (iter.hasNext()) { // 取得上传文件 MultipartFile file = multiRequest.getFile(iter.next()); if (file != null) { // 取得当前上传文件的文件名称 String fileName = file.getOriginalFilename(); // 如果名称不为"",说明该文件存在,否则说明该文件不存在 if (BasicUtil.isNotEmpty(fileName)) { // 重命名上传后的文件名 String sufName = FileUtil.getSuffixFileName(fileName); // 定义上传路径 File localFile = new File(dir,BasicUtil.getRandomLowerString(10)+"."+sufName); file.transferTo(localFile); result.add(localFile); } } } } return result; }
/** * 向当前运行的war中上传文件 * @param upPath * @param request * @param response * @return * @throws IllegalStateException * @throws IOException */ @RequestMapping("/upload") public String upload2(HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException { // @RequestParam String upPath , log.info("upPath:[{}][{}]", request.getParameter("upPath"), request.getAttribute("upPath")); String upPath = "/tpl/"; log.info("username[{}],upPath[{}]开始上传文件", SessionUtil.getUserInfo().getMember().getAccNo(), upPath); String rootPath = request.getSession().getServletContext().getRealPath("/"); log.debug("获取到项目根路径为rootPath[{}]", rootPath); ShiroHttpServletRequest shiroRequest = (ShiroHttpServletRequest) request; CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); MultipartHttpServletRequest multipartRequest = commonsMultipartResolver.resolveMultipart((HttpServletRequest) shiroRequest.getRequest()); Iterator<String> itr = multipartRequest.getFileNames(); MultipartFile file = null; while (itr.hasNext()) { file = multipartRequest.getFile(itr.next()); long pre = System.currentTimeMillis(); if (file != null) { if (StringUtils.isNotBlank(file.getOriginalFilename())) { String fileName = StringUtil.combinationString(upPath, "/", file.getOriginalFilename()); String path = rootPath + fileName; File localFile = new File(path); file.transferTo(localFile); } } long finaltime = System.currentTimeMillis(); log.info("上传文件耗时:{}" , (finaltime - pre)); } return "file/success"; }
@Bean public CommonsMultipartResolver multipartResolver() { final CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setDefaultEncoding("utf-8"); resolver.setMaxUploadSizePerFile(Long.valueOf(env.getRequiredProperty("web.max_upload_size_per_file"))); return resolver; }
@Bean public CommonsMultipartResolver commonsMultipartResolver(){ CommonsMultipartResolver re = new CommonsMultipartResolver(); re.setMaxUploadSize(1); re.setDefaultEncoding("utf-8"); return re ; }
@Bean(name = "multipartResolver") public CommonsMultipartResolver getMultipartResolver() { // TODO properties.driven config of multiPartConfig CommonsMultipartResolver multipart = new CommonsMultipartResolver(); multipart.setMaxUploadSizePerFile(1024 * 1024 * 5); multipart.setMaxUploadSize(1024 * 1024 * 20); return multipart; }
/** * Multipart resolver. * * @return the commons multipart resolver */ @Bean public CommonsMultipartResolver multipartResolver() { CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); commonsMultipartResolver.setDefaultEncoding("utf-8"); commonsMultipartResolver.setMaxUploadSize(50000000); return commonsMultipartResolver; }
@Bean public CommonsMultipartResolver multipartResolver(){ CommonsMultipartResolver muliMultipartResolver = new CommonsMultipartResolver(); muliMultipartResolver.setMaxUploadSize(MAX_UPLOAD_FILE_SIZE); muliMultipartResolver.setDefaultEncoding(DEFAULT_ENCODING); return muliMultipartResolver; }
@Bean public CommonsMultipartResolver filterMultipartResolver(){ CommonsMultipartResolver b = new CommonsMultipartResolver(); b.setMaxUploadSize( environment.getProperty( "springfield.multipart.size", Long.class, new Long(-1) ) ); return b; }
@Bean public CommonsMultipartResolver multipartResolver(){ CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); commonsMultipartResolver.setDefaultEncoding("utf-8"); commonsMultipartResolver.setMaxUploadSize(50000000); return commonsMultipartResolver; }
/** * Needed for the file upload of the mail attachments. */ @Bean public CommonsMultipartResolver multipartResolver() { CustomMultipartResolver multipart = new CustomMultipartResolver(); multipart.setMaxUploadSize(maxUploadFileSizeMB * 1024 * 1024); return multipart; }
public Request(HttpServletRequest servletRequest, ApplicationInstance application) throws IOException { if (logger.isDebugEnabled()) { logger.debug("Content-Type: " + servletRequest.getContentType() + ", charset=" + servletRequest.getCharacterEncoding()); } this.servletRequest = servletRequest; CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); if (multipartResolver.isMultipart(servletRequest)) { MultipartHttpServletRequest mpr = multipartResolver.resolveMultipart(servletRequest); this.servletRequest = mpr; body = mpr.getFileMap(); } else if (StringUtils.startsWithIgnoreCase(this.servletRequest.getContentType(), "application/json")) { byte[] bytes = IOUtils.toByteArray(this.servletRequest.getInputStream()); String charset = this.servletRequest.getCharacterEncoding(); if (StringUtils.isEmpty(charset)) { charset = application.getApplication().getCharsetEncoding(); } String s = new String(bytes, charset); ObjectMapper mapper = new ObjectMapper(); body = mapper.readValue(s, Object.class); } requestAttributeMap = new RequestAttributeMap(this.servletRequest); requestHeaderMap = new RequestHeaderMap(this.servletRequest); requestParameterMap = new RequestParameterMap(this.servletRequest); }
@Bean public CommonsMultipartResolver multipartResolver() { CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); commonsMultipartResolver.setDefaultEncoding(CharsetConstants.CHARSET_UTF8); commonsMultipartResolver.setMaxUploadSize(1024 * 1024 * 2); return commonsMultipartResolver; }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); MultipartHttpServletRequest httpServletRequest = multipartResolver.resolveMultipart(request); Map<String, MultipartFile> map = httpServletRequest.getFileMap(); MultipartFile multipartFile = map.values().iterator().next(); receivedFiles.add(multipartFile.getOriginalFilename()); }
@Bean public MultipartResolver multipartResolver () { final long DEFAULT_MAX_UPLOAD_SIZE = 1024L * 1024L * 200; // 200Mb //TODO: find a better way to bypass Spring controllers for OpenRefine CommonsMultipartResolver resolver = new CommonsMultipartResolver (){ @Override public boolean isMultipart(HttpServletRequest request){ if(request.getServletPath().startsWith ("/annotations")){ if(request.getServletPath().endsWith("/row/import") || request.getServletPath().endsWith("/column/import")) //annotation import is handled by AnnotationConroller return super.isMultipart (request); else //Let OpenRefine handle parsing of the multipart request return false; }else{ return super.isMultipart (request); } } }; // StandardServletMultipartResolver resolver = new StandardServletMultipartResolver (); resolver.setMaxUploadSize (environment.getProperty ("multipart.upload.max.size", Long.class, DEFAULT_MAX_UPLOAD_SIZE)); resolver.setResolveLazily (true); return resolver; }
@Bean public CommonsMultipartResolver multipartResolver() { final CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setMaxUploadSize(-1); resolver.setDefaultEncoding("utf-8"); configureMultipartResolver(resolver); return resolver; }
@Bean public MultipartResolver multipartResolver() { final CommonsMultipartResolver resolver = new CommonsMultipartResolver(); // Max upload size: 1 MB resolver.setMaxUploadSize(1048576); resolver.setDefaultEncoding(StandardCharsets.UTF_8.name()); resolver.setResolveLazily(true); return resolver; }
/** 上传文件处理(支持批量) */ public static List<FileInfo> uploadFiles(HttpServletRequest request) { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); List<FileInfo> fileNames = InstanceUtil.newArrayList(); if (multipartResolver.isMultipart(request)) { MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request; String pathDir = getUploadDir(request); File dirFile = new File(pathDir); if (!dirFile.isDirectory()) { dirFile.mkdirs(); } for (Iterator<String> iterator = multiRequest.getFileNames(); iterator.hasNext();) { String key = iterator.next(); MultipartFile multipartFile = multiRequest.getFile(key); if (multipartFile != null) { FileInfo fileInfo = new FileInfo(); String name = multipartFile.getOriginalFilename(); fileInfo.setOrgName(name); if (name.indexOf(".") == -1 && "blob".equals(name)) { name = name + ".png"; } String uuid = UUID.randomUUID().toString(); String postFix = name.substring(name.lastIndexOf(".")).toLowerCase(); String fileName = uuid + postFix; String filePath = pathDir + File.separator + fileName; File file = new File(filePath); file.setWritable(true, false); fileInfo.setFileSize(multipartFile.getSize()); try { multipartFile.transferTo(file); fileInfo.setFileName(fileName); fileNames.add(fileInfo); } catch (Exception e) { logger.error(name + "保存失败", e); } } } } return fileNames; }
/** 上传文件处理(支持批量) * @throws IOException * @throws IllegalStateException */ public static List<String> uploadImage(HttpServletRequest request) throws IllegalStateException, IOException { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); List<String> fileNames = InstanceUtil.newArrayList(); if (multipartResolver.isMultipart(request)) { MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator<String> iterator = multiRequest.getFileNames(); String pathDir = request.getSession().getServletContext() .getRealPath(uploadFileDir + DateUtils.currentTime()); File dirFile = new File(pathDir); if (!dirFile.isDirectory()) { dirFile.mkdirs(); } while (iterator.hasNext()) { String key = iterator.next(); MultipartFile multipartFile = multiRequest.getFile(key); if (multipartFile != null) { String name = multipartFile.getOriginalFilename(); // 默认文件格式为png图片 if (name.indexOf(".") == -1 && "blob".equals(name)) { name = name + ".png"; } String uuid = UUID.randomUUID().toString().replace("-", ""); int lastIndexOf = name.lastIndexOf("."); if(lastIndexOf==-1){ name=name + ".png"; } lastIndexOf = name.lastIndexOf("."); String postFix=name.substring(lastIndexOf).toLowerCase(); String fileName = uuid + postFix; String filePath = pathDir + File.separator + fileName; File file = new File(filePath); file.setWritable(true, false); multipartFile.transferTo(file); fileNames.add(file.getAbsolutePath()); } } } return fileNames; }
@Bean public MultipartResolver multipartResolver() { return new CommonsMultipartResolver(); }
@Bean(name = "multipartResolver") public MultipartResolver multipartResolver() throws IOException { CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setMaxUploadSize(MAX_UPLOAD_SIZE); return resolver; }
@Bean(name="filterMultipartResolver") public CommonsMultipartResolver getResolver() throws IOException { return new CommonsMultipartResolver(); }
/** 上传文件处理(支持批量) * @throws IOException * @throws IllegalStateException */ public static List<String> uploadImage(HttpServletRequest request) throws IllegalStateException, IOException { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( request.getSession().getServletContext()); List<String> fileNames = new ArrayList<String>(); if (multipartResolver.isMultipart(request)) { MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator<String> iterator = multiRequest.getFileNames(); String pathDir = request.getSession().getServletContext() .getRealPath(uploadFileDir + DateUtils.currentTime()); File dirFile = new File(pathDir); if (!dirFile.isDirectory()) { dirFile.mkdirs(); } while (iterator.hasNext()) { String key = iterator.next(); MultipartFile multipartFile = multiRequest.getFile(key); if (multipartFile != null) { String name = multipartFile.getOriginalFilename(); // 默认文件格式为png图片 if (name.indexOf(".") == -1 && "blob".equals(name)) { name = name + ".png"; } String uuid = UUID.randomUUID().toString().replace("-", ""); int lastIndexOf = name.lastIndexOf("."); if(lastIndexOf==-1){ name=name + ".png"; } lastIndexOf = name.lastIndexOf("."); String postFix=name.substring(lastIndexOf).toLowerCase(); String fileName = uuid + postFix; String filePath = pathDir + File.separator + fileName; File file = new File(filePath); file.setWritable(true, false); multipartFile.transferTo(file); fileNames.add(file.getAbsolutePath()); } } } return fileNames; }