upload zip file to ckan using java - 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?

Related

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.

Custom MimeType file won't upload using HttpComponent

Okay, so I am trying to upload an audio file to a server using Java/Apache-HttpComponents. Server expects that file's MimeType be "audio/wav". Here is what i am trying:
File file = new File("testfile2.wav");
HttpPost httpPost = new HttpPost(uploadURL);
HttpResponse httpResponse;
String htmlResponse = "";
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody fileBody = new FileBody(file, ContentType.create("audio/wav"));
builder.addPart("mediaListForm.mediaForms[0].description", new StringBody("RBT Name", ContentType.TEXT_PLAIN));
builder.addPart("mediaListForm.mediaForms[0].artistId", new StringBody("2649", ContentType.TEXT_PLAIN));
builder.addPart("mediaListForm.mediaForms[0].categoryId", new StringBody("2", ContentType.TEXT_PLAIN));
builder.addPart("mediaListForm.mediaForms[0].file;", fileBody);
httpPost.setEntity(builder.build());
If i do not specify MimeType then default MimeType is sent as "application/octet-stream" and server rejects my request. If I specify mime type as above new FileBody(file, ContentType.create("audio/wav")); then Server says Invalid value for field mediaListForm.mediaForms[0].file. I tried debugging and explored request object which is like this:
------WebKitFormBoundaryqQFxF4y3FTYY3pEf
Content-Disposition: form-data; name="mediaListForm.mediaForms[0].file";
Content-Type: audio/wav
File name is missing from my request, any suggestions?
Not sure about why wouldn't method below work:
builder.addPart("mediaListForm.mediaForms[0].file", fileBody);
//If filebody contains custom contentType it does not work otherwise file is uploaded but server rejects it because of invalid contentType
Now there are multiple methods for adding file to our entity, some methods expect FileInputStream some expects File, use the one expected by server like I tried FileInputStream it server threw an exception and I figured it out that I had to send File instead, so this method worked like a charm.
builder.addBinaryBody("mediaListForm.mediaForms[0].file", file, ContentType.create("audio/wav"), rbt.getFullPath());

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 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);

Fail sending big file using DefaultHttpClient

I'm trying to transfer big file to the server using httpput.
However, I can't to transfer big files. I get IOException with error message: "I/O error during system call, Connection reset by peer".
I'm using the code:
// create authenticate client
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
// create HTTP put with the file
HttpPut httpPut = new HttpPut(url);
final File recordingFile = new File(mDir, mName);
FileEntity entity = new FileEntity(recordingFile, "binary/octet-stream");
entity.setChunked(true);
httpPut.setEntity(entity);
httpPut.addHeader("Connection", "Keep-Alive");
httpPut.addHeader("Content-Type", "application/zip");
// Execute
HttpResponse res = client.execute(httpPut);
int statusCode = res.getStatusLine().getStatusCode();
When sending file through http remember that your server http has a max-limit to dimension of the file.
If i'm not wrong the default value is 2MB: but you can change this on the configuration file of the server (PHP).
The file to check is php.ini.
Open the file and search for 'upload_max_filesize = 2M': simply change 2 with the dimension you need for your project and save.
That's all!
I think you need to change the maximum Request size in the web.config file.
<httpRuntime executionTimeout="110" maxRequestLength="8192" />

Categories