MultipartFile

MultipartFile其实是一个接口,其中定义提供了上传文件的各方面信息

public interface MultipartFile extends InputStreamSource {
    //获取文件的名字,这里指的是post表单里面定义的名字
    String getName();
    //获取文件的原名字,这里指的是本地文件真正的名字
    @Nullable
    String getOriginalFilename();
    //文件的类型
    @Nullable
    String getContentType();
    //文件是否为空
    boolean isEmpty();
    //文件的大小
    long getSize();
    //获取文件的byte数组
    byte[] getBytes() throws IOException;
    //以流的方式获取文件
    InputStream getInputStream() throws IOException;
    //将其转为Resource类型,可以将其视为文件资源
    default Resource getResource() {
        return new MultipartFileResource(this);
    }
    //将其转换为文件
    void transferTo(File var1) throws IOException, IllegalStateException;
    //通过提供文件路径的方式将其转换为文件
    default void transferTo(Path dest) throws IOException, IllegalStateException {
        FileCopyUtils.copy(this.getInputStream(), Files.newOutputStream(dest));
    }
}

存储图片

    @PostMapping("/uploadImage")
    @ResponseBody
    public BaseGduiDTO<?> uploadImageFile(@RequestParam("img") MultipartFile uploadImage) throws Exception {

        String fileName = uploadImage.getOriginalFilename();
        String imgFilePath = "D:\\img";
        File targetFile = new File(imgFilePath, fileName);
        //保存
        uploadImage.transferTo(targetFile);
        return BaseGduiDTO.ok(imgFilePath+"\\"+fileName);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值