我尝试使用此代码上传图片:
private String uploadFile() {
String responseString = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL_M);
Log.i("UploadApp", "upload url: " + Config.FILE_UPLOAD_URL_M);
AndroidMultiPartEntity entity;
entity = new AndroidMultiPartEntity(
new AndroidMultiPartEntity.ProgressListener() {
@Override
public void transferred(long num) {
// publishProgress((int) ((num / (float) totalSize) * 100));
}
});
File sourceFile;
sourceFile = new File(compressImage(filePath));
Log.i("UploadApp", "file path: " + filePath);
// Adding file data to http body
entity.addPart("f", new FileBody(sourceFile)); //problem is here
entity.addPart("category", new StringBody("Bill"));
entity.addPart("description", new StringBody("test single"));
entity.addPart("file", new StringBody("unknown1"));
entity.addPart("clientid", new StringBody("4"));
totalSize = entity.getContentLength();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: " + statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
Log.e("UploadApp", "exception: " + responseString);
} catch (IOException e) {
responseString = e.toString();
Log.e("UploadApp", "exception: " + responseString);
}
return responseString;
}
我有一个用于上传图像的web服务,它有5个参数,其中4个是字符串,但是一个是字节数组(byte [] f)
主要问题:如何在字节数组中转换我的源文件(图像),以便在上面代码对应此Web服务的服务器上上传图像。
答案 0 :(得分:1)
答案 1 :(得分:0)
首先,从源文件中你可以获得绝对路径然后调用上传方法
String mCurrentPhotoPath = sourceFile.getAbsolutePath();
private String upload() {
Bitmap bm = BitmapFactory.decodeFile(mCurrentPhotoPath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 50, bao);
byte[] ba = bao.toByteArray();
return Base64.encodeToString(ba, Base64.DEFAULT);
}
答案 2 :(得分:0)
您可以使用此代码上传任何文件,仅发送带有点扩展名文件编码字符串的文件名
在服务器端发送编码字符串和Base64.decode文件字符串
String strAttachmentCoded = ""; private int PICK_PDF_REQUEST = 1; Uri filePath; @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_PDF_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) { filePath = data.getData(); File uploadFile = new File(filePath.toString()); URI uri = URI.create(uploadFile.getPath()); try { if (uploadFile != null) { File uploadFile1 = new File(uri); FileInputStream objFileIS = new FileInputStream(uploadFile1); ByteArrayOutputStream objByteArrayOS = new ByteArrayOutputStream(); byte[] byteBufferString = new byte[1024]; int readNum; readNum = objFileIS.read(byteBufferString); while (readNum != -1) { Log.v(" ", "" + readNum); objByteArrayOS.write(byteBufferString, 0, readNum); // system.out.println("read " + readNum + " bytes,"); readNum = objFileIS.read(byteBufferString); } byte[] byteBinaryData = Base64.encode(objByteArrayOS.toByteArray(), Base64.DEFAULT); strAttachmentCoded = String.valueOf(byteBinaryData); } } catch (Exception e) { e.printStackTrace(); } } }