/** * Test that the body consisting of a file part can be posted. */ public void testPostFilePart() throws Exception { this.server.setHttpService(new EchoService()); PostMethod method = new PostMethod(); byte[] content = "Hello".getBytes(); MultipartRequestEntity entity = new MultipartRequestEntity( new Part[] { new FilePart( "param1", new ByteArrayPartSource("filename.txt", content), "text/plain", "ISO-8859-1") }, method.getParams()); method.setRequestEntity(entity); client.executeMethod(method); assertEquals(200,method.getStatusCode()); String body = method.getResponseBodyAsString(); assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0); assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0); assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0); assertTrue(body.indexOf("Hello") >= 0); }
private List<Part> buildParts(BaseRequest request) { List<Part> parts = new ArrayList<Part>(); for (PartHolder h : TankHttpUtil.getPartsFromBody(request)) { if (h.getFileName() == null) { StringPart stringPart = new StringPart(h.getPartName(), new String(h.getBodyAsString())); if (h.isContentTypeSet()) { stringPart.setContentType(h.getContentType()); } parts.add(stringPart); } else { PartSource partSource = new ByteArrayPartSource(h.getFileName(), h.getBody()); FilePart p = new FilePart(h.getPartName(), partSource); if (h.isContentTypeSet()) { p.setContentType(h.getContentType()); } parts.add(p); } } return parts; }
private MockHttpServletRequest getMultipartRequest( ) throws Exception { MockHttpServletRequest request = new MockHttpServletRequest( ); byte [ ] fileContent = new byte [ ] { 1, 2, 3 }; Part [ ] parts = new Part [ ] { new FilePart( "file1", new ByteArrayPartSource( "file1", fileContent ) ) }; MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity( parts, new PostMethod( ).getParams( ) ); // Serialize request body ByteArrayOutputStream requestContent = new ByteArrayOutputStream( ); multipartRequestEntity.writeRequest( requestContent ); // Set request body to HTTP servlet request request.setContent( requestContent.toByteArray( ) ); // Set content type to HTTP servlet request (important, includes Mime boundary string) request.setContentType( multipartRequestEntity.getContentType( ) ); request.setMethod( "POST" ); return request; }
public HttpMethod buildMultipartRequest(URI location, Map<Path, byte[]> toUpload) throws HttpException, IOException { PostMethod filePost = new PostMethod(location.toASCIIString()); List<Part> parts = new ArrayList<Part>(toUpload.size()); for (Entry<Path, byte[]> entry : toUpload.entrySet()) parts.add(new FilePart(entry.getKey().toString(), new ByteArrayPartSource(entry.getKey().toString(), entry.getValue()))); filePost.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), filePost.getParams())); return filePost; }
private static void addFilePart(final IContext context, String partname, final IMendixObject source, List<Part> parts) throws IOException { ByteArrayPartSource p = new ByteArrayPartSource( getFileDocumentFileName(context, source), IOUtils.toByteArray(Core.getFileDocumentContent(context, source))); parts.add(new FilePart(partname, p)); }
@Test public void testMultipart() throws IOException, ServletException { String partName = "file"; String resourceName = "/testFile.txt"; byte[] fileContent = FileCopyUtils.copyToByteArray( getClass().getResourceAsStream(resourceName)); // Create part & entity from resource Part[] parts = new Part[] { new FilePart(partName, new ByteArrayPartSource(resourceName, fileContent))}; MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new PostMethod().getParams()); // Serialize request body ByteArrayOutputStream requestContent = new ByteArrayOutputStream(); multipartRequestEntity.writeRequest(requestContent); // Set request body to HTTP servlet request request.setContent(requestContent.toByteArray()); // Set content type to HTTP servlet request (important, includes Mime boundary string) request.setContentType(multipartRequestEntity.getContentType()); request.setPathInfo("/multipart-test"); request.setMethod("POST"); servlet.service(request, response); assertEquals(resourceName, response.getHeader("file-name")); assertEquals("This is a file for multipart test.\n", response.getHeader("file-content")); }
@Override public void write(String name, byte[] bytes, String fileName, String contentType, String charset) throws IOException { parts.add(new FilePart(name, new ByteArrayPartSource(fileName, bytes), contentType, charset)); }
/** * Get similar images by vector * * @param vector * @param threshold * @return */ public JsonResultSet getSimilarImagesAndIndex(String id, double[] vector, double threshold) { JsonResultSet similar = new JsonResultSet(); byte[] vectorInBytes = new byte[8 * vector.length]; ByteBuffer bbuf = ByteBuffer.wrap(vectorInBytes); for (double value : vector) { bbuf.putDouble(value); } PostMethod queryMethod = null; String response = null; try { ByteArrayPartSource source = new ByteArrayPartSource("bytes", vectorInBytes); Part[] parts = { new StringPart("id", id), new FilePart("vector", source), new StringPart("threshold", String.valueOf(threshold)) }; queryMethod = new PostMethod(webServiceHost + "/rest/visual/qindex/" + collectionName); queryMethod.setRequestEntity(new MultipartRequestEntity(parts, queryMethod.getParams())); int code = httpClient.executeMethod(queryMethod); if (code == 200) { InputStream inputStream = queryMethod.getResponseBodyAsStream(); StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer); response = writer.toString(); queryMethod.releaseConnection(); similar = parseResponse(response); } } catch (Exception e) { _logger.error("Exception for vector of length " + vector.length, e); response = null; } finally { if (queryMethod != null) { queryMethod.releaseConnection(); } } return similar; }
/** * Get similar images by vector * * @param vector * @param threshold * @return */ public JsonResultSet getSimilarImages(double[] vector, double threshold) { JsonResultSet similar = new JsonResultSet(); byte[] vectorInBytes = new byte[8 * vector.length]; ByteBuffer bbuf = ByteBuffer.wrap(vectorInBytes); for (double value : vector) { bbuf.putDouble(value); } PostMethod queryMethod = null; String response = null; try { ByteArrayPartSource source = new ByteArrayPartSource("bytes", vectorInBytes); Part[] parts = { new FilePart("vector", source), new StringPart("threshold", String.valueOf(threshold)) }; queryMethod = new PostMethod(webServiceHost + "/rest/visual/query_vector/" + collectionName); queryMethod.setRequestEntity(new MultipartRequestEntity(parts, queryMethod.getParams())); int code = httpClient.executeMethod(queryMethod); if (code == 200) { InputStream inputStream = queryMethod.getResponseBodyAsStream(); StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer); response = writer.toString(); queryMethod.releaseConnection(); similar = parseResponse(response); } } catch (Exception e) { _logger.error("Exception for vector of length " + vector.length, e); response = null; } finally { if (queryMethod != null) { queryMethod.releaseConnection(); } } return similar; }