作者平台:
| CSDN:blog.csdn.net/qq_41153943
| 知乎:www.zhihu.com/people/1024…
| GitHub:github.com/JiangXia-10…
| 微信公众号:1024笔记
本文大约3845字,预计阅读时长10分钟
前言
文件上传是很多业务场景需要实现的功能,今天就简单以Springboot框架为基础实现文件上传的接口。
正文
代码
使用SpringBoot实现文件上传接口其实很简单。
1、首先定义一个文件上传的服务service及其实现类:
public interface FileService {
public FileReturn uploadFile(MultipartFile multipartFile);
}
复制代码
@Service
public class FileServiceImpl implements FileService {
private static final Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);
@Override
public FileReturn uploadFile(MultipartFile multipartFile) {
// 文件保存路径
String filePath = "F:\\filepath";
// 文件名
String fileName = String.valueOf(System.currentTimeMillis());
File file = new File(filePath +File.separator + fileName);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
IOUtils.copy(multipartFile.getInputStream(),fileOutputStream);
System.out.println("===========file upload success=======");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
// 关闭
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
logger.error("文件关闭错误",e);
}
}
return new FileReturn<>(1,"文件上传成功",file);
}
}
复制代码
2、定义一个统一返回结果的实体类:
public class FileReturn<T> implements Serializable {
private static final long serialVersionUID = -1959544190118740608L;
private int resultCode;
private String msg;
private T data;
public FileReturn() {
}
public FileReturn(int resultCode, String msg, T data) {
this.resultCode = resultCode;
this.msg = msg;
this.data = data;
}
public int getResultCode() {
return resultCode;
}
public void setResultCode(int resultCode) {
this.resultCode = resultCode;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "FileReturn{" +
"resultCode=" + resultCode +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
复制代码
3、再定义一个工具类处理返回的结果:
public class ReturnValue<T> implements Serializable {
private static final long serialVersionUID = -1959544190118740608L;
private int ret;
private String msg;
private T data;
public ReturnValue() {
this.ret = 0;
this.msg = "";
this.data = null;
}
public ReturnValue(int retCode, String msg, T data) {
this.ret = 0;
this.msg = "";
this.data = null;
this.ret = retCode;
this.data = data;
this.msg = msg;
}
public ReturnValue(int retCode, String msg) {
this.ret = 0;
this.msg = "";
this.data = null;
this.ret = retCode;
this.msg = msg;
}
public ReturnValue(ReturnCodeAndMsgEnum codeAndMsg) {
this(codeAndMsg.getCode(), codeAndMsg.getMsg(), null);
}
public ReturnValue(ReturnCodeAndMsgEnum codeAndMsg, T data) {
this(codeAndMsg.getCode(), codeAndMsg.getMsg(), data);
}
public int getRet() {
return this.ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public String getMsg() {
return this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "ReturnValue{" +
"ret=" + ret +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
复制代码
4、最后就是实现一个Controller,处理文件上传的请求:
@RestController
@RequestMapping(value="/file")
public class FileController {
@Autowired
private FileService fileService;
@RequestMapping("/upload")
public FileReturn uploadFile(@RequestParam("uploadFile") MultipartFile multipartFile){
return fileService.uploadFile(multipartFile);
}
}
复制代码
5、可以在application.yml中加入以下配置,限制大小:
复制代码
spring:
servlet:
multipart:
# 单个文件最大限制 类型是datasize,单位kb
max-file-size: 1024
# 单次请求最大限制
max-request-size: 2048
复制代码
测试
这里使用postman测试软件进行测试,请求地址如下:
http://localhost:8081/share/file/upload
复制代码
参数类型选择file,具体请求如下:
选择一个zip格式的文件,然后点击send,然后请求成功了,返回数据如下:
然后f盘,file文件夹下文件已经上传过来了,文件名就是时间戳:
总结
以上就是如何使用springboot开发文件上传接口,其实和springboot没有太大的关系,就是使用它进行接口开发, 这里仅仅是简单的核心内容,可以根据自己实际的业务和需求进行拓展和延伸。
如有任何问题或者不对的地方欢迎一起交流讨论学习!
本项目源码在:
欢迎下载、Star!