I have very little idea how html works.What i want to do is exactly similar to the following but on android
<body>
<form action="<%= some_url %>" method="post" enctype="multipart/form-data">
<input type="file" name="myFile">
<input type="submit" value="Submit">
</form>
</body>
I tried the following code -
private static void postToUrl(String url_to_upload_on,
String file_name_with_ext, byte[] byteArray) {
String attachmentName = "file";
String attachmentFileName = file_name_with_ext;
String crlf = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try{
URL url = new URL(url_to_upload_on);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty(
"Content-Type", "multipart/form-data;boundary=" + boundary);
DataOutputStream request = new DataOutputStream(
connection.getOutputStream());
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"" +
attachmentName + "\";filename=\"" +
attachmentFileName + "\"" + crlf);
request.writeBytes(crlf);
request.write(byteArray);
request.writeBytes(crlf);
request.writeBytes(twoHyphens + boundary +
twoHyphens + crlf);
request.flush();
request.close();
}catch(Exception e){
e.printStackTrace();
}
}
this gives me no direct errors but when i get error-stream using-
Log.w(TAG, "connection.getErrorStream() = " + connection.getErrorStream());
i get this-
12-14 18:25:54.911: W/uploadToBlobStore(30558): httpUrlConnection.getErrorStream() = com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream@426dd5a8
with no success.
PS- I am uploading a file to google blobstore
PS- I can not use Apache http libraries or its multipart class as android says its depreciated
EDIT 1
Now I am using the following code but it is working only for files less then 2.3Mb -
private static void postToUrl3(String url_to_upload_on,
String file_name_with_ext, byte[] byteArray, String mimeType) {
CloseableHttpClient httpClient = null;
try {
httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost(url_to_upload_on);
MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
reqEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayBody bab = new ByteArrayBody(byteArray, file_name_with_ext);
reqEntity.addPart("file", bab);
postRequest.setEntity(reqEntity.build());
httpClient.execute(postRequest);// takes time
} catch (Exception e) {
Log.w("uploadToBlobStore", "postToUrl Exception e = " + e);
e.printStackTrace();
} finally {
if (httpClient != null) {
Log.w("uploadToBlobStore", "connection.closing ");
try {
httpClient.close();
} catch (IOException e) {
Log.w("uploadToBlobStore", "connection.closing errot e = "
+ e);
e.printStackTrace();
}
}
}
}
how to make it work with larger files?
PS- i am sending it to blobstore and i did set the maxUploadSizeBytes
and MaxUploadSizeBytesPerBlob
to 30MB.I am not able to figure out the issue with size because google blobstore documentation says -
Google App Engine includes the Blobstore service, which allows applications to serve data objects limited only by the amount of data that can be uploaded or downloaded over a single HTTP connection.
So can it be a problem with http connection? and if so, how can i configure it.