I am currently trying to upload file / images on to my apache server from AVD. I’m not getting any errors when i execute my code, but image is not uploaded to the server. I’ve changed permission of the folder on the apache server which is
http://10.0.2.2/images to writable and editable by other users. I am having trouble with this, please help. Follow is my code
This code gets the image and decode into bitmap.
bmp = BitmapFactory.decodeFile(filePath);
Following is code that i run to upload files to the server
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Bitmap tmpbmp = bmp;
tmpbmp.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url);
ByteArrayBody bab = new ByteArrayBody(data, "test.jpg");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploaded", bab);
reqEntity.addPart("photoCaption", new StringBody("aaaa"));
postRequest.setEntity(reqEntity);
HttpResponse httpResponse = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
Log.i("wi", "Response: " + s);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
Log.i("wi", "Encoding failed: " + e);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.i("wi", "client protocol exception!: " + e);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("wi", "IO Exception!: " + e);
}
And i am getting response from Log.i(“wi”, “Response: ” + s); =
Response: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html> <head>
<title>Index of /images</title> </head> <body><h1>Index of /images</h1><ul><li><a
href="/"> Parent Directory</a></li></ul></body></html>
It hasn’t uploaded the file.
I am using Apache’s commons-net-ftp library to upload my song file to the server…i am giving you the code….Try using this..