利用HttpURLConnection发送post请求上传多个文件
本文要用java.net.HttpURLConnection来实现多个文件上传
1. 研究 form 表单到底封装了什么样的信息发送到servlet。
假如我参数写的内容是hello word,然后二个文件是二个简单的txt文件,form提交的信息为:
-----------------------------7da2e536604c8
Content-Disposition: form-data; name="username" hello word
-----------------------------7da2e536604c8
Content-Disposition: form-data; name="file1"; filename="D:/haha.txt"
Content-Type: text/plain haha
hahaha
-----------------------------7da2e536604c8
Content-Disposition: form-data; name="file2"; filename="D:/huhu.txt"
Content-Type: text/plain messi
huhu
-----------------------------7da2e536604c8--
研究下规律发现有如下几点特征
1.第一行是“ -----------------------------7d92221b604bc ”作为分隔符,然后是“ /r/n ” 回车换行符。 这个7d92221b604bc 分隔符浏览器是随机生成的。
2.第二行是Content-Disposition: form-data; name="file2"; filename="D:/huhu.txt";name=对应input的name值,filename对应要上传的文件名(包括路径在内),
3.第三行如果是文件就有Content-Type: text/plain;这里上传的是txt文件所以是text/plain,如果上穿的是jpg图片的话就是image/jpg了,可以自己试试看看。
然后就是回车换行符。
4.在下就是文件或参数的内容或值了。如:hello word。
5.最后一行是-----------------------------7da2e536604c8--,注意最后多了二个--;
有了这些就可以使用HttpURLConnection来实现上传文件功能了
private void upload(String[] uploadFiles, String actionUrl) {
String end = "/r/n";
String twoHyphens = "--";
String boundary = "*****";
try {
URL url = new URL(actionUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// 发送POST请求必须设置如下两行
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream ds =
new DataOutputStream(con.getOutputStream());
for (int i = 0; i < uploadFiles.length; i++) {
String uploadFile = uploadFiles[i];
String filename = uploadFile.substring(uploadFile.lastIndexOf("//") + 1);
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " +
"name=/"file" + i + "/";filename=/"" +
filename + "/"" + end);
ds.writeBytes(end);
FileInputStream fStream = new FileInputStream(uploadFile);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
while ((length = fStream.read(buffer)) != -1) {
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
/* close streams */
fStream.close();
}
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
ds.flush();
// 定义BufferedReader输入流来读取URL的响应
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
String s = b.toString();
if (s.contains("successfully")) {
// for (int i = 1; i < 5; i++) {
int beginIndex = s.indexOf("url =") + 5;
int endIndex = s.indexOf("/n", beginIndex);
String urlStr = s.substring(beginIndex, endIndex).trim();
System.out.println(urlStr);
// }
}
ds.close();
} catch (Exception e) {
}
}
利用HttpURLConnection发送post请求上传多个文件的更多相关文章
- el-upload控件一次接口请求上传多个文件
el-upload组件默认情况下上传多少个文件就会请求多少次上传接口,如何一次上传多个文件而不必多次请求上传接口呢?直接看代码 html <el-upload :action="act ...
- elementUI一次请求上传多个文件
elementui <el-upload class="upload-demo" ac ...
- Java 利用HttpURLConnection发送http请求
写了一个简单的 Http 请求的Class,实现了 get, post ,postfile package com.asus.uts.util; import org.json.JSONExcepti ...
- python发送post请求上传文件,无法解析上传的文件
前言 近日,在做接口测试时遇到一个奇葩的问题. 使用post请求直接通过接口上传文件,无法识别文件. 遇到的问题 以下是抓包得到的信息: 以上请求是通过Postman直接发送请求的. 在这里可以看到消 ...
- Java客户端通过Http发送POST请求上传文件到web服务器
http://www.cnblogs.com/WilliamJiang/archive/2012/04/29/2475883.html 1.朋友的一个需求,让我给他实现,需求是这样的,需要用ASP.n ...
- 利用django如何解析用户上传的excel文件
https://www.jb51.net/article/119452.htm 前言 我们在工作中的时候,会有这种需求:用户上传一个格式固定excel表格到网站上,然后程序负债解析内容并进行处理.我最 ...
- HttpURLConnection 发送http请求帮助类
java 利用HttpURLConnection 发送http请求 提供GET / POST /上传文件/下载文件 功能 import java.io.*; import java.net.*; im ...
- SSM框架下,使用ajax请求上传文件(doc\docx\excel\图片等)
1.准备工作 1.1.添加上传必要jar包 <dependency> <groupId>commons-io</groupId> <artifactId> ...
- 【JAVA】通过HttpURLConnection 上传和下载文件(二)
HttpURLConnection文件上传 HttpURLConnection采用模拟浏览器上传的数据格式,上传给服务器 上传代码如下: package com.util; import java.i ...
随机推荐
- 阿里巴巴Java开发手册及Java代码规约扫描eclipse插件
一.github地址: https://github.com/alibaba/p3c 二..eclipse插件的安装 此处示例采用eclipse,版本为 Neon.1 Release RC3 (4.6 ...
- HTTP.ContentType
1. multipart/x-mixed-replace http://blog.dubbelboer.com/2012/01/08/x-mixed-replace.html
- Mac下配置域名和网站测试环境
一.在 /etc/hosts 下配置相关域名 1, control+space 打开spotlight, 搜索“terminal” 2, 打开Terminal 3, 在terminal界面中输入 ...
- 超详细的PS抠图方法
步骤: 1.打开图片,根据图片的特点选择抠图工具: 2.在图像上找到第一个定点,要求定点要完全暴露在画布中,并且是清晰可见的顶点: 3.抠取图像时,多边形套索的定点以及边线应该向内1-2个像素,为了避 ...
- hdfs结构
hdfs文件系统主要由四部分组成:client客户端.namenode.datanode.secondary namenode. client:1.分割文件成block. 2.与namenode交 ...
- Elastix 安装G729 G723语音编码
下載適合自己機器及軟體版本的模組檔,基本上略分為 pentium/pentium2/pentium3/x86_64,Asterisk 1.2/1.4/1.6.前往 http://asterisk.ho ...
- 【Game】2048小游戏
每个男孩都有一个游戏梦吧,本例简单讲述一款很火的游戏<2048>的制作. 本例参考地址:https://www.imooc.com/learn/76 游戏准备 1.游戏的逻辑(2048大家 ...
- 【轻松前端之旅】<!DOCTYPE>标签
前端学习,先学习HTML,CSS,Javascript HTML - HyperText Markup Language HTML-超文本标记语言,提供了一种标记网页内容的方法. 浏览器怎么知道如何显 ...
- Java第11章笔记
什么是类,什么是对象 举例说明什么是类,什么是对象? 一句话:万物皆对象 类的概念:类是具有相同属性和服务的一组对象的集合. 1.为属于该类的所有对象提供了统一的抽象描述,其内部包括属性和服务两个部分 ...
- unity3DGI
Realtime GI,实时全局光照, 1.构成 : 可实时更新的lightmap + 可实时更新的光照探头(light probe)+ 可实时更新的cubemap(Reflection probe) ...