org.apache.commons.fileupload.RequestContext Java Examples

The following examples show how to use org.apache.commons.fileupload.RequestContext. 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: UploadUtil.java    From mumu with Apache License 2.0 6 votes vote down vote up
/** 获取所有文本域 */
public static final List<?> getFileItemList(HttpServletRequest request, File saveDir) throws FileUploadException {
	if (!saveDir.isDirectory()) {
		saveDir.mkdir();
	}
	List<?> fileItems = null;
	RequestContext requestContext = new ServletRequestContext(request);
	if (FileUpload.isMultipartContent(requestContext)) {
		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setRepository(saveDir);
		factory.setSizeThreshold(fileSizeThreshold);
		ServletFileUpload upload = new ServletFileUpload(factory);
		fileItems = upload.parseRequest(request);
	}
	return fileItems;
}
 
Example #2
Source File: MultipartTestCases.java    From nio-multipart with Apache License 2.0 6 votes vote down vote up
public RequestContext getRequestContext(){
    return new RequestContext() {
        @Override
        public String getCharacterEncoding() {
            return charEncoding;
        }

        @Override
        public String getContentType() {
            return contentType;
        }

        @Override
        public int getContentLength() {
            return body.length;
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return getBodyInputStream();
        }
    };
}
 
Example #3
Source File: FormHttpMessageConverterTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void writeMultipart() throws Exception {
	MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
	parts.add("name 1", "value 1");
	parts.add("name 2", "value 2+1");
	parts.add("name 2", "value 2+2");
	parts.add("name 3", null);

	Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
	parts.add("logo", logo);

	// SPR-12108
	Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {
		@Override
		public String getFilename() {
			return "Hall\u00F6le.jpg";
		}
	};
	parts.add("utf8", utf8);

	Source xml = new StreamSource(new StringReader("<root><child/></root>"));
	HttpHeaders entityHeaders = new HttpHeaders();
	entityHeaders.setContentType(MediaType.TEXT_XML);
	HttpEntity<Source> entity = new HttpEntity<>(xml, entityHeaders);
	parts.add("xml", entity);

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage);

	final MediaType contentType = outputMessage.getHeaders().getContentType();
	// SPR-17030
	assertThat(contentType.getParameters().keySet(), Matchers.contains("charset", "boundary"));

	// see if Commons FileUpload can read what we wrote
	FileItemFactory fileItemFactory = new DiskFileItemFactory();
	FileUpload fileUpload = new FileUpload(fileItemFactory);
	RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
	List<FileItem> items = fileUpload.parseRequest(requestContext);
	assertEquals(6, items.size());
	FileItem item = items.get(0);
	assertTrue(item.isFormField());
	assertEquals("name 1", item.getFieldName());
	assertEquals("value 1", item.getString());

	item = items.get(1);
	assertTrue(item.isFormField());
	assertEquals("name 2", item.getFieldName());
	assertEquals("value 2+1", item.getString());

	item = items.get(2);
	assertTrue(item.isFormField());
	assertEquals("name 2", item.getFieldName());
	assertEquals("value 2+2", item.getString());

	item = items.get(3);
	assertFalse(item.isFormField());
	assertEquals("logo", item.getFieldName());
	assertEquals("logo.jpg", item.getName());
	assertEquals("image/jpeg", item.getContentType());
	assertEquals(logo.getFile().length(), item.getSize());

	item = items.get(4);
	assertFalse(item.isFormField());
	assertEquals("utf8", item.getFieldName());
	assertEquals("Hall\u00F6le.jpg", item.getName());
	assertEquals("image/jpeg", item.getContentType());
	assertEquals(logo.getFile().length(), item.getSize());

	item = items.get(5);
	assertEquals("xml", item.getFieldName());
	assertEquals("text/xml", item.getContentType());
	verify(outputMessage.getBody(), never()).close();
}
 
Example #4
Source File: FormHttpMessageConverterTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void writeMultipartOrder() throws Exception {
	MyBean myBean = new MyBean();
	myBean.setString("foo");

	MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
	parts.add("part1", myBean);

	HttpHeaders entityHeaders = new HttpHeaders();
	entityHeaders.setContentType(MediaType.TEXT_XML);
	HttpEntity<MyBean> entity = new HttpEntity<>(myBean, entityHeaders);
	parts.add("part2", entity);

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	this.converter.setMultipartCharset(StandardCharsets.UTF_8);
	this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage);

	final MediaType contentType = outputMessage.getHeaders().getContentType();
	assertNotNull("No boundary found", contentType.getParameter("boundary"));

	// see if Commons FileUpload can read what we wrote
	FileItemFactory fileItemFactory = new DiskFileItemFactory();
	FileUpload fileUpload = new FileUpload(fileItemFactory);
	RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
	List<FileItem> items = fileUpload.parseRequest(requestContext);
	assertEquals(2, items.size());

	FileItem item = items.get(0);
	assertTrue(item.isFormField());
	assertEquals("part1", item.getFieldName());
	assertEquals("{\"string\":\"foo\"}", item.getString());

	item = items.get(1);
	assertTrue(item.isFormField());
	assertEquals("part2", item.getFieldName());

	// With developer builds we get: <MyBean><string>foo</string></MyBean>
	// But on CI server we get: <MyBean xmlns=""><string>foo</string></MyBean>
	// So... we make a compromise:
	assertThat(item.getString(),
			allOf(startsWith("<MyBean"), endsWith("><string>foo</string></MyBean>")));
}
 
Example #5
Source File: FormHttpMessageConverterTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void writeMultipart() throws Exception {
	MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
	parts.add("name 1", "value 1");
	parts.add("name 2", "value 2+1");
	parts.add("name 2", "value 2+2");
	parts.add("name 3", null);

	Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
	parts.add("logo", logo);

	// SPR-12108
	Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {
		@Override
		public String getFilename() {
			return "Hall\u00F6le.jpg";
		}
	};
	parts.add("utf8", utf8);

	Source xml = new StreamSource(new StringReader("<root><child/></root>"));
	HttpHeaders entityHeaders = new HttpHeaders();
	entityHeaders.setContentType(MediaType.TEXT_XML);
	HttpEntity<Source> entity = new HttpEntity<>(xml, entityHeaders);
	parts.add("xml", entity);

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage);

	final MediaType contentType = outputMessage.getHeaders().getContentType();
	// SPR-17030
	assertThat(contentType.getParameters().keySet(), Matchers.contains("charset", "boundary"));

	// see if Commons FileUpload can read what we wrote
	FileItemFactory fileItemFactory = new DiskFileItemFactory();
	FileUpload fileUpload = new FileUpload(fileItemFactory);
	RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
	List<FileItem> items = fileUpload.parseRequest(requestContext);
	assertEquals(6, items.size());
	FileItem item = items.get(0);
	assertTrue(item.isFormField());
	assertEquals("name 1", item.getFieldName());
	assertEquals("value 1", item.getString());

	item = items.get(1);
	assertTrue(item.isFormField());
	assertEquals("name 2", item.getFieldName());
	assertEquals("value 2+1", item.getString());

	item = items.get(2);
	assertTrue(item.isFormField());
	assertEquals("name 2", item.getFieldName());
	assertEquals("value 2+2", item.getString());

	item = items.get(3);
	assertFalse(item.isFormField());
	assertEquals("logo", item.getFieldName());
	assertEquals("logo.jpg", item.getName());
	assertEquals("image/jpeg", item.getContentType());
	assertEquals(logo.getFile().length(), item.getSize());

	item = items.get(4);
	assertFalse(item.isFormField());
	assertEquals("utf8", item.getFieldName());
	assertEquals("Hall\u00F6le.jpg", item.getName());
	assertEquals("image/jpeg", item.getContentType());
	assertEquals(logo.getFile().length(), item.getSize());

	item = items.get(5);
	assertEquals("xml", item.getFieldName());
	assertEquals("text/xml", item.getContentType());
	verify(outputMessage.getBody(), never()).close();
}
 
Example #6
Source File: FormHttpMessageConverterTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void writeMultipartOrder() throws Exception {
	MyBean myBean = new MyBean();
	myBean.setString("foo");

	MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
	parts.add("part1", myBean);

	HttpHeaders entityHeaders = new HttpHeaders();
	entityHeaders.setContentType(MediaType.TEXT_XML);
	HttpEntity<MyBean> entity = new HttpEntity<>(myBean, entityHeaders);
	parts.add("part2", entity);

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	this.converter.setMultipartCharset(StandardCharsets.UTF_8);
	this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage);

	final MediaType contentType = outputMessage.getHeaders().getContentType();
	assertNotNull("No boundary found", contentType.getParameter("boundary"));

	// see if Commons FileUpload can read what we wrote
	FileItemFactory fileItemFactory = new DiskFileItemFactory();
	FileUpload fileUpload = new FileUpload(fileItemFactory);
	RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
	List<FileItem> items = fileUpload.parseRequest(requestContext);
	assertEquals(2, items.size());

	FileItem item = items.get(0);
	assertTrue(item.isFormField());
	assertEquals("part1", item.getFieldName());
	assertEquals("{\"string\":\"foo\"}", item.getString());

	item = items.get(1);
	assertTrue(item.isFormField());
	assertEquals("part2", item.getFieldName());

	// With developer builds we get: <MyBean><string>foo</string></MyBean>
	// But on CI server we get: <MyBean xmlns=""><string>foo</string></MyBean>
	// So... we make a compromise:
	assertThat(item.getString(),
			allOf(startsWith("<MyBean"), endsWith("><string>foo</string></MyBean>")));
}
 
Example #7
Source File: FormHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void writeMultipart() throws Exception {
	MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
	parts.add("name 1", "value 1");
	parts.add("name 2", "value 2+1");
	parts.add("name 2", "value 2+2");
	parts.add("name 3", null);

	Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
	parts.add("logo", logo);

	// SPR-12108
	Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {
		@Override
		public String getFilename() {
			return "Hall\u00F6le.jpg";
		}
	};
	parts.add("utf8", utf8);

	Source xml = new StreamSource(new StringReader("<root><child/></root>"));
	HttpHeaders entityHeaders = new HttpHeaders();
	entityHeaders.setContentType(MediaType.TEXT_XML);
	HttpEntity<Source> entity = new HttpEntity<Source>(xml, entityHeaders);
	parts.add("xml", entity);

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	this.converter.setMultipartCharset(UTF_8);
	this.converter.write(parts, new MediaType("multipart", "form-data", UTF_8), outputMessage);

	final MediaType contentType = outputMessage.getHeaders().getContentType();
	assertNotNull("No boundary found", contentType.getParameter("boundary"));

	// see if Commons FileUpload can read what we wrote
	FileItemFactory fileItemFactory = new DiskFileItemFactory();
	FileUpload fileUpload = new FileUpload(fileItemFactory);
	RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
	List<FileItem> items = fileUpload.parseRequest(requestContext);
	assertEquals(6, items.size());
	FileItem item = items.get(0);
	assertTrue(item.isFormField());
	assertEquals("name 1", item.getFieldName());
	assertEquals("value 1", item.getString());

	item = items.get(1);
	assertTrue(item.isFormField());
	assertEquals("name 2", item.getFieldName());
	assertEquals("value 2+1", item.getString());

	item = items.get(2);
	assertTrue(item.isFormField());
	assertEquals("name 2", item.getFieldName());
	assertEquals("value 2+2", item.getString());

	item = items.get(3);
	assertFalse(item.isFormField());
	assertEquals("logo", item.getFieldName());
	assertEquals("logo.jpg", item.getName());
	assertEquals("image/jpeg", item.getContentType());
	assertEquals(logo.getFile().length(), item.getSize());

	item = items.get(4);
	assertFalse(item.isFormField());
	assertEquals("utf8", item.getFieldName());
	assertEquals("Hall\u00F6le.jpg", item.getName());
	assertEquals("image/jpeg", item.getContentType());
	assertEquals(logo.getFile().length(), item.getSize());

	item = items.get(5);
	assertEquals("xml", item.getFieldName());
	assertEquals("text/xml", item.getContentType());
	verify(outputMessage.getBody(), never()).close();
}
 
Example #8
Source File: FormHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void writeMultipartOrder() throws Exception {
	MyBean myBean = new MyBean();
	myBean.setString("foo");

	MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
	parts.add("part1", myBean);

	HttpHeaders entityHeaders = new HttpHeaders();
	entityHeaders.setContentType(MediaType.TEXT_XML);
	HttpEntity<MyBean> entity = new HttpEntity<MyBean>(myBean, entityHeaders);
	parts.add("part2", entity);

	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	this.converter.setMultipartCharset(UTF_8);
	this.converter.write(parts, new MediaType("multipart", "form-data", UTF_8), outputMessage);

	final MediaType contentType = outputMessage.getHeaders().getContentType();
	assertNotNull("No boundary found", contentType.getParameter("boundary"));

	// see if Commons FileUpload can read what we wrote
	FileItemFactory fileItemFactory = new DiskFileItemFactory();
	FileUpload fileUpload = new FileUpload(fileItemFactory);
	RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
	List<FileItem> items = fileUpload.parseRequest(requestContext);
	assertEquals(2, items.size());

	FileItem item = items.get(0);
	assertTrue(item.isFormField());
	assertEquals("part1", item.getFieldName());
	assertEquals("{\"string\":\"foo\"}", item.getString());

	item = items.get(1);
	assertTrue(item.isFormField());
	assertEquals("part2", item.getFieldName());

	// With developer builds we get: <MyBean><string>foo</string></MyBean>
	// But on CI server we get: <MyBean xmlns=""><string>foo</string></MyBean>
	// So... we make a compromise:
	assertThat(item.getString(),
			allOf(startsWith("<MyBean"), endsWith("><string>foo</string></MyBean>")));
}