RESTEasy client multipart post file - java

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.

Related

Send and receive different types of object in netty

I am building an application in Netty which will allow people to download and upload file. But before that they should be able to log into the server. Once they log in they can download or upload the file.
Issue I am having is how will I know the data I have received is a string(username password string) or a chunked file(as I send files as ChunkedFile) or any other java object. How will I properly get them to original form. I know we can use encoder and decoders but there are no proper example available which shows anything close to the problem I am having?
Thanks
Send a POST http request with Headers
// Prepare the HTTP request.
FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uriJson.toASCIIString());
request.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
ByteBuf buf = Unpooled.copiedBuffer(data, CharsetUtil.UTF_8);
request.content().writeBytes(buf);
request.headers().set(CONTENT_LENGTH,request.content().readableBytes());

How to download a file using XMLHttpRequest

I am posting html content to the serverside and converting it to PDF and streaming the file back.
But am not able to download the file using:
var formData = new FormData();
formData.append("htmlContent", strHTML);
var request = new XMLHttpRequest();
request.open("POST", "RenderHtmlAsPDF.jsp");
request.send(formData);
Download works when I create a dynamic form and post the html content targeting it to an iframe. But I am limited by the amount of data I can send.
You can't download a file with AJAX. However you can "simulate" the behaviour by doing the folowing: make the ajax post request for file generation, after the file was generated on the server, generate a token or id with witch you can identify the file, send it back to the client and when you have received the response token on the client simply generate an iframe with the src pointing to a method on your back-end which receives the token and sends back the file.

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".

file size increased during upload

I am uploading file using httpclient. After uploading file size get changed. During file upload some extra things get added in to file.
Before uploading file it contains:
hi this is vipin check
After uploading the file contains:
--j9q7PmvnWSP9wKHHp2w_KCI4Q2jCniJvPbrE0
Content-Disposition: form-data; name="vipin.txt"; filename="vipin.txt"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
hi this is vipin check
--j9q7PmvnWSP9wKHHp2w_KCI4Q2jCniJvPbrE0--
Why file size is changing?
Why does this extra contents get added?
My httpclient code is:
HttpPut httppost = new HttpPut(URIUtil.encodeQuery(newUrl));
httppost.setHeader("X-Auth-Token", cred.getAuthToken());
httppost.addHeader("User-Agent", "NetMagic-file-upload");
System.out.println("Dest : " + dest.getAbsolutePath());
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = (ContentBody) new FileBody(src);
mpEntity.addPart(dest.getName(), cbFile);
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
You're doing a PUT request, yet your client uses multipart encoding as commonly uses in HTML form posts.
What appears to be happening is that the client is sending the file to be uploaded as multipart entity, but the server is treating it as an plain file. It is not entirely clear where the fault lies.
It is possible that the server is ignoring the content type in the request header. That would most likely be a bug in the servlet (or whatever) that is responsible for handing the upload request.
It is possible that the client is not setting a content type in the request header. I'd have expected that the client library would take care of that for you. But it is possible that you need to do it explicitly.
I'd advise looking at the request headers as they are sent by the client or received by the server to see if there is a proper multi-part content-type. That will help you determine where the problem is.
But there is an obvious solution. If the server cannot cope with multiparts, change the client side to not send them.

Categories