android httpurlconnection 上传图片,通过HttpUrlConnection实现文件上传

本文详细介绍了HTTP POST请求中multipart/form-data格式的使用,特别是文件上传的过程。通过示例代码展示了如何设置请求头、定义分隔符、传递文件内容,并解释了不同部分的数据结构。着重讲解了文件上传时的Content-Disposition和Content-Type字段的设置,以及如何接收服务器响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

帮助文档

the user enters "Larry" in the text input, and selects the text file "file1.txt", the user agent might send back the following data:Content-Type: multipart/form-data; boundary=AaB03x

--AaB03x

Content-Disposition: form-data; name="submit-name"

Larry

--AaB03x

Content-Disposition: form-data; name="files"; filename="file1.txt"

Content-Type: text/plain

... contents of file1.txt ...

--AaB03x--

注意:网上编写的代码可能编译环境的不同,代码的表示也是不同的,再或者本身没有实现,一定找到规定书写的帮助文档,直接对照书写代码。

具体的代码实现:

String urlStr = " ";

URL url = new URL(urlStr);

HttpURLConnection http = (HttpURLConnection) url.openConnection();

http.setRequestMethod("POST");

//上传文件形式 "multipart/form-data"

http.setRequestProperty("Content-Type", "multipart/form-data; boundary=----footfoodapplicationrequestnetwork");

//boundary自定义的分隔符的类型

http.setRequestProperty(

"Accept",

"image/gif,   image/x-xbitmap,   image/jpg,   image/pjpeg,   application/vnd.ms-excel,   application/vnd.ms-powerpoint,                                  application/msword,   application/x-shockwave-flash,   application/x-quickviewplus,   */*");

http.setDoOutput(true);

http.setDoInput(true);

//若提交为post方式,需要修改为false

http.setUseCaches(false);

System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒

System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒

http.connect();

fin = new FileInputStream(filepath);

byte[] bytes = new byte[1024];

BufferedOutStream os = null;

//输入数据

os = new BufferedOutputStream(http.getOutputStream());

//注意格式  --boundary

//name="files"  (文件类型)filename="pic_1.jpg" (有很多地方是说是根路径,其实不是就是单纯的文件名)

buffer.append("------footfoodapplicationrequestnetwork\r\n");

buffer.append("Content-Disposition:form-data;name=\"files\";" );

buffer.append("filename=\"");

buffer.append("pic_1.jpg"+"\"");

buffer.append("\r\n");

buffer.append("Content-Type: image/jpg");   //如果是图片,或者音频,文本文件类型的那么还需要隔一行进行添加内容

buffer.append("\r\n\r\n");

os.write(buffer.toString().getBytes());

int bytelength;

while((bytelength=fin.read(bytes))!=-1){

os.write(bytes, 0, bytelength);

}

//文件传输结束格式:--boundary--

os.write("\r\n------footfoodapplicationrequestnetwork--\r\n".getBytes());

//接收网络返回内容

ins = http.getInputStream();

int size = ins.available();

byte[] jsonBytes = new byte[size];

ins.read(jsonBytes);

String message = new String(jsonBytes, "utf-8");

JSONObject demoJson = new JSONObject(message);

String media_id = demoJson.getString("media_id");

if(media_id!=null){

System.out.println(media_id);

return "图片上传成功!";

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值