@Override public Part getPart(String name) throws IOException, ServletException { return b.request.getPart(name); }
Part part = request.getPart(inputToDecodeId);
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) }
@Override public Object getValue(HttpServletRequest request) throws Exception { if (List.class.isAssignableFrom(targetType.getRawClass())) { JavaType contentType = targetType.getContentType(); if (contentType != null && contentType.getRawClass().equals(Part.class)) { //get all parts return request.getParts() .stream() .filter(part -> part.getName().equals(paramPath)) .collect(Collectors.toList()); } } return request.getPart(paramPath); }
@MultipartConfig @WebServlet("/uploadServlet") public class UploadServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Part file = request.getPart("file"); String filename = getFilename(file); InputStream filecontent = file.getInputStream(); // ... Do your file saving job here. response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); response.getWriter().write("File " + filename + " successfully uploaded"); } private static String getFilename(Part part) { for (String cd : part.getHeader("content-disposition").split(";")) { if (cd.trim().startsWith("filename")) { String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", ""); return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix. } } return null; } }
@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(); }
private static void decodeAdvanced(FacesContext context, FileUpload fileUpload, HttpServletRequest request) throws IOException, ServletException { String clientId = fileUpload.getClientId(context); Part part = request.getPart(clientId); if (part != null) { NativeUploadedFile uploadedFile = new NativeUploadedFile(part, fileUpload); if (isValidFile(context, fileUpload, uploadedFile)) { fileUpload.queueEvent(new FileUploadEvent(fileUpload, uploadedFile)); } } }
@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; } }
return (isMultipart ? request.getPart(name): null);
return (isMultipart ? request.getPart(name): null);
/** * The default behavior of this method is to call getPart on the wrapped * request object. * * @since Servlet 3.0 */ public Part getPart(String name) throws IOException, ServletException { return this._getHttpServletRequest().getPart(name); }
@Override public Part getPart(String name) throws IOException, ServletException { return getRequest().getPart(name); }
public Part getPart(String name) throws IOException, ServletException { return getHttpServletRequest().getPart(name); }
InputStream in = null; try { Part p = httpRequest.getPart(name); if (p != null) { long len = p.getSize();
/** * @throws IllegalStateException if the request is too large */ public String getContentType( String partName ) { String result = null; if (isMultiPartRequest) { try { Part p = httpRequest.getPart(partName); if (p != null) result = p.getContentType(); } catch (IOException ioe) { log(ioe); } catch (ServletException se) { log(se); } catch (IllegalStateException ise) { log(ise); throw ise; } } return result; }
/** * @throws IllegalStateException if the request is too large */ public String getFilename(String partName ) { String result = null; if (isMultiPartRequest) { try { Part p = httpRequest.getPart(partName); if (p != null) result = p.getSubmittedFileName(); } catch (IOException ioe) { log(ioe); } catch (ServletException se) { log(se); } catch (IllegalStateException ise) { log(ise); throw ise; } } return result; }
/** * @throws IllegalStateException if the request is too large */ public InputStream getInputStream(String partName ) { InputStream result = null; if (isMultiPartRequest) { try { Part p = httpRequest.getPart(partName); if (p != null) result = p.getInputStream(); } catch (IOException ioe) { log(ioe); } catch (ServletException se) { log(se); } catch (IllegalStateException ise) { log(ise); throw ise; } } return result; }
/** * The default behavior of this method is to call getPart on the wrapped * request object. * * @since Servlet 3.0 */ @Override public Part getPart(String name) throws IOException, ServletException { return this._getHttpServletRequest().getPart(name); }