java服务器间http通讯,同时传输⽂件流和数据,并接收返回的
⽂件流或数据
废话:这⾥使⽤的是HttpUrlConnection,httpclient没有试过,这篇⽂章也是收集了很多⼤佬的成果,但是由于太久远了不到原⽂了,如果有⼤佬看到提醒⼀下,愿意贴上原⽂链接的哈,抱歉抱歉,现在实现了同时传输⽂件和数据,但是response返回的⽂件和数据只能接收⼀个,如果⼤家有同时接收的⽅法,望告知,谢谢⼤家了。
需求:其实就是服务器间通过http进⾏通讯,不⾛前端的那种,公司需求给某个⽹址主动发⽂件流,并且接收response中的反馈⽂件流或者错误信息。
连接⼯具类HttpUrlConnection.java,现在⽀持传多个数据,⼀个⽂件,如果需要多个⽂件,照着改改应该没问题的
@Component
public class HttpUrlConnection {
//头部格式
private static final String nextLine = "\r\n";
private static final String twoHyphens = "--";
//随便写⼀个
private static final String boundary = java.util.UUID.randomUUID().toString();
/**
* @Description:java项⽬的服务端之间的通信
* @Param: [requestUrl,请求url
* jsessionId, 浏览器的访问的Cookie,即被访问的服务端的session。若被访问的服务器没有做url过滤器,则该参数可以为null。
* file, 发送出去的⽂件
* feedbackFile, 收到的反馈⽂件存放位置
* name,对⽅⽤来接收⽂件的名字
* params,要传的参数
* @Return: java.util.Map
* @Author: Liting
* @Date: 2019-11-26 08:54
*/
public static Map httpUrlConnection(String requestUrl, String jsessionId, File file, String feedbackFile, String name, Map<String,String> params) throws IOException {        HttpURLConnection con = (HttpURLConnection) new URL(requestUrl).openConnection();
if (!Utils.isEmpty(jsessionId)) {
con.setRequestProperty("Cookie", "JSESSIONID=" + jsessionId);
}
// 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在
// http正⽂内,因此需要设为true, 默认情况下是false;
con.setDoOutput(true);
// 设置是否从httpUrlConnection读⼊,默认情况下是true;
con.setDoInput(true);
// 设定请求的⽅法为"POST",默认是GET
con.setRequestMethod("POST");
// Post 请求不能使⽤缓存
con.setUseCaches(false);
//设置接收返回值的格式
con.setRequestProperty("Accept", "text/plain, */*");
//设置接收编码
con.setRequestProperty("Accept-Language", "zh-cn");
con.setRequestProperty("Host","127.0.0.1");
//设置请求参数格式以及boundary分割线
con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
con.setRequestProperty("User-Agent"," WinHttpClient");
//开启长连接可以持续传输
con.setRequestProperty("Connection", "Keep-Alive");
     //连接超时时间20秒
con.setConnectTimeout(20000);
     //读取超时时间20秒
con.setReadTimeout(20000);
OutputStream out = OutputStream();
//分隔符头部
//拼接参数
if (!Utils.isEmpty(params)){
int i = 0;
for (String paramName : params.keySet()){
StringBuffer strBufparam = new StringBuffer();
strBufparam.append(twoHyphens);
strBufparam.append(boundary);
strBufparam.append(nextLine);
strBufparam.append("Content-Disposition: form-data;name=\""+paramName+"\"");
strBufparam.append(nextLine);
strBufparam.append("Content-Type: " + "text/plain");
strBufparam.append(nextLine);
strBufparam.append("Content-Length: " + (paramName).getBytes().length);
strBufparam.append(nextLine);
strBufparam.append(nextLine);
strBufparam.(paramName));
i++;
if (i!=params.size()){
strBufparam.append(nextLine);
}
out.String().getBytes());
}
}
//拼接⽂件
if (!Utils.isEmpty(file)){
StringBuffer strBufFile = new StringBuffer();
strBufFile.append(nextLine);
strBufFile.append(twoHyphens);
strBufFile.append(boundary);
strBufFile.append(nextLine);
strBufFile.append("Content-Disposition: form-data; name=\""+name+"\"; filename=\"" + Name() + "\"");            strBufFile.append(nextLine);
strBufFile.append("Content-Type: " + "application/x-tar");
strBufFile.append(nextLine);
strBufFile.append("Content-Length: " + file.length());
strBufFile.append(nextLine);
strBufFile.append(nextLine);
//写⼊输出流
out.String().getBytes());
//读取本地⽂件流
FileInputStream inputStream = new FileInputStream(file);
byte[] data = new byte[2048];
int len = 0;
int sum = 0;
while ((len = ad(data)) != -1) {
//将读取到的本地⽂件流读取到HttpsURLConnection,进⾏上传
out.write(data, 0, len);
sum = len + sum;
}
//⽂件写⼊完成后加回车
out.Bytes());
inputStream.close();
}
//写⼊结束分隔符
String footer = nextLine + twoHyphens + boundary + twoHyphens + nextLine;
out.Bytes());
out.flush();
out.close();
int responseCode = ResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
if (!Utils.isEmpty(feedbackFile)) {//有返回⽂件的时候
String fileName = "";
try {
fileName = HeaderField(1).substring(21, HeaderField(1).length() - 1);
fileName = "EBDT"+fileName.split("_")[1];
System.out.println(fileName);
}catch (Exception e){
return null;
}
BufferedInputStream in = new InputStream());
File f = new File(feedbackFile);
if (!f.exists()) {
f.mkdirs();
}
File res = new File(feedbackFile+"/"+fileName);
FileOutputStream fos = new FileOutputStream(res);
byte[] by = new byte[1024];
int length = 0;
while ((length = in.read(by)) != -1) {
fos.write(by, 0, length);
}
jsessionidfos.close();
in.close();
con.disconnect();
if (Utils.isEmpty(fos)) {
return null;
}
//处理接收到的⽂件
//保存到本地
Map map = new HashMap();
map.put("fileName",fileName);
return map;
}else{
//接收到的return中的值
BufferedReader br = new BufferedReader(new InputStreamReader(
String s = br.readLine();
br.close();
con.disconnect();
try{
if (!Utils.isEmpty(s)){
Object m =  JSONObject.parse(s);
Map map = (Map) m;
return map;
}
}catch (Exception e){
Map map = new HashMap();
map.put("feedback",s);
return map;
}
}
return new HashMap();
} else {
con.disconnect();
return null;
}
}
}
main⽅法测试
public static void main(String[] args) throws IOException {
Map<String,String> map = new HashMap<>();
map.put("name","张三");
map.put("age","23");
File file = new File("D:\\1.xml");
Map map1 = HttpUrlConnection.httpUrlConnection("127.0.0.1/user/addUser", "69e4baee-ff22-4cdf-a73b-6156c5d6d2c1", file, "",                "files", map);
System.out.println(map1);
}
结果
到这就是全部了,如有问题欢迎留⾔

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。