Reputation: 45
Hi friends i am making file convert application by use of web API. And for that i need to send my file to server my code is run completely i am able to send file to server but when i choose large size file it show me memory exception the code to upload file is here
private String apiCall(String action, List query, String formatOptionXml)
throws Exception {
String currentUrl = OnlineConvert.URL;
String xmlResponse = "";
Log.d("main", "upload start ");
/*
* if (!"get-queue".equals(action)) {
* this.getServer(this.targetTypeOptions.get(this.targetType)); }
*/
try {
//FileInputStream fi = new FileInputStream(new File(in.inputpath));
URL browser = new URL(currentUrl + "/" + action);
HttpURLConnection conn = (HttpURLConnection) browser.openConnection();
String params = "queue=" + URLEncoder.encode(this.getMap2XML((Map) query.get(OnlineConvert.QUEUE_COMMAN_PARAMS),"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",formatOptionXml+ this.getFileMap2XML((Map) query.get(OnlineConvert.QUEUE_FILE_METADATA_PARAMS))),"UTF-8");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length",String.valueOf(params.length()));
conn.setRequestProperty("connection", "close");
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream wr = conn.getOutputStream();
wr.write(params.getBytes());
wr.flush();
// Get the response
int statusCode = conn.getResponseCode();
BufferedReader in = null;
String inputLine;
StringBuilder response = new StringBuilder();
if (statusCode >= 200 && statusCode < 400) {
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
} else {
in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
}
xmlResponse = response.toString();
wr.close();
in.close();
} catch (MalformedURLException e) {
Log.d("main", "MalformedURLException " + e);
throw new MalformedURLException(
"Error downloading pathway overview images :"
+ e.getMessage());
} catch (IOException e) {
Log.d("main", "IOException " + e);
throw new IOException("Error In IOException :" + e.getMessage());
} catch (Exception e) {
Log.d("main", "Exception " + e);
throw new Exception("Exception :" + e.getMessage());
}
Log.d("main", "upload done");
return xmlResponse;
}
by this i am able to upload small size file but when i try to send file more than 10MB it show me memory exception so please help me to solve this problem
Upvotes: 0
Views: 116
Reputation: 4823
I think you can send this using multipart connection request and change your code.
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
Upvotes: 1