I am wanting to upload a video file (.mp4) by POST Request to JIRA. The file gets uploaded to the server, but the video becomes corrupt (i.e. opening it doesn't work). Sending other attachments, like screenshots (.png) and text files (.txt), works fine without corrupting the file.
I am using the Apache HttpComponents HttpClient 4.3.6.
Here is example code:
File file = new File("location/to/file.mp4");
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create().addBinaryBody("file", file);
HttpPost postRequest = new HttpPost();
postRequest.addHeader(HttpHeaders.AUTHORIZATION, BASIC_AUTH);
postRequest.addHeader("X-Atlassian-Token", "nocheck");
postRequest.setEntity(multipartEntity.build());
postRequest.setURI(uri);
CloseableHttpClient client = HttpClients.createDefault();
try {
HttpResponse response = client.execute(request);
} finally {
client.close();
}
I attempted to add a video/mp4 MIME type but that didn't seem to help any:
MultipartEntityBuilder.create().addBinaryBody("file", file, ContentType.create("video/mp4"), file.getName())
The issue I had here was that QuickTime on Mac wasn't compatible with the .mp4 file format. I downloaded VLC media player and the file worked just fine without specifying a MIME type.
Related
I have a working java application that uploads a text file to a ckan installation using this code.
File file = new File("path/testFile.txt")
ContentBody cbFile = new FileBody(file ,ContentType.TEXT_HTML);
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("file", cbFile)
.build();
postRequest = new HttpPost(HOST+"/api/action/resource_create");
postRequest.setEntity(reqEntity);
postRequest.setHeader("X-CKAN-API-Key", myApiKey);
HttpResponse response = httpclient.execute(postRequest);
When i try to change the text file to a zip file, i get a 500 error, "server Error"
I am guessing this is because of the ContentType. I tried to find alternatives but nothing seems to work. Tried to create my own like so
ContentType zipType=ContentType.create("application/zip");
ContentBody cbFile = new FileBody(file ,zipType);
But still didnt work.
Any ideas?
UPDATE
It seems that there is an issue with the file size. my program works as it is for small zip files (<5KB). I am not sure if this is a java or a ckan issue. Any help?
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?
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
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.
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);