Java Code Examples for org.apache.commons.fileupload.FileUpload#parseRequest()
The following examples show how to use
org.apache.commons.fileupload.FileUpload#parseRequest() .
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: ClassifyUtils.java From ontopia with Apache License 2.0 | 5 votes |
public static ClassifiableContentIF getFileUploadContent(HttpServletRequest request) { // Handle file upload String contentType = request.getHeader("content-type"); // out.write("CT: " + contentType + " " + tm + " " + id); if (contentType != null && contentType.startsWith("multipart/form-data")) { try { FileUpload upload = new FileUpload(new DefaultFileItemFactory()); for (FileItem item : upload.parseRequest(request)) { if (item.getSize() > 0) { // ISSUE: could make use of content type if known byte[] content = item.get(); ClassifiableContent cc = new ClassifiableContent(); String name = item.getName(); if (name != null) cc.setIdentifier("fileupload:name:" + name); else cc.setIdentifier("fileupload:field:" + item.getFieldName()); cc.setContent(content); return cc; } } } catch (Exception e) { throw new OntopiaRuntimeException(e); } } return null; }
Example 2
Source File: FormHttpMessageConverterTests.java From spring-analysis-note with MIT License | 4 votes |
@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 3
Source File: FormHttpMessageConverterTests.java From spring-analysis-note with MIT License | 4 votes |
@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 4
Source File: FormHttpMessageConverterTests.java From java-technology-stack with MIT License | 4 votes |
@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 5
Source File: FormHttpMessageConverterTests.java From java-technology-stack with MIT License | 4 votes |
@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 6
Source File: FormHttpMessageConverterTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@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 7
Source File: FormHttpMessageConverterTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@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>"))); }