org.springframework.web.multipart.commons.CommonsMultipartResolver Java Examples

The following examples show how to use org.springframework.web.multipart.commons.CommonsMultipartResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebConfig.java    From ssm-demo with MIT License 5 votes vote down vote up
@Bean(name="multipartResolver")
protected CommonsMultipartResolver MultipartResolver() {
	CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
	//multipartResolver.setUploadTempDir(new FileSystemResource("/tmp"));//可以不设置
	multipartResolver.setMaxUploadSize(2097152);//2M
	multipartResolver.setMaxInMemorySize(0);
	multipartResolver.setDefaultEncoding("UTF-8");
	return multipartResolver;
}
 
Example #2
Source File: AppConfig.java    From tutorials with MIT License 5 votes vote down vote up
/** Multipart file uploading configuratioin */
@Bean
public CommonsMultipartResolver multipartResolver() throws IOException {
    CommonsMultipartResolver resolver = new CommonsMultipartResolver();
    resolver.setMaxUploadSize(10000000);
    return resolver;
}
 
Example #3
Source File: DefaultAlfrescoMvcServletContextConfiguration.java    From alfresco-mvc with Apache License 2.0 5 votes vote down vote up
@Bean
public CommonsMultipartResolver multipartResolver() {
	final CommonsMultipartResolver resolver = new CommonsMultipartResolver();
	resolver.setMaxUploadSize(-1);
	resolver.setDefaultEncoding("utf-8");
	configureMultipartResolver(resolver);
	return resolver;
}
 
Example #4
Source File: FakeArtifactPublisherServlet.java    From gocd with Apache License 2.0 5 votes vote down vote up
@Override
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());
}
 
Example #5
Source File: MvcAutoConfiguration.java    From MaxKey with Apache License 2.0 5 votes vote down vote up
/**
 * upload file support .
 * @return multipartResolver
 */
@Bean (name = "multipartResolver")
public CommonsMultipartResolver commonsMultipartResolver(
        @Value("${spring.servlet.multipart.max-file-size:0}") int maxUploadSize)  {
    _logger.debug("maxUploadSize " + maxUploadSize);
    CommonsMultipartResolver multipartResolver = 
            new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(maxUploadSize);
    return multipartResolver;
}
 
Example #6
Source File: CommonBeanConfiguration.java    From Insights with Apache License 2.0 5 votes vote down vote up
/**
 * used for initialization of MultipartResolver ,MultipartResolver check files
 * in our application it apply before initialization.
 * It restirct any unwanted file attact in our application.
 * 
 * @return
 */
@Bean(name = "filterMultipartResolver")
public CommonsMultipartResolver multipartResolver() {
	CommonsMultipartResolver resolver = new CommonsMultipartResolver();
	resolver.setDefaultEncoding("utf-8");
	resolver.setMaxUploadSize(2197152);
	return resolver;
}
 
Example #7
Source File: WebMvcConfiguration.java    From DouBiNovel with Apache License 2.0 5 votes vote down vote up
@Bean(name = "multipartResolver")
public CommonsMultipartResolver getCommonsMultipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(20971520);
    multipartResolver.setMaxInMemorySize(1048576);
    return multipartResolver;
}
 
Example #8
Source File: UploadUtil.java    From mumu with Apache License 2.0 5 votes vote down vote up
/**
    * 上传文件处理(支持批量)
    * @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;
}
 
Example #9
Source File: DocUploadController.java    From mumu with Apache License 2.0 5 votes vote down vote up
/**
 * 上传文件
 * @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;
}
 
Example #10
Source File: SpringDispatcherConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
@Bean(name = "multipartResolver")
public CommonsMultipartResolver getResolver() throws IOException{
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
  
        resolver.setMaxUploadSizePerFile(5242880);
        resolver.setMaxUploadSize(52428807);
        resolver.setDefaultEncoding("UTF-8");
        return resolver;
    }
 
Example #11
Source File: WxMaMediaController.java    From sdb-mall with Apache License 2.0 5 votes vote down vote up
/**
 * 上传临时素材
 *
 * @return 素材的media_id列表,实际上如果有的话,只会有一个
 */
@PostMapping("/upload")
public List<String> uploadMedia(HttpServletRequest request) throws WxErrorException {
    CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());

    if (!resolver.isMultipart(request)) {
        return Lists.newArrayList();
    }

    MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
    Iterator<String> it = multiRequest.getFileNames();
    List<String> result = Lists.newArrayList();
    while (it.hasNext()) {
        try {
            MultipartFile file = multiRequest.getFile(it.next());
            File newFile = new File(Files.createTempDir(), file.getOriginalFilename());
            this.logger.info("filePath is :" + newFile.toString());
            file.transferTo(newFile);
            WxMediaUploadResult uploadResult = this.service.getMediaService().uploadMedia(WxMaConstants.KefuMsgType.IMAGE, newFile);
            this.logger.info("media_id : " + uploadResult.getMediaId());
            result.add(uploadResult.getMediaId());
        } catch (IOException e) {
            this.logger.error(e.getMessage(), e);
        }
    }

    return result;
}
 
Example #12
Source File: SpringDispatcherConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
@Bean(name = "multipartResolver")
public CommonsMultipartResolver getResolver() throws IOException{
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
  
        resolver.setMaxUploadSizePerFile(5242880);
        resolver.setMaxUploadSize(52428807);
        resolver.setDefaultEncoding("UTF-8");
        return resolver;
    }
 
Example #13
Source File: SpringRootConfig.java    From AuTe-Framework with Apache License 2.0 5 votes vote down vote up
@Bean
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
    commonsMultipartResolver.setMaxUploadSize(50 * 1024 * 1024);
    commonsMultipartResolver.setMaxInMemorySize(1024 * 1024);
    return commonsMultipartResolver;
}
 
Example #14
Source File: FileService.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
private UploadChunkParam parseUploadChunkRequest(HttpServletRequest request) throws GovernanceException {
    UploadChunkParam chunkParam = new UploadChunkParam();
    String fileId = request.getParameter("identifier");
    FileChunksMeta fileChunksMeta = this.fileChunksMap.get(fileId).getKey();

    chunkParam.setFileChunksMeta(fileChunksMeta);
    chunkParam.setBrokerId(Integer.parseInt(request.getParameter("brokerId")));
    chunkParam.setChunkNumber(Integer.parseInt(request.getParameter("chunkNumber")) - 1);
    chunkParam.setFileId(fileId);

    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
    if (multipartResolver.isMultipart(request)) {
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        Iterator<String> iter = multiRequest.getFileNames();
        while (iter.hasNext()) {
            MultipartFile file = multiRequest.getFile(iter.next());
            if (!Objects.isNull(file)) {
                try {
                    chunkParam.setChunkData(file.getBytes());
                } catch (IOException e) {
                    log.error("parse upload chunk data error.", e);
                    throw new GovernanceException(ErrorCode.PARSE_CHUNK_REQUEST_ERROR);
                }
            }
        }
    }
    return chunkParam;
}
 
Example #15
Source File: MultipartResolverConfig.java    From seppb with MIT License 5 votes vote down vote up
@Bean
public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
    commonsMultipartResolver.setDefaultEncoding(ENCODING);
    commonsMultipartResolver.setMaxUploadSize(MAX_UPLOAD_SIZE);
    commonsMultipartResolver.setMaxInMemorySize(MAX_IN_MEMORY_SIZE);
    return commonsMultipartResolver;
}
 
Example #16
Source File: AnylineController.java    From anyline with Apache License 2.0 5 votes vote down vote up
/** 
 * 上传文件 
 * @param dir  dir
 * @return return
 * @throws IllegalStateException IllegalStateException
 * @throws IOException  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; 
}
 
Example #17
Source File: RequestPartIntegrationTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Bean
public MultipartResolver multipartResolver() {
	return new CommonsMultipartResolver();
}
 
Example #18
Source File: DefaultAlfrescoMvcServletContextConfiguration.java    From alfresco-mvc with Apache License 2.0 4 votes vote down vote up
private void configureMultipartResolver(final CommonsMultipartResolver resolver) {
}
 
Example #19
Source File: CKImageUploadUtil.java    From mogu_blog_v2 with Apache License 2.0 4 votes vote down vote up
/**
 * 图片上传
 *
 * @param request
 * @param DirectoryName 文件上传目录:比如upload(无需带前面的/) upload/news ..
 * @return
 * @throws IllegalStateException
 * @throws IOException
 * @Title upload
 */
public static String upload(HttpServletRequest request, String DirectoryName) throws IllegalStateException,
        IOException {
    // 创建一个通用的多部分解析器
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession()
            .getServletContext());
    // 图片名称
    String fileName = null;
    // 判断 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() != "") {
                    // 获得图片的原始名称
                    String originalFilename = file.getOriginalFilename();
                    // 获得图片后缀名称,如果后缀不为图片格式,则不上传
                    String suffix = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
                    if (!fileTypes.contains(suffix)) {
                        continue;
                    }
                    // 获得上传路径的绝对路径地址(/upload)-->
                    String realPath = basePath + DirectoryName;
                    System.out.println(realPath);
                    // 如果路径不存在,则创建该路径
                    File realPathDirectory = new File(realPath);
                    if (realPathDirectory == null || !realPathDirectory.exists()) {
                        realPathDirectory.mkdirs();
                    }
                    // 重命名上传后的文件名 111112323.jpg
                    fileName = UUID.randomUUID() + suffix;
                    // 定义上传路径 .../upload/111112323.jpg
                    File uploadFile = new File(realPathDirectory + "\\" + fileName);
                    System.out.println(uploadFile);
                    file.transferTo(uploadFile);
                }
            }
            // 记录上传该文件后的时间
            // int finaltime = (int) System.currentTimeMillis();
            // System.out.println(finaltime - pre);
        }
    }
    return fileName;
}
 
Example #20
Source File: MvcConfiguration.java    From alf.io with GNU General Public License v3.0 4 votes vote down vote up
@Bean
public CommonsMultipartResolver multipartResolver() {
    return new CommonsMultipartResolver();
}
 
Example #21
Source File: FileTransfertConfig.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Bean
public CommonsMultipartResolver multipartResolver() {
	return new CommonsMultipartResolver();
}
 
Example #22
Source File: WebAdminConfiguration.java    From wallride with Apache License 2.0 4 votes vote down vote up
@Bean
public MultipartResolver multipartResolver() {
	return new CommonsMultipartResolver();
}
 
Example #23
Source File: WallRideWebMvcConfiguration.java    From wallride with Apache License 2.0 4 votes vote down vote up
@Bean
public MultipartResolver multipartResolver() {
	return new CommonsMultipartResolver();
}
 
Example #24
Source File: WebMvcConfig.java    From gerbil with GNU Affero General Public License v3.0 4 votes vote down vote up
@Bean
public CommonsMultipartResolver multipartResolver() {
    return new CommonsMultipartResolver();
}
 
Example #25
Source File: RequestPartIntegrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Bean
public MultipartResolver multipartResolver() {
	return new CommonsMultipartResolver();
}
 
Example #26
Source File: MessageConfig.java    From Insights with Apache License 2.0 4 votes vote down vote up
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
	CommonsMultipartResolver resolver = new CommonsMultipartResolver();
	resolver.setDefaultEncoding("utf-8");
	return resolver;
}
 
Example #27
Source File: RequestPartIntegrationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Bean
public MultipartResolver multipartResolver() {
	return new CommonsMultipartResolver();
}
 
Example #28
Source File: UploadUtil.java    From mumu with Apache License 2.0 4 votes vote down vote up
/** 上传文件处理(支持批量)
 * @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;
}