实际上,我与volley library
multipart
合作,将图片上传到服务器。
完成这项任务。
我能够成功将图像上传到服务器但现在我遇到的问题是服务器Image
看起来像textfile
它不是实际的图像格式...... < / p>
我正在努力解决这个问题,任何人都可以帮助我或建议我解决这个问题....
这是我的自定义请求类:
public class MyCustomRequest<T> extends Request<T>{
private static final String KEY_PICTURE = "file";
private static final String TAG = "MutlipartRequest";
private HttpEntity mHttpEntity;
private final Class mClass;
private Response.Listener<T>mListener;
private Map mHeaders;
private Class<T> clazz;
private final Gson gson;
public MyCustomRequest(String url, File file,
Class clazz,
Map headers,
Response.Listener listener,
Response.ErrorListener errorListener) {
super(Request.Method.POST, url, errorListener);
mHeaders = headers;
mClass = clazz;
mListener = listener;
gson = new Gson();
mHttpEntity = buildMultipartEntity(file);
}
private HttpEntity buildMultipartEntity(String path) {
File file = new File(path);
return buildMultipartEntity(file);
}
private HttpEntity buildMultipartEntity(File file) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
String fileName = file.getName();
builder.addBinaryBody(KEY_PICTURE, file, ContentType.create("image/*"), fileName);
return builder.build();
}
@Override
public Map getHeaders() throws AuthFailureError {
return mHeaders != null ? mHeaders : super.getHeaders();
}
@Override
public String getBodyContentType() {
return mHttpEntity.getContentType().getValue();
}
@Override
public byte[] getBody() throws AuthFailureError {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
mHttpEntity.writeTo(bos);
} catch (IOException e) {
VolleyLog.e("IOException writing to ByteArrayOutputStream");
}
return bos.toByteArray();
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
String json;
try {
json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
Log.e(TAG, String.format("Encoding problem parsing API response. NetworkResponse:%s", response.toString()), e);
return Response.error(new ParseError(e));
}
try {
return Response.success(gson.fromJson(json, mClass), HttpHeaderParser.parseCacheHeaders(response));
} catch (JsonSyntaxException e) {
Log.e(TAG, String.format("Couldn't API parse JSON response. NetworkResponse:%s", response.toString()), e);
Log.e(TAG, String.format("Couldn't API parse JSON response. Json dump: %s", json));
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(T response) {
mListener.onResponse(response);
}
}
这是我用来从我的活动上传图片的方法:
private void uploadImage( String id ){
String photoUploadUrl = "https://merchantconsole.autogard.io/carservice/api/repair-requests/" + id + "/photos/upload";
if (_photoPath.length() == 0){
closeProgress();
}
if (getIntent().getBooleanExtra("isPart", false)){
photoUploadUrl = "https://merchantconsole.autogard.io/carservice/api/repair-requests/" + id + "/photos/upload";
}
HashMap<String, String> headers = new HashMap<String , String>();
String token = Preference.getInstance().getValue(SubmitActivity.this, PreConst.PREFKEY_TOKEN, "");
String boundary = "-------------" + System.currentTimeMillis();
Log.d("token==========>", token);
headers.put("Content-Type", "multipart/form-data; boundary="+boundary);
headers.put(ReqConst.PRAM_AUTHENTICATION, token);
try {
File file = new File(_photoPath);
Map<String, String> params = new HashMap<String, String>();
params.put("id", "notuseful");
// params.put(ReqConst.PARAM_IMAGE_TYPE, String.valueOf(0));
// String url = ReqConst.SERVER_URL + ReqConst.REQ_UPLOADIMAGE;
Log.d("photoUploadUrl=====>", photoUploadUrl);
MultiPartRequest reqMultiPart = new MultiPartRequest(photoUploadUrl, headers, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
closeProgress();
Log.d("error====>", error.toString());
}
}, new Response.Listener<String>() {
@Override
public void onResponse(String json) {
gotoDashboardActivity();
Log.d("response============>", json);
}
}, file, ReqConst.PRAM_FILE, params);
reqMultiPart.setRetryPolicy(new DefaultRetryPolicy(
Constants.VOLLEY_TIME_OUT, 0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AutogardApplication.getInstance().addToRequestQueue(reqMultiPart, photoUploadUrl);
} catch (Exception e) {
e.printStackTrace();
closeProgress();
}