import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
* 模拟html表单提交数据。
*/
public class HttpMultipartRequest {
// 每个post参数之间的分隔
static final String BOUNDARY = "----------V2ymHFg03ehbqgZCaKO6jy";
public static void main(String[] args) {
Map<String,String> stringParams = new HashMap<String,String>();
stringParams.put("do", "justDoIt");
stringParams.put("myname", "aa我是一个测试cc123");
stringParams.put("description","bb我是码农我是一个测试我是一个测试cc567");
Map<String,File> fileParams = new HashMap<String,File>();
// fileParams.put("zhengqiuyijianhan", new File("D:\\ClientTcpSend.java"));
fileParams.put("yaoqinghanyiwen", new File("D:\\xxxx.xlsx"));
fileParams.put("yaoqinghan", new File("D:\\yyyyy.docx"));
HttpMultipartRequest req = new HttpMultipartRequest(
"http://localhost:8087/snptcfams/back/jsp/task/mockfilltask.html?saveflag=0", stringParams, fileParams);
try {
byte[] response = req.sendPost();
System.out.println(new String(response));
} catch (Exception e) {
e.printStackTrace();
}
}
// 连接的地址
private String urlStr;
// 文字post项集 strParams 1:key 2:value
private Map<String,String> strParams;
// 文件的post项集 fileParams 1:fileField, 2.fileName, 3.fileType, 4.filePath
private Map<String,File> fileParams;
/**
* 建立一个request连接
*
* @param urlStr
* @param strParams
* 1:key 2:value
* @param fileParams
* 1:key 2:value
*/
public HttpMultipartRequest(String urlStr, Map<String,String> strParams,
Map<String,File> fileParams) {
this.urlStr = urlStr;
this.strParams = strParams;
this.fileParams = fileParams;
}
/**
* 向服务器发送post请求
*
* @return byte[]
* @throws Exception
*/
public byte[] sendPost() throws Exception {
// http连接器
HttpURLConnection hc = null;
// byte输出流,用来读取服务器返回的信息
ByteArrayOutputStream bos = null;
// 输入流,用来读取服务器返回的信息
InputStream is = null;
// 保存服务器返回的信息的byte数组
byte[] res = null;
try {
URL url = new URL(urlStr);
hc = (HttpURLConnection) url.openConnection();
hc.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY);
hc.setRequestProperty("Charsert", "UTF-8");
// 发送POST请求必须设置如下两行
hc.setDoOutput(true);
hc.setDoInput(true);
hc.setUseCaches(false);
hc.setRequestMethod("POST");
OutputStream dout = hc.getOutputStream();
// //1.先写文字形式的post流
// 头
String boundary = BOUNDARY;
// 中
StringBuffer resSB = new StringBuffer("\r\n");
// 尾
String endBoundary = "\r\n--" + boundary + "--\r\n";
// strParams 1:key 2:value
if (strParams != null) {
for (String key : strParams.keySet()) {
String value = strParams.get(key);
resSB.append("--").append(boundary).append("\r\n")
.append("Content-Disposition: form-data; name=\"")
.append(key).append("\"\r\n").append("\r\n")
.append(value).append("\r\n");
}
}
String boundaryMessage = resSB.toString();
System.out.println("boundaryMessage:" + boundaryMessage);
// 写出流
dout.write(boundaryMessage.getBytes("utf-8"));
// 2.再写文件开式的post流
// fileParams 1:fileField, 2.fileName, 3.fileType, 4.filePath
resSB = new StringBuffer();
if (fileParams != null) {
for (String fileField:fileParams.keySet()) {
File tempFile = fileParams.get(fileField);
String filePath = tempFile.getPath();
// System.out.println("tempFile:" + tempFile +"\r\n"
// + "fileName:" + fileName +"\r\n"
// + "filePath:" + filePath +"\r\n"
// );
resSB.append("--").append(boundary).append("\r\n")
.append("Content-Disposition: form-data; name=\"")
.append(fileField).append("\"; filename=\"")
.append(filePath).append("\"\r\n")
.append("Content-Type:application/octet-stream\r\n\r\n")
.append("\r\n\r\n");
dout.write(resSB.toString().getBytes("utf-8"));
// 开始写文件
File file = new File(filePath);
DataInputStream in = new DataInputStream(
new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024 * 5];
while ((bytes = in.read(bufferOut)) != -1) {
dout.write(bufferOut, 0, bytes);
}
in.close();
}
}
// 3.最后写结尾
dout.write(endBoundary.getBytes("utf-8"));
dout.close();
int ch;
is = hc.getInputStream();
bos = new ByteArrayOutputStream();
while ((ch = is.read()) != -1) {
bos.write(ch);
}
res = bos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bos != null)
bos.close();
if (is != null)
is.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return res;
}
}
模拟form表单提交(包含文件上传)
最新推荐文章于 2024-03-22 12:49:19 发布