[英]How to upload image from android to java
我正在開發一種應將圖像從android上傳到java的軟件。 到目前為止,我已經在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;
}
服務器是:
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();
}
該圖像已完全復制到服務器,但已損壞,無法查看。 問題是什么?
看起來好像上傳是以多部分形式完成的,它允許您將圖像插入為要發送的數據的一部分。 但服務器將其讀取為純八位位組流,而不是多部分形式。
您需要選擇一個。 要么將其解釋為服務器上的表單數據,要么將數據作為流而不是表單數據發送。
我建議您看看Apache Commons FileUpload ,它將簡化很多工作。
您還可以使用多個數據部分實體進行圖像上傳
您可以在此處查看完整的演示: http : //niravranpara.blogspot.in/2012/11/upload-video-in-server.html
我最終可以使用本文解決問題:
我在Android上使用的代碼是:
String urlString=params[0];
String path=params[1];
File file=new File(path);
int maxBufferSize=1024;
URL url=null;
try {
url=new URL(urlString);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpURLConnection connection=null;
try {
connection = (HttpURLConnection) url.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
try {
connection.setRequestMethod("PUT");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection.setRequestProperty("Content-Type", "image/jpg");
OutputStream outputStream=null;
FileInputStream fileInputStream=null;
byte[] buffer;
int bytesRead,bytesAvailable,bufferSize;
try {
outputStream=connection.getOutputStream();
fileInputStream=new FileInputStream(file);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while(bytesRead > 0){
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.d("test Response Code ",Integer.toString(serverResponseCode));
Log.d("test Response Message ", serverResponseMessage);
} catch (IOException e) {
Log.d("test", e.getMessage());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.