基于SpringBoot 实现一个文件上传的API接口。并使用postman测试

1.  创建实体类用于返回结果、

public class FileReturn<T> implements Serializable {
    private static final  long serialVersionUID = -133233199901011121L;
    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 +
                '}';
    }
}

 2. 定义文件上传接口以及实现类

@Mapper
public interface FileMapper {
    public FileReturn uploadFile(MultipartFile multipartFile);
}

 

@Service
public class FileServiceImpl implements FileMapper {

    private static final Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);

    @Override
    public FileReturn uploadFile(MultipartFile multipartFile) {
//      文件保存路径
        String filePath = "E:\\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);
    }
}

 3. service 业务层

@Service
public class FileService {
    @Resource
    private FileServiceImpl fileServiceImpl;

    public FileReturn uploadFile(MultipartFile multipartFile){
        return fileServiceImpl.uploadFile(multipartFile);
    }
}

4. controller 控制层

 

@RestController
@RequestMapping(value = "/file")
public class FileController {
    @Resource
    private FileService fileService;

    @RequestMapping("/upload")
    public FileReturn uploadFile(@RequestParam("uploadFile")MultipartFile multipartFile){
        return fileService.uploadFile(multipartFile);
    }
}

 5. postman 测试

 

文章参考 链接SpringBoot实现文件上传接口-阿里云开发者社区 (aliyun.com)

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值