栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MultipartFile文件上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MultipartFile文件上传

关于MultipartFile 文件上传

看一下MultipartFile源码

package org.springframework.web.multipart;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.springframework.core.io.InputStreamSource;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.util.FileCopyUtils;

public interface MultipartFile extends InputStreamSource {
    String getName();

    @Nullable
    String getOriginalFilename();

    @Nullable
    String getContentType();

    boolean isEmpty();

    long getSize();

    byte[] getBytes() throws IOException;

    InputStream getInputStream() throws IOException;

    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));
    }
}

 (1)、getName方法

          getName方法获取的是前后端约定的传入文件的参数的名称,在SpringBoot后台中则是通过@Param("uploadFile") 注解定义的内容。

 (2)、getOriginalFileName方法 

         getOriginalFileName方法获取的是文件的完整名称,包括文件名称+文件拓展名。

 (3)、getContentType方法 

         getContentType方法获取的是文件的类型,注意是文件的类型,不是文件的拓展名。

 (4)、isEmpty方法 

         isEmpty方法用来判断传入的文件是否为空,如果为空则表示没有传入任何文件。

 (5)、getSize方法 

         getSize方法用来获取文件的大小,单位是字节。

 (6)、getBytes方法 

         getBytes方法用来将文件转换成一种字节数组的方式进行传输,会抛出IOException异常。

 (7)、getInputStream方法 

         getInputStream方法用来将文件转换成输入流的形式来传输文件,会抛出IOException异常。

 (8)、transferTo方法 

         transferTo方法用来将接收文件传输到给定目标路径,会抛出IOException、IllegalStateException异常。该方法在实际项目开发中使用较少。

来了解一下这个上传接口

controller控制


@Api(tags = "上传服务")
@RestController
@RequestMapping("/image")
@Slf4j
public class ImageUploadController {

    @Resource
    private IUploadService uploadService;

    @ApiOperation("上传")
    @PostMapping("/upload")
    public JsonResult upload(MultipartFile file) {
        if (file.getSize() <= 0){
            return JsonResult.build(CodeEnums.SYS_INPUT_ERROR);
        }

        return JsonResult.ok(uploadService.upload(file));
    }


}

接口

IUploadService

public interface IUploadService {

    ImageUploadRespDTO upload(MultipartFile file);

}

类实现接口

UploadServiceImpl 
@Service
@Slf4j
public class UploadServiceImpl implements IUploadService {

    
    @Value("${agreement.annexAddress.url:file/default/attachment/agreement}")
    private String annexAddress;

    @Override
    public ImageUploadRespDTO upload(MultipartFile file) {
        ImageUploadRespDTO dto = new ImageUploadRespDTO();

        String originalFilename = file.getOriginalFilename();
        String suffix = Objects.requireNonNull(originalFilename)
                .substring(originalFilename.lastIndexOf(Const.POINT));
        String fileName = RandomUtils.randomString() + suffix;

        dto.setFileName(fileName);
        dto.setLocalPath(String.format(ImageConst.LOCAL_PATH, DateUtil.today(), fileName));
        dto.setPath(ImageConst.COS_PATH_HEADER + dto.getLocalPath());
        dto.setSize(file.getSize());
        dto.setUrl(COSUtils.getCdnUrl(dto.getPath()));

        try {
            COSUtils.putObject(dto.getPath(), file.getInputStream());
        } catch (Exception e){
            throw new ImageProcessException("上传失败!");
        }

        return dto;
    }

}

返回的DTO

@ApiModel("图片上传数据返回对象")
@Data
public class ImageUploadRespDTO implements Serializable {
    @ApiModelProperty(value = "文件大小")
    private Long size;

    @ApiModelProperty(value = "文件名")
    private String fileName;

    @ApiModelProperty(value = "链接")
    private String url;

    @ApiModelProperty(value = "相对路径")
    private String path;

    @ApiModelProperty(value = "本地路径")
    private String localPath;
}

本文是引用了腾讯云cos服务工具

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/781292.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号