我已经使用这种方法将图像上传到Web服务。在这里我通过Image Uri上传图片。但是我需要一个方法来传递字节数组来上传图像。我用来上传图片的代码如下。
public static ResponseFromServer upload(Uri imageUri, Context c) {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
ResponseFromServer response = new ResponseFromServer();
try {
c=image&m=upload&userId="+Constants.logedInUserId+"&&accessKey="+Constants.ACCESS_KEY;
String urlServer = Constants.SERVER_URL + "/image_upload?version="
+ "1.0" + "&email=" + URLEncoder.encode(Constants.email)
+ "&proc_date=" + URLEncoder.encode(Constants.proc_date)
+ "&access_key=" + URLEncoder.encode(Constants.ACCESS_KEY)
+ "&original_image_name="
+ URLEncoder.encode(getdate() + ".jpg");
Log.e("Image Upload API TKB ", "" + urlServer);
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = c.getContentResolver().query(imageUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
// Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
// Bitmap bm=BitmapFactory.decodeFile(imageUri.getPath());
FileInputStream fileInputStream = new FileInputStream(new File(
filePath));
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setReadTimeout(60000);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream
.writeBytes("Content-Disposition: form-data; name=\"original_image_bynary\";filename=\""
+ filePath + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
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);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
Log.e("serverResponseCode: ", serverResponseCode + "");
BufferedReader rd = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
String responseString = sb.toString();
JSONObject jsonObject;
jsonObject = new JSONObject(responseString);
String status = jsonObject.getString("status");
String message = jsonObject.getString("msg");
// String data=jsonObject.getString("data");
response.setStatus(status);
response.setMessage(message);
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (SocketTimeoutException ste) {
Log.e("Timeout", "Data not found");
return null;
}
catch (Exception ex) {
// Exception handling
response.setMessage("Upload failed. " + ex.getMessage());
response.setStatus("0");
ex.printStackTrace();
return null;
// Exception e=ex;
}
return response;
}如何自定义此代码,以便只传递Byte数组以上传Image。