Java Code Examples for org.springframework.web.multipart.support.StandardMultipartHttpServletRequest

The following examples show how to use org.springframework.web.multipart.support.StandardMultipartHttpServletRequest. These examples are extracted from open source projects. 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
/**
 * Create a new {@link MockMultipartHttpServletRequest} based on the
 * supplied {@code ServletContext} and the {@code MockMultipartFiles}
 * added to this builder.
 */
@Override
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {

	MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(servletContext);
	this.files.stream().forEach(request::addFile);
	this.parts.values().stream().flatMap(Collection::stream).forEach(request::addPart);

	if (!this.parts.isEmpty()) {
		new StandardMultipartHttpServletRequest(request)
				.getMultiFileMap().values().stream().flatMap(Collection::stream)
				.forEach(request::addFile);
	}

	return request;
}
 
Example 2
/**
 * Create a new {@link MockMultipartHttpServletRequest} based on the
 * supplied {@code ServletContext} and the {@code MockMultipartFiles}
 * added to this builder.
 */
@Override
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {

	MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(servletContext);
	this.files.stream().forEach(request::addFile);
	this.parts.values().stream().flatMap(Collection::stream).forEach(request::addPart);

	if (!this.parts.isEmpty()) {
		new StandardMultipartHttpServletRequest(request)
				.getMultiFileMap().values().stream().flatMap(Collection::stream)
				.forEach(request::addFile);
	}

	return request;
}
 
Example 3
Source Project: Aooms   Source File: ParamInterceptor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
    Map<String,String[]> paramMaps = request.getParameterMap();
    Map<String,Object> params = CollectionUtil.newHashMap();
    paramMaps.forEach((k,v) -> {
        params.put(k,convert(v));
    });

    // 文件上传
    if(request instanceof AbstractMultipartHttpServletRequest){
        //CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getServletContext());
        StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest)request;
        Map<String,MultipartFile> multipartFileMap = multipartHttpServletRequest.getFileMap();
        DataBoss.self().getPara().setFiles(multipartFileMap);
    }

    // 请求参数
    DataBoss.self().getPara().setData(params);
    // 路径参数
    DataBoss.self().getPara().setPathVars((Map)request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE));
    return true;
}
 
Example 4
Source Project: cloud-portal   Source File: UseCaseController.java    License: MIT License 6 votes vote down vote up
private void writeFilesAndAddToMap(HttpServletRequest request, Map<String, Object> variableMap, List<File> tempFileList) {
	
	// get multipart request
	StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;

	// get file map from request
	Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
	
	for (Entry<String, MultipartFile> fileMapEntry : fileMap.entrySet()) {

		// write file uploads to disk
		File file = writeMultipartFile(fileMapEntry.getValue());
		if (file != null) {
			
			// add to temp file list
			tempFileList.add(file);

			// add file paths to variable map
			variableMap.put(fileMapEntry.getKey(), file.getAbsolutePath());
		}
	}
}
 
Example 5
private Collection<Part> getWireMockParts() {
	MockHttpServletRequest request = MockMvcRequestBuilders
			.request(this.result.getMethod(), this.result.getUriTemplate())
			.contentType(this.result.getRequestHeaders().getContentType())
			.content(this.result.getRequestBodyContent())
			.buildRequest(new MockServletContext());
	try {
		return new StandardMultipartHttpServletRequest(request).getParts().stream()
				.map(part -> partFromServletPart(part)).collect(Collectors.toList());
	}
	catch (Exception e) {
		throw new IllegalStateException(e);
	}
}
 
Example 6
Source Project: paas   Source File: FileUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 上传文件,如果是SpringBoot,配置如下:
 * # 上传单个文件最大允许
 * spring.servlet.multipart.max-file-size=10MB
 * # 每次请求最大允许
 * spring.servlet.multipart.max-request-size=100MB
 *
 * @return 自定义消息
 * @author jitwxs
 * @version 创建时间:2018年4月17日 下午4:05:26
 */
public static String upload(HttpServletRequest request) throws Exception {
    StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;

    // 遍历普通参数(即formData的fileName和fileSize)
    Enumeration<String> names = req.getParameterNames();
    while (names.hasMoreElements()) {
        String key = names.nextElement();
        String val = req.getParameter(key);
        System.out.println("FormField:k=" + key + "v=" + val);
    }

    // 遍历文件参数(即formData的file)
    Iterator<String> iterator = req.getFileNames();
    if (iterator == null){
        return "未选择文件";
    }
    String result = "";
    while (iterator.hasNext()) {
        MultipartFile file = req.getFile(iterator.next());
        String fileNames = file.getOriginalFilename();
        // 文件名
        fileNames = new String(fileNames.getBytes("UTF-8"));
        //int split = fileNames.lastIndexOf(".");
        // 文件前缀
        //String fileName = fileNames.substring(0, split);
        // 文件后缀
        //String fileType = fileNames.substring(split + 1, fileNames.length());
        // 文件大小
        //Long fileSize = file.getSize();
        // 文件内容
        byte[] content = file.getBytes();

        File file1 = new File("D:\\test\\"+fileNames);

        FileUtils.writeByteArrayToFile(file1, content);

        FileOutputStream fos = new FileOutputStream(file1);
        fos.write(content);
        fos.flush();
        result = "D:\\test\\"+fileNames;
        System.out.println("write success");
    }
    return result;
}