使用HTTP multipart进行文件上传数据包举例

POST /load/servlet/upload.do HTTP/1.1
accept:text/*
content-type:multipart/form-data; boundary=----------Ef1KM7GI3Ef1ei4Ij5ae0KM7cH2KM7
user-agent:Shockwave Flash
host:192.168.1.237:8080
content-length:555
connection:Keep-Alive
cache-control:no-cache
cookie:theworld_client_delete=theworld_client_delete

 

------------Ef1KM7GI3Ef1ei4Ij5ae0KM7cH2KM7
Content-Disposition: form-data; name="Filename"

 

鑺傜洰娓呭崟.txt
------------Ef1KM7GI3Ef1ei4Ij5ae0KM7cH2KM7
Content-Disposition: form-data; name="Filedata"; filename="鑺傜洰娓呭崟.txt"
Content-Type: application/octet-stream

 

锘�2009-11-06
05:30-05:59 1 鍔ㄧ敾涓栫晫
06:00-06:29 1 浣撹偛鏂伴椈
06:30-06:59 1 浜轰笌鑷劧
07:00-07:30 1 鏂伴椈鑱旀挱
------------Ef1KM7GI3Ef1ei4Ij5ae0KM7cH2KM7
Content-Disposition: form-data; name="Upload"

 

Submit Query
------------Ef1KM7GI3Ef1ei4Ij5ae0KM7cH2KM7--

 

说明:

1。multipart消息体各段均使用"\r\n"+"--"+boundary开始

2。multipart的http包体结束时,必须是"\r\n"+"--"+boundary+"--";

某些HTTP服务器要求是"\r\n"+"--"+boundary+"--\r\n"

像apache的HttpClient上传的文件,multipart的http包体结束时就是"\r\n"+"--"+boundary+"--\r\n",

但flex组件上传文件的http包体结束时就不含有"\r\n"

3.content-length说明了http包体的大小

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用HttpURLConnection来实现multipart/form-data POST上传文件。下面是一个示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class FileUploader { public static void main(String[] args) throws Exception { String apiUrl = "http://example.com/upload"; String filePath = "/path/to/file"; uploadFile(apiUrl, filePath); } public static void uploadFile(String apiUrl, String filePath) throws Exception { URL url = new URL(apiUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=boundary"); File file = new File(filePath); String fileName = file.getName(); FileInputStream inputStream = new FileInputStream(file); OutputStream outputStream = conn.getOutputStream(); outputStream.write(("--boundary\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"\r\n" + "Content-Type: application/octet-stream\r\n\r\n").getBytes()); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.write(("\r\n--boundary--").getBytes()); outputStream.flush(); outputStream.close(); int responseCode = conn.getResponseCode(); System.out.println("Response Code: " + responseCode); } } ``` 在示例代码中,首先指定了上传文件的API地址和文件路径。然后通过HttpURLConnection打开连接,并设置请求方法为POST,Content-Type为multipart/form-data。接着读取文件内容,将文件内容写入请求的OutputStream中。最后获取响应码,如果响应码为200,则说明文件上传成功。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值