添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
私奔的领结  ·  RestTemplate 最详解 - 知乎·  1 年前    · 
开心的海豚  ·  Android ...·  1 年前    · 
买醉的海豚  ·  WPF RichTextBox ...·  1 年前    · 
睡不着的荒野  ·  python psycopg2 ...·  1 年前    · 
首发于 1024笔记
SpringBoot实现文件上传接口

SpringBoot实现文件上传接口

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第5天, 点击查看活动详情


作者平台:

| CSDN: blog.csdn.net/qq_41153943

| 掘金: juejin.cn/user/651387…

| 知乎: 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: