public static boolean matchAttachmentId(Attachment at, String value) { if (value.isEmpty()) { return true; } if (at.getContentId().equals(value)) { return true; } ContentDisposition cd = at.getContentDisposition(); return cd != null && value.equals(cd.getParameter("name")); }
public Attachment getAttachment(String contentId) { for (Attachment a : atts) { if (contentId.equalsIgnoreCase(a.getContentId())) { return a; } ContentDisposition cd = a.getContentDisposition(); if (cd != null && contentId.equals(cd.getParameter("name"))) { return a; } } return null; }
public static boolean matchAttachmentId(Attachment at, String value) { if (value.isEmpty()) { return true; } if (at.getContentId().equals(value)) { return true; } ContentDisposition cd = at.getContentDisposition(); if (cd != null && value.equals(cd.getParameter("name"))) { return true; } return false; }
public Attachment getAttachment(String contentId) { for (Attachment a : atts) { if (contentId.equalsIgnoreCase(a.getContentId())) { return a; } ContentDisposition cd = a.getContentDisposition(); if (cd != null && contentId.equals(cd.getParameter("name"))) { return a; } } return null; }
private static Map<String, Attachment> fromListToMap(List<Attachment> atts, boolean preferContentDisposition) { Map<String, Attachment> map = new LinkedHashMap<>(); for (Attachment a : atts) { String contentId = null; if (preferContentDisposition) { ContentDisposition cd = a.getContentDisposition(); if (cd != null) { contentId = cd.getParameter("name"); } } if (contentId == null) { contentId = a.getContentId(); } map.put(contentId, a); } return map; }
private static Map<String, Attachment> fromListToMap(List<Attachment> atts, boolean preferContentDisposition) { Map<String, Attachment> map = new LinkedHashMap<String, Attachment>(); for (Attachment a : atts) { String contentId = null; if (preferContentDisposition) { ContentDisposition cd = a.getContentDisposition(); if (cd != null) { contentId = cd.getParameter("name"); } } if (contentId == null) { contentId = a.getContentId(); } map.put(contentId, a); } return map; }
@Path("/books/file/semicolon") @Consumes("multipart/form-data") @Produces("text/plain") @POST public String addBookFileNameSemicolon(@Multipart("a") Attachment att) { return att.getObject(String.class) + ", filename:" + att.getContentDisposition().getParameter("filename"); }
if (cd != null && cd.getParameter("filename") != null) { ub.path(cd.getParameter("filename"));
String fileName = file.getContentDisposition().getParameter( "filename" );
if (cd != null && cd.getParameter("filename") != null) { ub.path(cd.getParameter("filename"));
String fileName = file.getContentDisposition().getParameter( "filename" );
continue; String cdName = cd == null ? null : cd.getParameter("name"); String contentId = a.getContentId(); String name = StringUtils.isEmpty(cdName) ? contentId : cdName.replace("\"", "").replace("'", "");
continue; String cdName = cd == null ? null : cd.getParameter("name"); String contentId = a.getContentId(); String name = StringUtils.isEmpty(cdName) ? contentId : cdName.replace("\"", "").replace("'", "");
@Test public void testUploadImageFromForm2() throws Exception { File file = new File(getClass().getResource("/org/apache/cxf/systest/jaxrs/resources/java.jpg") .toURI().getPath()); String address = "http://localhost:" + PORT + "/bookstore/books/formimage2"; WebClient client = WebClient.create(address); client.type("multipart/form-data").accept("multipart/form-data"); WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart", "true"); MultipartBody body2 = client.post(file, MultipartBody.class); InputStream is2 = body2.getRootAttachment().getDataHandler().getInputStream(); byte[] image1 = IOUtils.readBytesFromStream( getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg")); byte[] image2 = IOUtils.readBytesFromStream(is2); assertTrue(Arrays.equals(image1, image2)); ContentDisposition cd2 = body2.getRootAttachment().getContentDisposition(); assertEquals("form-data;name=file;filename=java.jpg", cd2.toString()); assertEquals("java.jpg", cd2.getParameter("filename")); }
@Test public void testUploadImageFromForm() throws Exception { InputStream is1 = getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg"); String address = "http://localhost:" + PORT + "/bookstore/books/formimage"; WebClient client = WebClient.create(address); HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit(); conduit.getClient().setReceiveTimeout(1000000); conduit.getClient().setConnectionTimeout(1000000); client.type("multipart/form-data").accept("multipart/form-data"); ContentDisposition cd = new ContentDisposition("attachment;filename=java.jpg"); MultivaluedMap<String, String> headers = new MetadataMap<>(); headers.putSingle("Content-ID", "image"); headers.putSingle("Content-Disposition", cd.toString()); headers.putSingle("Content-Location", "http://host/bar"); headers.putSingle("custom-header", "custom"); Attachment att = new Attachment(is1, headers); MultipartBody body = new MultipartBody(att); MultipartBody body2 = client.post(body, MultipartBody.class); InputStream is2 = body2.getRootAttachment().getDataHandler().getInputStream(); byte[] image1 = IOUtils.readBytesFromStream( getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg")); byte[] image2 = IOUtils.readBytesFromStream(is2); assertTrue(Arrays.equals(image1, image2)); ContentDisposition cd2 = body2.getRootAttachment().getContentDisposition(); assertEquals("attachment;filename=java.jpg", cd2.toString()); assertEquals("java.jpg", cd2.getParameter("filename")); assertEquals("http://host/location", body2.getRootAttachment().getHeader("Content-Location")); }