Uploading images to Google App Engine using a standalone Java application - java

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?

Related

upload zip file to ckan using java

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?

Sending a video file via REST API to JIRA in Java

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.

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

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

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?

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