Java模拟Form表单提交带⽂件调⽤接⼝
import java.io.*;
import java.HttpURLConnection;
import java.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestDemo1 {
//boundary 的值随机即可
static String boundary = "----WebKitFormBoundaryeIAzkhBx5AozTHAY";
static String prefix = "--";
static String newLine = "\r\n";
public static void main(String[] args) {
test();
}
private static void test() {
try {
URL url = new URL("localhost:8090");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setConnectTimeout(10000);
connection.setReadTimeout(60000);
connection.setRequestMethod("POST");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"); connection.setRequestProperty("Charsert", "UTF-8");
connection.setRequestProperty("Content-type", "multipart/form-data;boundary=" + boundary);
OutputStream());
InputStream ins = InputStream();
byte[] b = readBuffer(ins);
System.out.println(new String(b,"UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
private static void ConfigHttpMultipart(final OutputStream out) throws Exception{
StringBuffer params = new StringBuffer();
Map<String, String> textMap = initTextMap();
for (Map.Entry<String, String> map : Set()) {
// 注意换⾏符或--不要太多,太多导致数据⽆法传输
params.append(newLine + prefix + boundary + newLine);
params.append("Content-Disposition: form-data; name=\"" + Key() + "\"");
params.append(newLine + newLine);
params.Value());
}
// 隔开⽂本与⽂件
params.append(newLine);
out.String().getBytes("UTF-8"));
List<String> fileList = initFileList();
for(int i = 0; i < fileList.size(); i++){
//添加参数file
File file = new (i));
StringBuffer sb = new StringBuffer();
sb.append(newLine + prefix + boundary + newLine);
sb.append("Content-Disposition: form-data; name=\"files\"; filename=\"" + Name() + "\"");
sb.append(newLine);
sb.append("Content-Type: multipart/form-data; boundary=" + boundary);
sb.append(newLine + newLine);
out.String().getBytes("utf-8"));
InputStream in = new FileInputStream(file);
byte b[] = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
in.close();
}
byte[] end_data = (newLine + prefix + boundary + prefix + newLine).getBytes();
out.write(end_data);
out.flush();
printformout.close();
}
private static List<String> initFileList() {
List<String> list = new ArrayList<String>();
list.add("F:\\⼯作\\2021-07\\ToExcel.xlsx");
list.add("F:\\⼯作\\2021-07\\print.xlsx");
return list;
}
private static Map<String, String> initTextMap() {
Map<String, String> param = new HashMap<String, String>();
param.put("userAccount", "ceshi");
return param;
}
public static byte[] readBuffer(final InputStream ins) throws IOException { byte b[] = new byte[1024];
ByteArrayOutputStream stream = new ByteArrayOutputStream();
int len = 0;
while ((len = ad(b)) != -1) {
stream.write(b, 0, len);
}
ByteArray();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论