Wednesday, February 1, 2012

How to upload the sdcard file on server through HTTP?



//Below is the method for uploading the file present on sdcard.For using below method the following jar files are required
httpmime-4.1.1.jar


public int fileupload(String upload_url, String filepath) {
// TODO Auto-generated method stub
{
int result = 0;
try {

HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(upload_url);
String compfilepath = "/sdcard/"+filepath;

File file= new File(compfilepath);
FileBody bin = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("filename", bin);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();

while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
System.out.println("Response: " + s);
result = 1;

} catch (Exception e) {
Log.e(e.getClass().getName(), e.getMessage());
}
return result;

}
}

No comments:

Post a Comment