String url = "接口地址";
Connection connection = Jsoup.connect(url).ignoreContentType(true);
//设置请求头
connection.header("Content-Type", "multipart/form-data");
//传文件参数
File file = new File("D:/测试文件.txt");
InputStream inputStream = new FileInputStream(file);
connection.data("接口参数名", "文件名", inputStream);
//POST请求
connection.method(Connection.Method.POST).execute();
废话不多说,主要是以下三点:
①设置Content-Type请求头的值为 multipart/form-data
②使用connection.data("参数名", "文件名", inputStream);进行参数传递
③Jsoup版本不能太老,最好是用比较新的版本,我用的是1.13.1