我正在使用一个应用程序,我正在尝试使用android volley和SimpleMultiPartRequest上传文件到服务器,我的文件成功上传到服务器,我收到文件URL但扩展名为xyz.octet-stream,无论文件是什么。下面是SimpleMultiPartRequest的代码。
SimpleMultiPartRequest request = new SimpleMultiPartRequest(methodType, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
uploadSettable.set(s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
uploadSettable.setException(volleyError);
Log.v(TAG,volleyError.toString());
}
}){
@Override
public Map<String, String> getFilesToUpload() {
Map<String,String> map = new HashMap<>();
map.put("FileData",filePath);
return map;
}
@Override
public int getMethod() {
return Method.POST;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> map = new HashMap<>();
map.put(PrefUtils.AUTHORIZATION_KEY, "Bearer " + PrefUtils.getString(PrefUtils.PREF_UTILS_ACCESS_TOKEN,"N/A",ArkaaApplicationClass.getInstance().getBaseContext()));
return map;
}
@Override
public void onProgress(final long transferredBytes, final long totalSize) {
fileSize = totalSize;
super.onProgress(transferredBytes, totalSize);
new Thread(new Runnable() {
@Override
public void run() {
if(progressBarStatus < totalSize) {
// performing operation
progressBarStatus = (int)((transferredBytes*100)/totalSize);
try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
// Updating the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
// performing operation if file is downloaded,
if (progressBarStatus >= totalSize) {
// sleeping for 1 second after operation completed
try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
// close the progress bar dialog
progressBar.dismiss();
}
}
}).start();
}
};
request.setShouldCache(false);
request.setRetryPolicy(new DefaultRetryPolicy(10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
ArkaaApplicationClass.getInstance().addToRequestQueue(request);
用它来发送请求:
// FILE_KEY is your key
// mImagePath is your absolute file path
request.addFile(FILE_KEY,mImagePath);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);