Sending a model object and a binary stream in one multipart response - java

I´m currently recovering from a change in the company network configuration, which assassinated the file transfer in my document management application when users are connected via VPN. The communication between the rich client and my application server is implemented using Springs HttpInvoker, the file transfer used RMIIO. The RemoteInputStream was simply an attribute on my model object that represented the file I transfered.
So anyway, I have to replace RMIIO (DirectRemoteInputStream is not a solution here) and play around with plain HTTP streaming. I want to send a serialized model object and the binary data in a stream to the server, which works fine so far using a multipart request:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(...);
MultipartEntity multipartEntity = new MultipartEntity();
InputStreamBody filePart = new InputStreamBody(new FileInputStream(file), "application/octet-stream", file.getName());
// this is my model object
byte[] serializedFileObject = serializeObject(fileObject);
multipartEntity.addPart("file", filePart);
multipartEntity.addPart("fileObject", new ByteArrayBody(serializedFileObject, "fileObject.ser"));
post.setEntity(multipartEntity);
HttpResponse response = client.execute(post);
EntityUtils.consume(response.getEntity());
Works fine and I can send model objects and binary data to the application server. But how would one do the opposite - return a model object and the binary data in one response? I read about multipart responses and how they can be consumed by several browsers, but how would one implement it with commons-httpclient - or even sweeter, using springs RestTemplate?

Related

RESTEasy client multipart post file

I'm trying to send a file with resteasy client to an http server with some code like this:
File source = new File("test.pdf");
Client client = ClientBuilder.newClient();
MultipartFormDataOutput upload = new MultipartFormDataOutput();
upload.addFormData("source", source, MediaType.APPLICATION_OCTET_STREAM_TYPE);
Entity entity = Entity.entity(upload, MediaType.MULTIPART_FORM_DATA_TYPE)
Response response = client.target(url).request().post(entity);
What happens is that on the http server I'm not getting the usual "file" in request (with the content, the name etc..), but something like a regular POST parameter named "source" with the file content as its value.
I tried it with some different web servers, so the issue have to be in the request that RESTeasy builds.
Any help?
MultipartFormDataOutput behaves the same away as a HTML form would do. It sends key/value pairs to the server.
If you want to upload a MIME message consider using MultipartOutput.

Uploading images to Google App Engine using a standalone Java application

I'm writing a simple, no UI (hence no .jsp/.html files), console based Java application to read image file paths from a file and upload the images to my sample photo feed application on Google App Engine.
I'm using Apache HttpClient to make the URL connection and the POST request to my app engine application and this is the code I have so far.
MultipartEntity multiPartEntity = new MultipartEntity();
multiPartEntity.addPart("photo", new FileBody(new File(filePath)));
multiPartEntity.addPart("title", new StringBody(comment));
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(uploadUrl);
post.setEntity(multiPartEntity);
HttpResponse response = client.execute(post);
This uploads the file and I can see it in the Blob Viewer. However, the Content-Type of the file is set to "application/octet-stream".
I want the Content-Type to be set to "image/jpeg" or "image/png" or so on depending on the image type. I tried modifying the code a bit using
MultipartEntity multiPartEntity = new MultipartEntity();
multiPartEntity.addPart("photo", new FileBody(new File(filePath), "image/jpeg"));
multiPartEntity.addPart("title", new StringBody(comment));
but this failed to even upload the file into the Blob Viewer and I still get the same response from the application servlet.
Can somebody help me crack this?

how to sending multipart/form-data Post Request in with use of Apache HttpComponents in java

i am creating a desktop application which send file to an tomcat server. the servlet receiver and saves file fine.
I need some help to do a java program that post in a https site. I dont know how to put the parameters because it a multpart form data contect type.. Please help! when I do a post with firefox its like this...
This will depend. I've used the following technique to upload a multi-part file to a server before, based on providing a series of form key/name pairs.
This will be depended on you own requirements and what the servlet is actually expecting...
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
String name = file.getName();
entity.addPart(new FormBodyPart("someFormParameter", new StringBody("someFormName")));
/*...*/
entity.addPart("formFileNameParameter", new FileBody(file, mimeType));
HttpClient client = /*...*/
HttpPost post = new HttpPost(url.toURI());
post.setEntity(entity);
HttpResponse response = client.execute(post);
// Process response

How to send text and binary data from a custom netty HTTP server in Java?

Based on netty 3.6 I am implementing a small HTTP server into my Java desktop application. By now I have successfully created the basic HTTP server layout, I can send various text-based files to my browser-based clients.
In my netty server pipeline factory I create new channel pipelines as following:
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpChunkAggregator(1048576));
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("handler", new HttpServerHandler());
In my HttpServerHandler class I send text data to the clients as following:
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.setHeader(CONTENT_TYPE, contentType + "; charset=UTF-8");
response.setHeader(PRAGMA, "no-cache");
response.setContent(ChannelBuffers.copiedBuffer(responseText, CharsetUtil.UTF_8));
if(keepAlive)
{
response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());
response.setHeader(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
ChannelFuture future = e.getChannel().write(response);
Good. Now I additionally want to send binary data (e.g. images) to my clients. Since I found no online example on how to achieve this I have two questions:
How do I need to modify the channel pipeline to be able to send both, text and binary data to my clients?
How to modify the HttpServerHandler class in order to send a binary file to the client?
I do not think there is any more differenece between context or binary. I think what you need to do is set the content-type right. Then browser know how to handle it.
If you want to send a png file to browser, set the content-type to "image/png".

How do I load a video file into the request to send to youtube?

I'm trying to upload a video file from the device to the YouTube api. I have the authorization part working, but their documentation has me embedding the video data right into the XML payload (between multipart request entity codes), and I'm not entirely clear on what the correct way is to read the file in, encode it, and print it back out to the request. My assumption is that I need to load it into a byte[] and then spit that back out to string while encoding it, but I'd rather have some authoritative guidance than play trial-and-error games in the dark.
TIA
I think you want to do this
InputStreamBody metadata = new InputStreamBody(xmlMetadata, "application/atom+xml; charset=UTF-8");
FileBody content = new FileBody(new File("video.mp4"), "application/octet-stream");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("metadata", metadata);
reqEntity.addPart("content", content);
post.setEntity(reqEntity);
client.execute(post);

Categories