这篇文章主要介绍“Java http调用接口提交表单以及文件的方法”,在日常操作中,相信很多人在Java http调用接口提交表单以及文件的方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java http调用接口提交表单以及文件的方法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
Java HttpURLConnection 使用
/**
*
*/
package com.demo.java;
import java.io.DataInputStream;
import java.io.DataOutputStream;
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.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
* <p>描述:</p>
*
* @author saohuo
* @date 2019年11月4日
* @version
*/
public class MultipartFormDataPost {
// multipart/form-data; 表单各字段提交分隔符
private static String boundary = "--69695201314";
public void submit(Map<String, String> generalField, List<File> files) throws Exception {
HttpURLConnection connection = null;
OutputStream os = null;
URL url = new URL("http:");
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("content-type", "multipart/form-data;boundary="+boundary);
os = new DataOutputStream(connection.getOutputStream());
// 文本参数组装
StringBuffer textFormBody = new StringBuffer();
Map<String, String> formField = generalField;
formField.forEach((k,v) -> {
textFormBody.append("\r\n").append("--").append(boundary).append("\r\n");
textFormBody.append("Content-Disposition: form-data; name=\""+k+"\"");
textFormBody.append("\r\n\r\n");//名称与数据之间要有两个回车换行
textFormBody.append(v);
});
// 提交文本表单
os.write(textFormBody.toString().getBytes("UTF-8"));
// 文件数据
if (files != null && !files.isEmpty()) {
int fileIndex = 0;
for(File file : files) {
String filename = file.getName();
String fileMinetype = URLConnection.getFileNameMap().getContentTypeFor(filename);
StringBuffer fileFormBody = new StringBuffer();
fileFormBody.append("\r\n").append("--").append(boundary).append("\r\n");
fileFormBody.append("Content-Disposition: form-data; name=\"files["+fileIndex+"]\"; filename=\"" + filename + "\"");
fileFormBody.append("\r\n");
fileFormBody.append("Content-Type:" + fileMinetype);
fileFormBody.append("\r\n\r\n");
// 提交文件基本信息(文件名、文件长度、文件类型等)
os.write(fileFormBody.toString().getBytes());
InputStream fileStream = new FileInputStream(file);
DataInputStream fileDataIs = new DataInputStream(fileStream);
int filebytes = 0;
byte[] filebufferOut = new byte[1024];
while ((filebytes = fileDataIs.read(filebufferOut)) != -1) {
os.write(filebufferOut, 0, filebytes);
}
fileDataIs.close();
fileIndex++;
}
} // 文件写入结束
// 最后写入结束标识
byte[] endBodyData = ("\r\n--" + boundary + "--\r\n").getBytes();
os.write(endBodyData);
os.flush();
int responseCode = connection.getResponseCode();
// 提交结果状态码
System.out.println(responseCode);
}
}
到此,关于“Java http调用接口提交表单以及文件的方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4198095/blog/3125819