@Override public InputStream getInputStream() throws IOException { return this.part.getInputStream(); }
@Override public InputStream getInputStream() throws IOException { return this.part.getInputStream(); }
private static String getValue(Part part) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(part.getInputStream(), "UTF-8")); StringBuilder value = new StringBuilder(); char[] buffer = new char[1024]; for (int length = 0; (length = reader.read(buffer)) > 0;) { value.append(buffer, 0, length); } return value.toString(); }
@Override public void transferTo(Path dest) throws IOException, IllegalStateException { FileCopyUtils.copy(this.part.getInputStream(), Files.newOutputStream(dest)); } }
@Override public byte[] getBytes() throws IOException { return FileCopyUtils.copyToByteArray(this.part.getInputStream()); }
@Override public void transferTo(File dest) throws IOException, IllegalStateException { this.part.write(dest.getPath()); if (dest.isAbsolute() && !dest.exists()) { // Servlet 3.0 Part.write is not guaranteed to support absolute file paths: // may translate the given path to a relative location within a temp dir // (e.g. on Jetty whereas Tomcat and Undertow detect absolute paths). // At least we offloaded the file from memory storage; it'll get deleted // from the temp dir eventually in any case. And for our user's purposes, // we can manually copy it to the requested location as a fallback. FileCopyUtils.copy(this.part.getInputStream(), Files.newOutputStream(dest.toPath())); } }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String description = request.getParameter("description"); // Retrieves <input type="text" name="description"> Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file"> String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix. InputStream fileContent = filePart.getInputStream(); // ... (do your job here) }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ... List<Part> fileParts = request.getParts().stream().filter(part -> "file".equals(part.getName())).collect(Collectors.toList()); // Retrieves <input type="file" name="file" multiple="true"> for (Part filePart : fileParts) { String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix. InputStream fileContent = filePart.getInputStream(); // ... (do your job here) } }
@Override public InputStream getInputstream() throws IOException { return sizeLimit == null ? part.getInputStream() : new BoundedInputStream(part.getInputStream(), sizeLimit); }
@Override public void transferTo(Path dest) throws IOException, IllegalStateException { FileCopyUtils.copy(this.part.getInputStream(), Files.newOutputStream(dest)); } }
@Override public byte[] getBytes() throws IOException { return FileCopyUtils.copyToByteArray(this.part.getInputStream()); }
@Override public void transferTo(File dest) throws IOException, IllegalStateException { this.part.write(dest.getPath()); if (dest.isAbsolute() && !dest.exists()) { // Servlet 3.0 Part.write is not guaranteed to support absolute file paths: // may translate the given path to a relative location within a temp dir // (e.g. on Jetty whereas Tomcat and Undertow detect absolute paths). // At least we offloaded the file from memory storage; it'll get deleted // from the temp dir eventually in any case. And for our user's purposes, // we can manually copy it to the requested location as a fallback. FileCopyUtils.copy(this.part.getInputStream(), Files.newOutputStream(dest.toPath())); } }
protected static InputStream maybeDecompress(Part item) throws IOException { InputStream in = item.getInputStream(); String contentType = item.getContentType(); if (contentType != null) { switch (contentType) { case "application/zip": in = new ZipInputStream(in); break; case "application/gzip": case "application/x-gzip": in = new GZIPInputStream(in); break; } } return in; }
@RequestMapping(value = "/part", method = RequestMethod.POST) public String processPart(@RequestParam Part part, @RequestPart Map<String, String> json, Model model) throws IOException { model.addAttribute("fileContent", part.getInputStream()); model.addAttribute("jsonContent", json); return "redirect:/index"; }
@Override public InputStream getBody() throws IOException { if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) { try { return this.multipartRequest.getPart(this.partName).getInputStream(); } catch (Exception ex) { throw new MultipartException("Could not parse multipart servlet request", ex); } } else { MultipartFile file = this.multipartRequest.getFile(this.partName); if (file != null) { return file.getInputStream(); } else { String paramValue = this.multipartRequest.getParameter(this.partName); return new ByteArrayInputStream(paramValue.getBytes(determineCharset())); } } }
@Override public InputStream getBody() throws IOException { if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) { try { return this.multipartRequest.getPart(this.partName).getInputStream(); } catch (Exception ex) { throw new MultipartException("Could not parse multipart servlet request", ex); } } else { MultipartFile file = this.multipartRequest.getFile(this.partName); if (file != null) { return file.getInputStream(); } else { String paramValue = this.multipartRequest.getParameter(this.partName); return new ByteArrayInputStream(paramValue.getBytes(determineCharset())); } } }
@Test public void read_no_input_stream_when_part_size_is_zero() throws Exception { when(source.getContentType()).thenReturn("multipart/form-data"); InputStream file = mock(InputStream.class); Part part = mock(Part.class); when(part.getInputStream()).thenReturn(file); when(part.getSize()).thenReturn(0L); when(source.getPart("param1")).thenReturn(part); assertThat(underTest.readInputStreamParam("param1")).isNull(); }
@Test public void returns_null_when_invalid_part() throws Exception { when(source.getContentType()).thenReturn("multipart/form-data"); InputStream file = mock(InputStream.class); Part part = mock(Part.class); when(part.getSize()).thenReturn(0L); when(part.getInputStream()).thenReturn(file); doThrow(IllegalArgumentException.class).when(source).getPart("param1"); assertThat(underTest.readInputStreamParam("param1")).isNull(); }
@Test public void read_input_stream() throws Exception { when(source.getContentType()).thenReturn("multipart/form-data"); InputStream file = mock(InputStream.class); Part part = mock(Part.class); when(part.getInputStream()).thenReturn(file); when(part.getSize()).thenReturn(10L); when(source.getPart("param1")).thenReturn(part); assertThat(underTest.readInputStreamParam("param1")).isEqualTo(file); assertThat(underTest.readInputStreamParam("param2")).isNull(); }
@Override @CheckForNull public Part readPart(String key) { try { if (!isMultipartContent()) { return null; } javax.servlet.http.Part part = source.getPart(key); if (part == null || part.getSize() == 0) { return null; } return new PartImpl(part.getInputStream(), part.getSubmittedFileName()); } catch (Exception e) { Loggers.get(ServletRequest.class).warn("Can't read file part for parameter " + key, e); return null; } }