欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot实现文件的上传和返回Json的结果

程序员文章站 2024-02-03 10:00:58
...

一、spring boot实现文件的上传

spring boot实现文件的上传与springMVC的步骤基本一致:
1、提供上传页面upload.html(resources或static文件夹下都可)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form method="post" action="upload" enctype="multipart/form-data">
    选择上传文件:<input type="file" name="upload"><br>
    <input type="submit" value="上传">
</form>
</body>
</html>

2、编写上传逻辑代码

package com.springboot.demo.Controller;

import com.springboot.demo.utils.JsonData;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class UploadController {

    @RequestMapping("upload")
    public String upload(MultipartFile upload){//upload应与前端input框的name值保持一致
        //1.获取上传文件的名称
        String filename = upload.getOriginalFilename();
        //2.设置文件路径
        String path = "D:\\upload";
        //3.判断文件是否存在
        File file = new File(path);
        if (!file.exists()){
            file.mkdir();
        }
        //4.修改文件名称
        String suffix = filename.substring(filename.lastIndexOf("."));
        //4.1拼接文件名
        String newname = path+File.separator+System.currentTimeMillis()+suffix;
        try {
            //5.上传
            upload.transferTo(new File(newname));
            return "上传成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败";
        }
    }


    //多文件上传
    @RequestMapping("uploadlist")
    public String uploadlist(MultipartFile[] upload){

        //1.设置文件路径
        String path = "D:\\upload";
        //2.判断文件是否存在
        File file = new File(path);
        if (!file.exists()){
            file.mkdir();
        }

        for (int i = 0; i < upload.length; i++) {
            //3.获取上传文件的名称
            String filename = upload[i].getOriginalFilename();
            //4.修改文件名称
            String suffix = filename.substring(filename.lastIndexOf("."));
            //4.1拼接文件名
            String newname = path+File.separator+System.currentTimeMillis()+suffix;
            try {
                //5.上传
                upload[i].transferTo(new File(newname));
                return "上传成功";
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败";
            }
        }

        return "上传成功";
    }
}

3、提供json工具类 ,在demo下创建utils文件夹,创建JsonData工具类

package com.springboot.demo.utils;

public class JsonData {

    private Integer code;  //请求成功或失败的一个响应码
    private String msg;    //成功或失败的一个信息描述
    private Object data;  //响应的具体数据

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public JsonData() {
    }

    public JsonData(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    //需要向外部暴露的成功或失败的处理
    public static JsonData buildsuccess(Object data){
        return new JsonData(200,"操作成功",data);
    }

    public static JsonData buildfail(){//此处也可传入错误的数据,根据需求填写。
        return new JsonData(-100,"操作失败",null);
    }
}

4、在启动类配置上传参数,即修改步骤2的数据返回类型为JsonData,并修改return的数据:

@RestController
public class UploadController {

    @RequestMapping("upload")
    public JsonData upload(MultipartFile upload){//upload应与前端input框的name值保持一致
        //1.获取上传文件的名称
        String filename = upload.getOriginalFilename();
        //2.设置文件路径
        String path = "D:\\upload";
        //3.判断文件是否存在
        File file = new File(path);
        if (!file.exists()){
            file.mkdir();
        }
        //4.修改文件名称
        String suffix = filename.substring(filename.lastIndexOf("."));
        //4.1拼接文件名
        String newname = path+File.separator+System.currentTimeMillis()+suffix;
        try {
            //5.上传
            upload.transferTo(new File(newname));
            return JsonData.buildsuccess(null);
        } catch (IOException e) {
            e.printStackTrace();
            return JsonData.buildfail();
        }
    }
}

5、测试,浏览器输入http://localhost:8080/upload.html,选择文件点击上传,成功页面。
spring boot实现文件的上传和返回Json的结果