怎么发送文件到服务器端,使用HttpClient发送文件流到服务器端
适用场景:
网络绝对路径的url文件或图片,不存储到本地,转换成stream,直接使用httpclient传送到springboot的服务端,将文件存储下来,并返回一个文件地址。目前分层架构的系统越来越多这种需求,所以记录下来以备不时之需。
1、调用端
首先引入httpclient所需包
org.apache.httpcomponents
httpclient
4.4
org.apache.httpcomponents
httpmime
4.4
调用代码:
package test.http;
import com.alibaba.fastjson.json;
import com.alibaba.fastjson.jsonarray;
import com.alibaba.fastjson.jsonobject;
import org.apache.http.httpentity;
import org.apache.http.httpresponse;
import org.apache.http.client.methods.httppost;
import org.apache.http.entity.contenttype;
import org.apache.http.entity.mime.multipartentitybuilder;
import org.apache.http.impl.client.closeablehttpclient;
import org.apache.http.impl.client.httpclients;
import org.apache.http.util.entityutils;
import java.io.*;
import java.net.url;
import java.nio.charset.charset;
/**
* 文件传送
* 发送文件流到服务器端
* 服务器端使用springboot的multipartfile接收
*
* 适用场景:
* 绝对路径的url文件,不存储到本地,转换成stream,直接使用httpclient传送到springboot
*
*/
public class testupload {
public static void main(string[] args) {
//文件url,此处取豆瓣上的一个图片
string fileurl ="https://img1.doubanio.com/view/photo/l/public/p2537149328.webp";
try {
//提取到文件名
string filename = fileurl.substring(fileurl.lastindexof("/")+1);
//转换成文件流
inputstream is = new url(fileurl).openstream();
//接收文件的服务器地址
string uploadurl = "http://localhost:8003/fileupload";
//创建httpclient
closeablehttpclient httpclient = httpclients.createdefault();
httppost httppost = new httppost(uploadurl);
multipartentitybuilder builder = multipartentitybuilder.create();
/*绑定文件参数,传入文件流和contenttype,此处也可以继续添加其他formdata参数*/
builder.addbinarybody("file",is, contenttype.multipart_form_data,filename);
httpentity entity = builder.build();
httppost.setentity(entity);
//执行提交
httpresponse response = httpclient.execute(httppost);
httpentity responseentity = response.getentity();
if(responseentity != null){
//将响应的内容转换成字符串
string result = entityutils.tostring(responseentity, charset.forname("utf-8"));
//此处根据服务器返回的参数转换,这里返回的是json格式
jsonobject output = json.parseobject(result);
jsonarray body = output.getjsonarray("body");
string resurl = body.get(0)+"";
system.out.println(resurl);
}
}catch (exception ex){
ex.printstacktrace();
}
}
}
2、服务端
服务端直接使用multipartfile接收即可
/**
* 上传文件
*
* @throws businessexception
*/
@postmapping("")
public string upload(@requestparam(defaultvalue = "", required = false) string prefix,
@requestparam("file") multipartfile... files) throws businessexception {
resultview> resultview = new resultview<>();
list list = new arraylist<>();
for (multipartfile file : files) {
if (file.isempty()) {
log.warn("have empty upload file,you need check is right?");
continue;
}
string filepath = storageservice.store(file, prefix);
list.add(fileserveraddress + filepath.replaceall("\\\\", "/"));
}
resultview.setbody(list);
log.info(jsonobject.tojsonstring(resultview));
return jsonobject.tojsonstring(resultview);
}
具体如何存储如何返回,因人而异,我这里返回的是json字符串。
其他:本文参考了博友vincent-li的博文,表示感谢:
怎么发送文件到服务器端,使用HttpClient发送文件流到服务器端相关推荐
- 通过httpclient发送请求的几种方式,发送文件、参数、json对象
使用工具:idea 框架:gradle.springboot 实现目标:使用 httpclient 发送文件/参数/json对象 method:post 主要用到的jar包: compile grou ...
- HttpClient发送请求
HttpClient发送请求 前言:前段时间接到一个需求,要求系统中实现一个可以上传语音文件的功能,然后将文件和需要的参数发送到电信的接口上: 开始说起来感觉很简单,但是就真的被折磨了好几天,主要还是 ...
- Ionic+Angular+Express实现前后端交互使用HttpClient发送get请求数据并加载显示(附代码下载)
场景 Ionic介绍以及搭建环境.新建和运行项目: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/106308166 在上面搭建起 ...
- 使用HttpClient发送WebService Security(WSS)请求
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 使用Ht ...
- httpclient发送Get请求和Post请求
创建HttpClient发送请求.接收响应 Get请求简介 get无参数 get有参数 Post请求简介 post携带JSON参数 post携带表单参数 postman自动生成OKhttp代码 Get ...
- 【狂神说】JavaWeb笔记整理 | SMBMS项目 | 文件上传和邮件发送
目录 JavaWeb 1.基本概念 1.1 概念 1.2 web应用程序: 1.3 静态web 1.4 动态web 2.web服务器 2.1 技术讲解 2.2 web 服务器 3. Tomcat 3. ...
- postman发送object_.NetCore HttpClient发送请求的时候为什么自动带上了一个RequestId头部...
奇怪的问题 最近在公司有个系统需要调用第三方的一个webservice.本来调用一个下很简单的事情,使用HttpClient构造一个SOAP请求发送出去拿到XML解析就是了. 可奇怪的是我们的请求在运 ...
- Httpclient发送json请求
一.Httpclient发送json请求 public String RequestJsonPost(String url){ String strresponse = null; t ...
- .NetCore HttpClient发送请求的时候为什么自动带上了一个RequestId头部?
奇怪的问题 最近在公司有个系统需要调用第三方的一个webservice.本来调用一个下很简单的事情,使用HttpClient构造一个SOAP请求发送出去拿到XML解析就是了.可奇怪的是我们的请求在运行 ...
最新文章
- 调查:中国CIO在亚太拥最大战略影响力
- [py]access日志入mysql-通过flask前端展示
- MySql 查询小数保留两位小数
- 三十九、SPSS神器界面功能介绍,计算变量和个案计数和加权
- Android studio 如何查看当前git 分支的4种方式
- SAP Spartacus 4.0 deprecation 之一 - i18next-xhr-backend
- python切片操作 当所有数据都省略时_python疑难问题---13、Python切片操作
- 又到了充钱的时候!苹果AirPods 3最快下月发布
- 【转】DICOM 网关的设计与实现
- 数据存储之偏好设置NSUserDefaults
- oracle 循环块,Oracle语句块PL/SQL循环判断
- Java设计模式之十 ---- 访问者模式和中介者模式
- gcc编译与vc编译器区别
- html级联选择器,HTML5 学习--级联样式与CSS选择器
- 计算机应用基础2-excel,《2014计算机应用基础excel2.doc
- 赵玉海:科技部已组织专家编制中国云
- APP推广关键词的怎么选词?
- [Unity]Mesh Baker3.1.0使用教程
- 美国诚实签经验——回答签证官的问题时,一定要问什么才答什么,不要犹豫改口、做很多解释或开玩笑,自信且镇静地与他对话,着装得体、举止自然、言谈自信...
- 必备模块知识——超声波传感器