SpringBoot RestTemplate接收文件,并将文件发送到另外一个程序进行存储
最近有个需求,接收用户上报的证书,并且保存起来,证书大小不到1M,但该证书的保存必须在另外一个程序进行,所以想到使用springboot接收上传文件后,再通过RestTemplate将文件发送给另外一个程序来处理,假设我们定义接收从页面中上传的文件并发送给另外一个程序的服务称之为客户端,接收客户端发送的文件的服务称之为服务端
pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 保存文件所需的工具类 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
客户端
页面
客户端使用了freemarker显示上传界面,pom文件添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
uploadFile.ftl
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title></title>
</head>
<body>
<form name="fileForm" method="post" action="/test/upload" enctype="multipart/form-data">
name: <input type="text" name="uploader"><br/>
file:<input type="file", name="uploadFile">
<input type="submit" id = "submit">
</form
</body>
</html>
接收页面上传文件
package com.demo.bootdemo; import java.util.Map; import javax.annotation.Resource; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile; @Controller
@RequestMapping("/test")
public class UploadFileController { private Logger logger = LoggerFactory.getLogger(UploadFileController.class); @Resource
private RestTemplate restTemplate; @RequestMapping("toUploadPage")
public String toUploadPage() {
return "uploadFile";
} @PostMapping("upload")
@ResponseBody
public Object upload(MultipartFile uploadFile, String uploader) { MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.MULTIPART_FORM_DATA); HttpHeaders fileHeader = new HttpHeaders();
fileHeader.setContentType(MediaType.parseMediaType(uploadFile.getContentType()));
fileHeader.setContentDispositionFormData("uploadFile", uploadFile.getOriginalFilename()); Map result = null;
try {
HttpEntity<ByteArrayResource> fileEntity = new HttpEntity<>(new ByteArrayResource(uploadFile.getBytes()),
fileHeader);
multiValueMap.add("uploadFile", fileEntity);
multiValueMap.add("uploader", uploader); String url = "http://127.0.0.1:8082/rest/createFile"; HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(multiValueMap, header);
ResponseEntity<Map> postForEntity = restTemplate.postForEntity(url, httpEntity, Map.class);
result = postForEntity.getBody();
} catch (Exception e) {
logger.error("", e);
} return result; } }
服务器端
接收文件
package com.demo.bootdemo; import java.io.File;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; @Controller
@RequestMapping("/rest")
public class CreateFileController {
private Logger logger = LoggerFactory.getLogger(CreateFileController.class); @PostMapping("createFile")
@ResponseBody
public Map<String, Object> createFile(MultipartFile uploadFile, String uploader) { String message = "";
boolean success = true;
try {
FileUtils.copyInputStreamToFile(uploadFile.getInputStream(),
new File("G:\\rest\\" + uploadFile.getOriginalFilename())); } catch (Exception e) {
logger.error("", e);
message = e.getMessage();
success = false;
}
Map<String, Object> result = new HashMap<>(); result.put("message", message);
result.put("success", success);
return result; }
}
备注
如果传递的文件过大,比如说超过了10M,则需要修改如下两个参数(单位Byte),但一般通过http上传的文件不要太大,如果太大,可以考虑使用ftp方式上传
## default 1M
spring.servlet.multipart.max-file-size=8000000000
## default 10M
spring.servlet.multipart.max-request-size=8000000000
SpringBoot RestTemplate接收文件,并将文件发送到另外一个程序进行存储的更多相关文章
- springBoot上传文件时MultipartFile报空问题解决方法
springBoot上传文件时MultipartFile报空问题解决方法 1.问题描述: 之前用spring MVC,转成spring boot之后发现上传不能用.网上参考说是spring boot已 ...
- C#网络编程(接收文件) - Part.5
这篇文章将完成 Part.4 中剩余的部分,它们本来是一篇完整的文章,但是因为上一篇比较长,合并起来页数太多,浏览起来可能会比较不方便,我就将它拆为两篇了,本文便是它的后半部分.我们继续进行上一篇没有 ...
- SpringBoot项目实现文件上传和邮件发送
前言 本篇文章主要介绍的是SpringBoot项目实现文件上传和邮件发送的功能. SpringBoot 文件上传 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 开发准备 环境要 ...
- 应用SecureCRT(发送接收文件)
使用 SecureCRT 和 cz. sz,可以从 Linux 服务器上下载/上传文件. Linux 上要安装 lszrz 包 (1)编译安装root 账号登陆后,依次执行以下命令 cd /tmp w ...
- 【Android Developers Training】 42. 从另一台设备接收文件
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- SpringBoot上传文件到本服务器 目录与jar包同级
前言 看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了. 当你使用tomcat发布项目的时候,上传文件存放会变得非常简单,因为你可以随意操作项目路径下的资源.但是当你 ...
- springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器
1. Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture&q ...
- 在Django中接收文件并存储
首先是一个views函数的例子 def get_user_profiles(request): if request.method == 'POST': myFile = request.FILES. ...
- python requests上传文件 tornado 接收文件
requests 上传文件 import requests def images(): url = 'http://127.0.0.1:8889/upload/image' files = {'fil ...
随机推荐
- python2.7.5安装docker-compose的方法
yum -y install epel-release && yum install -y python-pip && pip install --upgrade pi ...
- linux命令安装docker
安装: 1.Docker要求CentOS系统的内核版本高于 3.10 ,通过 uname -r 命令查看你当前的内核版本是否支持安账docker 2.更新yum包:sudo yum update 3. ...
- SAP UI5应用入口App.controller.js是如何被UI5框架加载的?
首先在UI5应用的manifes.json里,定义了UI5应用的入口视图为App: 调试器里的pending数组的两个元素: 实际上对应了我在App.controller.js里定义的两个依赖: 而a ...
- yml格式的在线转换工具
工具1: https://www.toolfk.com/tool-format-yaml 工具2: https://www.toyaml.com/index.html https://www.cnbl ...
- [Python][pythonchallenge][TBC]古老的python在线挑战赛,很有意思 (C0-C4)
预计阅读时间:15分钟 背景:搜索资料时候偶然发现的,很有意思,每一关都覆盖了很多知识点 Python版本:3.0 Talking is cheap,show me the code 主页: http ...
- 安装Vue脚手架和创建一个简单的Demo
https://www.cnblogs.com/pengjunhao/p/6762141.html https://www.cnblogs.com/yanxulan/p/8978732.html 查看 ...
- SP703 SERVICE - Mobile Service[DP]
题意翻译 Description 一个公司有三个移动服务员.如果某个地方有一个请求,某个员工必须赶到那个地方去(那个地方没有其他员工),某一时刻只有一个员工能移动.只有被请求后,他才能移动,不允许在同 ...
- 将CHROME WEBSTORE里的APPS和扩展下载到本地 转载自:http://ro-oo.diandian.com/post/2011-05-28/1103036
Chrome Webstore 自动改版后就不能再直接下载到本地... 下载地址: https://clients2.google.com/service/update2/crx?response=r ...
- linux 优化
如何优化Linux系统? 1)不用root超级用户登录,添加普通用户,通过sudo授权管理:/etc/sudoers 2)更改默认的远程连接ssh服务端口号,禁止root用户远程登录到服务器:/etc ...
- ACM-ICPC 2018 徐州赛区现场赛 I. Rikka with Sorting Networks (思维+DFS)
题目链接:https://codeforces.com/gym/102012/problem/I 题意:问有多少个 1 到 n 的排列,使得用给定的 k 个比较器(使 au 和 av 有序)排序后,整 ...