标签:sub info 本地 cat ons sea mime java.net hot
适用场景:
网络绝对路径的URL文件或图片,不存储到本地,转换成stream,直接使用HTTPClient传送到SpringBoot的服务端,将文件存储下来,并返回一个文件地址。目前分层架构的系统越来越多这种需求,所以记录下来以备不时之需。
1、调用端
首先引入httpclient所需包
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.4</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.4</version> </dependency>
调用代码:
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<List<String>> resultView = new ResultView<>(); List<String> 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的博文,表示感谢:
https://www.cnblogs.com/lyxy/p/5629151.html
标签:sub info 本地 cat ons sea mime java.net hot
原文地址:https://www.cnblogs.com/tobeymarshall/p/10215101.html