文章转载:https://blog.csdn.net/wohaqiyi/article/details/77621517
import microservice.fpzj.control.base.BaseController;
import org.springframework.web.bind.annotation。*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io. *;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
private String uploadFilePdf(MultipartFile uploadFile){
String result="";
try {
// 换行符
final String newLine = "\r\n";
final String boundaryPrefix = "--";
// 定义数据分隔线
String BOUNDARY = "========7d4a6d158c9";
// 服务器的域名
URL url = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置为POST情
conn.setRequestMethod("POST");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求头参数
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
OutputStream out = new DataOutputStream(conn.getOutputStream());
// 上传文件
StringBuilder sb = new StringBuilder();
sb.append(boundaryPrefix);
sb.append(BOUNDARY);
sb.append(newLine);
// 文件参数,file参数名可以随意修改(根据你要上传的文件类型)
sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + uploadFile.getOriginalFilename()
+ "\"" + newLine);
sb.append("Content-Type:application/octet-stream");
// 参数头设置完以后需要两个换行,然后才是参数内容
sb.append(newLine);
sb.append(newLine);
// 将参数头的数据写入到输出流中
out.write(sb.toString().getBytes());
// 数据输入流,用于读取文件数据
DataInputStream in = new DataInputStream(uploadFile.getInputStream());
byte[] bufferOut = new byte[1024*8];
int bytes = 0;
// 每次读8KB数据,并且将文件数据写入到输出流中
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
// 最后添加换行
out.write(newLine.getBytes());
in.close();
// 定义最后数据分隔线,即--加上BOUNDARY再加上--。
byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine)
.getBytes();
// 写上结尾标识
out.write(end_data);
out.flush();
out.close();
// 定义BufferedReader输入流来读取URL的响应
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
result += line; //这里读取的是上边url对应的上传文件接口的返回值,读取出来后,然后接着返回到前端,实现接口中调用接口的方式
}
} catch (Exception e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
}
return result;
}
返回参数:JSON
原文:https://blog.csdn.net/xiaose_307/article/details/72887285
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
public class HttpRequestUtil {
/ **
*发送get请求
*
* @param requestUrl
*请求url
* @param requestHeader
*请求头
* @param responseEncoding
*响应编码
* @return页面响应html
* /
public static String sendGet(String requestUrl,Map <String,String> requestHeader,String responseEncoding){
String result =“”;
BufferedReader reader = null;
try {
if(requestUrl == null || requestUrl.isEmpty()){
return result;
}
URL realUrl =新的URL(requestUrl);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty(“accept”,“text / html,application / xhtml + xml,image / jxr,* / *”);
connection.setRequestProperty(“user-agent”,“Mozilla / 5.0(Windows NT 10.0; WOW64; rv:53.0)Gecko / 20100101 Firefox / 53.0”);
if(requestHeader!= null && requestHeader.size()> 0){
for(Entry <String,String> entry:requestHeader.entrySet()){
connection.setRequestProperty(entry.getKey(),entry.getValue());
}
}
connection.connect();
if(responseEncoding == null || responseEncoding.isEmpty()){
responseEncoding =“UTF-8”;
}
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),responseEncoding));
while((line = reader.readLine())!= null){
result + = line;
}
}赶上(例外五){
的System.out.println( “发送GET请求出现异常!”);
e.printStackTrace();
} finally {
try {
if(reader!= null){
reader.close();
}
}赶上(例外五){
e.printStackTrace();
}
}
返回结果;
}
/ **
*发送帖子请求
*
* @param requestUrl
*请求url
* @param requestHeader
*请求头
* @param formTexts
*表单数据
* @param文件
*上传文件
* @param requestEncoding
*请求编码
* @param responseEncoding
*响应编码
* @return页面响应html
* /
public static String sendPost(String requestUrl,Map <String ,String> requestHeader,Map <String,String> formTexts,Map <String,String> files,String requestEncoding,String responseEncoding){
OutputStream out = null;
BufferedReader reader = null;
String result =“”;
try {
if(requestUrl == null || requestUrl.isEmpty()){
返回结果;
}
URL realUrl =新的URL(requestUrl);
HttpURLConnection connection =(HttpURLConnection)realUrl.openConnection();
connection.setRequestProperty(“accept”,“text / html,application / xhtml + xml,image / jxr,* / *”);
connection.setRequestProperty(“user-agent”,“Mozilla / 5.0(Windows NT 10.0; WOW64; rv:53.0)Gecko / 20100101 Firefox / 53.0”);
if(requestHeader!= null && requestHeader.size()> 0){
for(Entry <String,String> entry:requestHeader.entrySet()){
connection.setRequestProperty(entry.getKey(),entry.getValue());
}
}
connection.setDoOutput(真);
connection.setDoInput(真);
connection.setRequestMethod( “POST”);
if(requestEncoding == null || requestEncoding.isEmpty()){
requestEncoding =“UTF-8”;
}
如果(的ResponseEncoding == NULL || responseEncoding.isEmpty()){
的ResponseEncoding = “UTF-8”;
}
if(requestHeader!= null && requestHeader.size()> 0){
for(Entry <String,String> entry:requestHeader.entrySet()){
connection.setRequestProperty(entry.getKey(),entry.getValue()) ;
}
}
if(files == null || files.size()== 0){
connection.setRequestProperty(“content-type”,“application / x-www-form-urlencoded”);
out = new DataOutputStream(connection.getOutputStream());
if(formTexts!= null && formTexts.size()> 0){
String formData =“”;
for(Entry <String,String> entry:formTexts.entrySet()){
formData + = entry.getKey()+“=”+ entry.getValue()+“&”;
}
formData = formData.substring(0,formData.length() - 1);
out.write(formData.toString()的getBytes(requestEncoding));
String boundary =“-----------------------------”+ String.valueOf(new Date()。getTime());
connection.setRequestProperty(“content-type”,“multipart / form-data; boundary =”+ boundary);
out = new DataOutputStream(connection.getOutputStream());
if(formTexts!= null && formTexts.size()> 0){
StringBuilder sbFormData = new StringBuilder();
for(Entry <String,String> entry:formTexts.entrySet()){
sbFormData.append(“ - ”+ boundary +“\ r \ n”);
sbFormData.append(“Content-Disposition:form-data; name = \”“+ entry.getKey()+”\“\ r \ n \ r \ n”);
sbFormData.append(entry.getValue()+“\ r \ n”);
}
out.write(sbFormData.toString()。getBytes(requestEncoding));
}
为(条目<字符串,字符串>项:files.entrySet()){
字符串文件名= entry.getKey();
String filePath = entry.getValue();
if(fileName == null || fileName.isEmpty()|| filePath == null || filePath.isEmpty()){
continue;
}
file file = new File(filePath);
if(!file.exists()){
continue;
}
out.write((“ - ”+ boundary +“\ r \ n”)。getBytes(requestEncoding));
out.write((“Content-Disposition:form-data; name = \”“+ fileName +”\“; filename = \”“+ file.getName()+”\“\ r \ n”)。getBytes( requestEncoding));
out.write((“Content-Type:application / x-msdownload \ r \ n \ r \ n”)。getBytes(requestEncoding));
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte [] bufferOut = new byte [1024];
while((bytes = in.read(bufferOut))!= -1){
out.write(bufferOut,0,bytes);
out.write(。( “\ r \ n”个)的getBytes(requestEncoding));
}
//out.write(("--“+ boundary +” - “)。getBytes(requestEncoding));这样写微信公众号开发上传素材有问题
out.write((” - “+ boundary +” - \ r \ n“个)的getBytes(requestEncoding));
}
out.flush();
out.close();
out = null;
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),responseEncoding));
字符串行;
while((line = reader.readLine())!= null){
result + = line;
}
}赶上(例外五){
的System.out.println( “发送POST请求出现异常!”);
e.printStackTrace();
} finally {
try {
if(out!= null){
out.close();
}
if(reader!= null){
reader.close();
}
}赶上(IOException异常前){
ex.printStackTrace();
}
}
返回结果;
}
}
![](/qrcode.jpg)