Android的多部分上传与图像列表 [英] Android multipart upload with image list

查看:120
本文介绍了Android的多部分上传与图像列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个图像列表,我想上传到服务器

There is a image list I would like to upload to the server

我使用httpmime,这里是code:

I am using httpmime , and here is the code:

for(File file : gs.id_img) {
            builder.addPart("id_img[]", new FileBody(file)); 
        }

但似乎无法获得岗位价值的后端,如何解决呢?谢谢

But in the backend it seems can not get the post value, how to fix it? thanks

推荐答案

你有工作像这样

public void connectForMultipart() throws Exception {
    con = (HttpURLConnection) ( new URL(url)).openConnection();
    con.setRequestMethod("POST");
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setRequestProperty("Connection", "Keep-Alive");
    con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    con.connect();
    os = con.getOutputStream();
}

public void addFormPart(String paramName, String value) throws Exception {
    writeParamData(paramName, value);
}

public void addFilePart(String paramName, String fileName, byte[] data) throws Exception {
    os.write( (delimiter + boundary + "\r\n").getBytes());
    os.write( ("Content-Disposition: form-data; name=\"" + paramName +  "\"; filename=\"" + fileName + "\"\r\n"  ).getBytes());
    os.write( ("Content-Type: application/octet-stream\r\n"  ).getBytes());
    os.write( ("Content-Transfer-Encoding: binary\r\n"  ).getBytes());
    os.write("\r\n".getBytes());

    os.write(data);

    os.write("\r\n".getBytes());
}   
public void finishMultipart() throws Exception {
    os.write( (delimiter + boundary + delimiter + "\r\n").getBytes());
}

我已经改变了内容类型:应用程序/八位字节流\ r \ N内容类型:图像/ JPEG \ r \ ñ\ r \ñ和它的工作:)

这篇关于Android的多部分上传与图像列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆