您的位置:首页 > 移动开发 > Android开发

善用搜索引擎--使用Volley上传multipart/form-data数据

2017-04-15 18:54 751 查看
学会善用搜索引擎,对我们平时解决一些问题,很有帮助,同时仔细查看别人的解决思路,也容易形成自己的知识体系。

现在单就针对一个技术问题,进行搜索解决。

问题:如何使用Volley上传二进制数据(要求传递multipart/form-data类型)?

解决步骤:

1、提炼关键词:volley、multipart/form-data、post请求、二进制数据

2、先搜索post请求

我找到了post的四种提交方式



然后,具体看看multipart/form-data格式



这里发现multipart/form-data一次可以上传多个文件,文件之间以–boundary分隔符进行分割,同时还要根据ApI字段和文件类型,设置Content-Disposition、Content-Type。这个请求就算构建成功了。

3、在stackoverflow上搜索Volley multipart/form-data

我找到了下面这篇文章:http://stackoverflow.com/questions/16797468/how-to-send-a-multipart-form-data-post-in-android-with-volley

里面讲到了用volley对multipart/form-data进行封装,同时也涉及到了文件二进制流的转换。



根据题主的解决方法,再结合自己的需要,我先对文件进行二制流的转换,然后用volley构建multipart/form-data,最终解决了这个问题。

需要注意的是:请求头不用再说明Content-Type类型,直接在请求体中说明即可。

具体代码如下:

1、volley封装

public void uploadPhoto(String url, String TAG, int method, final HashMap<String, Object> params, final String filename, Response.Listener<JSONObject> res, Response.ErrorListener err) {

JsonObjectRequest req = new JsonObjectRequest(method, url, res, err) {

private final String twoHyphens = "--";
private final String lineEnd = "\r\n";
private final String boundary = "--------#HduinClient5258RedHome"; // 分隔符

@Override
public String getBodyContentType() {
return "multipart/form-data;boundary=" + boundary;
}

@Override
public byte[] getBody() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
// 构建请求体
try{
for (Map.Entry<String, Object> entry : params.entrySet()) {
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"; filename=\""
+ filename + "\"" + lineEnd);
dos.writeBytes(lineEnd);

ByteArrayInputStream fileInputStream = new ByteArrayInputStream((byte[]) entry.getValue());
int bytesAvailable = fileInputStream.available();

int maxBufferSize = 1024 * 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];

// read file and write it into form...
int 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);
}

dos.writeBytes(lineEnd);
}
// 结束标记
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
}catch (IOException e){
e.printStackTrace();
}

return bos.toByteArray();
}

@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
volleyError = getVolleyError(volleyError);

return volleyError;
}
};
req.setRetryPolicy(new DefaultRetryPolicy(5000, 0, 0));
req.setTag(TAG);
requestQueue.add(req);
}


2、图片文件二进制流转换(kotlin语言)

fun getByteDataFromBitmap(url: String, quality: Int): ByteArray {
val out = ByteArrayOutputStream()
val bitmap = ImageCompressUtil.compressImage(url)
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, out)
try {
out.flush()
out.close()
} catch (ex: IOException) {
ex.printStackTrace()
}

return out.toByteArray()
}


以上就是通过搜索引擎解决技术问题的例子了!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息