如何在android中使用Volley库上传文件?
Posted
技术标签:
【中文标题】如何在android中使用Volley库上传文件?【英文标题】:How to upload file using Volley library in android? 【发布时间】:2015-11-22 15:07:16 【问题描述】:我已经有一个 Request<JSONObject>
的子类,用于将 http post 发送到服务器。问题是,我不知道如何为文件添加参数。将字符串发布到服务器很容易。但我需要将文件添加为不同的参数。我该怎么做?
public class AddNewPetRequest extends Request<JSONObject>
private Response.Listener<JSONObject> listener;
public AddNewPetRequest(String url, Map<String, String> params,
Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener)
super(Request.Method.GET, url, errorListener);
this.listener = reponseListener;
this.params = params;
public AddNewPetRequest(int method, String url, Map<String, String> params,
Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener)
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError
return params;
;
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response)
try
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
catch (UnsupportedEncodingException e)
return Response.error(new ParseError(e));
catch (JSONException je)
return Response.error(new ParseError(je));
@Override
protected void deliverResponse(JSONObject response)
// TODO Auto-generated method stub
listener.onResponse(response);
更新问题: 我在***中遵循了其中一个答案的模式,我想出了这个实现:
public class MultipartRequest extends Request<String>
private MultipartEntity entity = new MultipartEntity();
private final Response.Listener<String> mListener;
private HashMap<String, String> mParams;
public MultipartRequest(String url, Response.ErrorListener errorListener, Response.Listener<String> listener)
super(Method.POST, url, errorListener);
mListener = listener;
buildMultipartEntity();
private void buildMultipartEntity()
entity.addPart("profile_picture", new FileBody(new File("/storage/emulated/0/Pictures/VSCOCam/2015-07-31 11.55.14 1.jpg")));
try
entity.addPart("user_id", new StringBody("15"));
entity.addPart("name", new StringBody("Bogs"));
entity.addPart("gender", new StringBody("Male"));
entity.addPart("date_birth", new StringBody("1999-12-5"));
entity.addPart("breed", new StringBody("monkey"));
catch (UnsupportedEncodingException e)
VolleyLog.e("UnsupportedEncodingException");
@Override
public String getBodyContentType()
return entity.getContentType().getValue();
@Override
public byte[] getBody() throws AuthFailureError
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try
entity.writeTo(bos);
catch (IOException e)
VolleyLog.e("IOException writing to ByteArrayOutputStream");
return bos.toByteArray();
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response)
return Response.success("Uploaded", getCacheEntry());
@Override
protected void deliverResponse(String response)
mListener.onResponse(response);
当我将此请求添加到我的请求队列时,它以 com.android.volley.TimeoutError
响应,但如果检查数据库,请求将执行并将项目添加到表中,但个人资料图片上传只有 1 个字节的大小。另一个问题,我的数据库项添加了两次。
【问题讨论】:
在这里查看:- ***.com/a/19981901/4018207 您可以阅读my question and answer here。希望这会有所帮助! @MamataGelanee 如何获得 MultipartEntity 类?我通过我的 gradle compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1' 导入这个,但似乎 MultipartEntity 仍然丢失。跨度> @Earwin delos Santos :尝试将所有库添加到 gradle -> apache-mime4j、httpclient、httpcore 和 httpmime。我认为“MultipartEntity”在 httpmime 中。 添加这个:'org.apache.httpcomponents:httpmime:4.5' 【参考方案1】:希望这个方法对你有帮助:
public int uploadFile(String sourceFileUri, String fileName)
String upLoadServerUri = "your_api_url";
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
//errMsg=Environment.getExternalStorageDirectory().getAbsolutePath();
if (!sourceFile.isFile())
Log.e("uploadFile", "Source File Does not exist");
return 0;
try
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
//conn.setRequestProperty("pid", "4");
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available(); // create a buffer of maximum size
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode != 200)
getActivity().runOnUiThread(new Runnable()
public void run()
Toast.makeText(context, "Il y a une erreur l'hors du trasfert de l'image.", Toast.LENGTH_SHORT).show();
);
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
catch (MalformedURLException ex)
// dialog.dismiss();
ex.printStackTrace();
Toast.makeText(context, "MalformedURLException", Toast.LENGTH_SHORT).show();
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
catch (Exception e)
// dialog.dismiss();
e.printStackTrace();
Toast.makeText(context,errMsg+ "Exception : " + e.getMessage(), Toast.LENGTH_SHORT).show();
Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e);
// dialog.dismiss();
return serverResponseCode;
【讨论】:
【参考方案2】:我们需要执行一个多部分请求。但问题是 volley 不直接支持多部分请求。这就是为什么我们需要创建自定义排球请求。
使用 VolleyMultipartRequest 创建一个 java 类,并在该文件中编写以下代码。
private void uploadImageFile(File file) throws IOException
file = new Compressor(this).compressToFile(file);
RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);
// MultipartBody.Part is used to send also the actual filename
MultipartBody.Part body = MultipartBody.Part.createFormData("avatar", file.getName(), requestFile);
ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class);
Call<ServerResponse> call = getResponse.uploadFile("Bearer "+jsonToken, body);
call.enqueue(new Callback< ServerResponse >()
@Override
public void onResponse(@NonNull Call < ServerResponse > call, @NonNull retrofit2.Response<ServerResponse> response)
ServerResponse serverResponse = response.body();
if (serverResponse.getData() != null)
Log.e(TAG, "Response is "+ serverResponse.getData());
loading.setVisibility(View.GONE);
Toast.makeText(ProfileSettings.this, "Avatar updated", Toast.LENGTH_SHORT).show();
else
Log.e("Response", String.valueOf(serverResponse));
@Override
public void onFailure(Call < ServerResponse > call, Throwable t)
Log.e(TAG, t.getMessage());
);
// Log.e(TAG, "request is "+call.request().body()+" and "+call.request().headers());
【讨论】:
以上是关于如何在android中使用Volley库上传文件?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android 中使用 Volley 库进行肥皂发布请求
如何使用 Gradle 替换 Android Volley 库?
既然 apache 的 MultipartEntityBuilder 已被弃用,那么使用 Volley 库上传图像或文件的最佳方式是啥?