@Override public String name() { return upload.getSubmittedFileName(); }
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) } }
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 List<NativeUpload> files(final String name) throws IOException { try { if (multipart) { return req.getParts().stream() .filter(part -> part.getSubmittedFileName() != null && part.getName().equals(name)) .map(part -> new ServletUpload(part, tmpdir)) .collect(Collectors.toList()); } return Collections.emptyList(); } catch (ServletException ex) { throw new IOException("File not found: " + name, ex); } }
@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; } }
contents.put(ul.getSubmittedFileName(), w.toString());
@Override public String getOriginalFilename() { return part.getSubmittedFileName(); }
private FlowFile savePartDetailsAsAttributes(final ProcessSession session, final Part part, final FlowFile flowFile, final int sequenceNumber, final int allPartsCount) { final Map<String, String> attributes = new HashMap<>(); for (String headerName : part.getHeaderNames()) { final String headerValue = part.getHeader(headerName); putAttribute(attributes, "http.headers.multipart." + headerName, headerValue); } putAttribute(attributes, "http.multipart.size", part.getSize()); putAttribute(attributes, "http.multipart.content.type", part.getContentType()); putAttribute(attributes, "http.multipart.name", part.getName()); putAttribute(attributes, "http.multipart.filename", part.getSubmittedFileName()); putAttribute(attributes, "http.multipart.fragments.sequence.number", sequenceNumber + 1); putAttribute(attributes, "http.multipart.fragments.total.number", allPartsCount); return session.putAllAttributes(flowFile, attributes); }
private FlowFile savePartAttributes(ProcessContext context, ProcessSession session, Part part, FlowFile flowFile, final int i, final int allPartsCount) { final Map<String, String> attributes = new HashMap<>(); for (String headerName : part.getHeaderNames()) { final String headerValue = part.getHeader(headerName); putAttribute(attributes, "http.headers.multipart." + headerName, headerValue); } putAttribute(attributes, "http.multipart.size", part.getSize()); putAttribute(attributes, "http.multipart.content.type", part.getContentType()); putAttribute(attributes, "http.multipart.name", part.getName()); putAttribute(attributes, "http.multipart.filename", part.getSubmittedFileName()); putAttribute(attributes, "http.multipart.fragments.sequence.number", i+1); putAttribute(attributes, "http.multipart.fragments.total.number", allPartsCount); return session.putAllAttributes(flowFile, attributes); }
private OperationRequestPart createOperationRequestPart(Part part) throws IOException { HttpHeaders partHeaders = extractHeaders(part); List<String> contentTypeHeader = partHeaders.get(HttpHeaders.CONTENT_TYPE); if (part.getContentType() != null && contentTypeHeader == null) { partHeaders.setContentType(MediaType.parseMediaType(part.getContentType())); } return new OperationRequestPartFactory().create(part.getName(), StringUtils.hasText(part.getSubmittedFileName()) ? part.getSubmittedFileName() : null, FileCopyUtils.copyToByteArray(part.getInputStream()), partHeaders); }
private void attachFile(String boundary, Iterator<Entry<String, Part>> uploadsIterator) { if (!uploadsIterator.hasNext()) { request.write(boundaryEndInfo(boundary)); request.end(); return; } Entry<String, Part> entry = uploadsIterator.next(); // do not use part.getName() to get parameter name // because pojo consumer not easy to set name to part String name = entry.getKey(); Part part = entry.getValue(); String filename = part.getSubmittedFileName(); Buffer fileHeader = fileBoundaryInfo(boundary, name, part); request.write(fileHeader); new PumpFromPart(context, part).toWriteStream(request).whenComplete((v, e) -> { if (e != null) { LOGGER.debug("Failed to sending file [{}:{}].", name, filename, e); asyncResp.consumerFail(e); return; } LOGGER.debug("finish sending file [{}:{}].", name, filename); attachFile(boundary, uploadsIterator); }); }
public static void prepareDownloadHeader(HttpServletResponseEx responseEx, Part part) { if (responseEx.getHeader(HttpHeaders.CONTENT_LENGTH.toString()) == null) { responseEx.setChunked(true); } if (responseEx.getHeader(HttpHeaders.CONTENT_TYPE.toString()) == null) { responseEx.setHeader(HttpHeaders.CONTENT_TYPE.toString(), part.getContentType()); } if (responseEx.getHeader(javax.ws.rs.core.HttpHeaders.CONTENT_DISPOSITION) == null) { // to support chinese and space filename in firefox // must use "filename*", (https://tools.ietf.org/html/rtf6266) String encodedFileName = HttpUtils.uriEncodePath(part.getSubmittedFileName()); responseEx.setHeader(javax.ws.rs.core.HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + encodedFileName + ";filename*=utf-8''" + encodedFileName); } }
/** * @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; }
protected Buffer fileBoundaryInfo(String boundary, String name, Part part) { Buffer buffer = Buffer.buffer(); buffer.appendString("\r\n"); buffer.appendString("--" + boundary + "\r\n"); buffer.appendString("Content-Disposition: form-data; name=\"") .appendString(name) .appendString("\"; filename=\"") .appendString(part.getSubmittedFileName() != null ? part.getSubmittedFileName() : "null") .appendString("\"\r\n"); buffer.appendString("Content-Type: ").appendString(part.getContentType()).appendString("\r\n"); buffer.appendString("Content-Transfer-Encoding: binary\r\n"); buffer.appendString("\r\n"); return buffer; }
@Override public String getFileName() { return this.part.getSubmittedFileName(); }
@Test public void requestWithPartWithContentType() throws Exception { MockHttpServletRequest mockRequest = MockMvcRequestBuilders.get("/foo") .buildRequest(new MockServletContext()); Part mockPart = mock(Part.class); given(mockPart.getHeaderNames()).willReturn(Arrays.asList("a", "b")); given(mockPart.getHeaders("a")).willReturn(Arrays.asList("alpha")); given(mockPart.getHeaders("b")).willReturn(Arrays.asList("bravo", "banana")); given(mockPart.getInputStream()) .willReturn(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 })); given(mockPart.getName()).willReturn("part-name"); given(mockPart.getSubmittedFileName()).willReturn("submitted.png"); given(mockPart.getContentType()).willReturn("image/png"); mockRequest.addPart(mockPart); OperationRequest request = this.factory.convert(mockRequest); assertThat(request.getParts().size()).isEqualTo(1); OperationRequestPart part = request.getParts().iterator().next(); assertThat(part.getName()).isEqualTo("part-name"); assertThat(part.getSubmittedFileName()).isEqualTo("submitted.png"); assertThat(part.getHeaders().getContentType()).isEqualTo(MediaType.IMAGE_PNG); assertThat(part.getHeaders().get("a")).containsExactly("alpha"); assertThat(part.getHeaders().get("b")).containsExactly("bravo", "banana"); assertThat(part.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 }); }
@Test public void requestWithPart() throws Exception { MockHttpServletRequest mockRequest = MockMvcRequestBuilders.get("/foo") .buildRequest(new MockServletContext()); Part mockPart = mock(Part.class); given(mockPart.getHeaderNames()).willReturn(Arrays.asList("a", "b")); given(mockPart.getHeaders("a")).willReturn(Arrays.asList("alpha")); given(mockPart.getHeaders("b")).willReturn(Arrays.asList("bravo", "banana")); given(mockPart.getInputStream()) .willReturn(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 })); given(mockPart.getName()).willReturn("part-name"); given(mockPart.getSubmittedFileName()).willReturn("submitted.txt"); mockRequest.addPart(mockPart); OperationRequest request = this.factory.convert(mockRequest); assertThat(request.getParts().size()).isEqualTo(1); OperationRequestPart part = request.getParts().iterator().next(); assertThat(part.getName()).isEqualTo("part-name"); assertThat(part.getSubmittedFileName()).isEqualTo("submitted.txt"); assertThat(part.getHeaders().getContentType()).isNull(); assertThat(part.getHeaders().get("a")).containsExactly("alpha"); assertThat(part.getHeaders().get("b")).containsExactly("bravo", "banana"); assertThat(part.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 }); }
continue; name = part.getSubmittedFileName(); size = part.getSize();
@Override public String getSubmittedFileName() { return getWrapped().getSubmittedFileName(); }
private OperationRequestPart createOperationRequestPart(Part part) throws IOException { HttpHeaders partHeaders = extractHeaders(part); List<String> contentTypeHeader = partHeaders.get(HttpHeaders.CONTENT_TYPE); if (part.getContentType() != null && contentTypeHeader == null) { partHeaders.setContentType(MediaType.parseMediaType(part.getContentType())); } return new OperationRequestPartFactory().create(part.getName(), StringUtils.hasText(part.getSubmittedFileName()) ? part.getSubmittedFileName() : null, FileCopyUtils.copyToByteArray(part.getInputStream()), partHeaders); }