I am developing a software in which images should be upload from android to java. so far I have developed the following client on android:
String url=params[0];
String filePath=params[1];
File file=new File(filePath);
MultipartEntityBuilder multipartEntityBuilder=MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntityBuilder.addPart("file", new FileBody(file));
HttpPut httpPut=new HttpPut(url);
HttpClient httpclient = new DefaultHttpClient();
httpPut.setEntity(multipartEntityBuilder.build());
HttpResponse response;
try {
response = httpclient.execute(httpPut);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
instream.close();
return result;
}
} catch (Exception e) {
}
return null;
}
and the server is:
String path=Server.imagesPath+Utilities.getRandomString(10)+".jpg";
InputStream inputStream= arg0.getRequestBody();
File file=new File(path);
Files.copy(inputStream, file.toPath());
String response="OK";
try{
arg0.sendResponseHeaders(200, response.length());
OutputStream outputStream=arg0.getResponseBody();
outputStream.write(response.getBytes());
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The image is completely copied to the server but it is damaged and it can not be viewed. What is the problem?
it is damaged. In which way? Did you compare filelength and original filelength? Please tell. What kind of server is this? You are sure you have to PUT? Not POST?arg0? What is that?