byte[]转换InputStream/MultipartFile

前言

最近有一个需求, 我在项目中使用了MinIo但是并没有找到文件重命名的方法,所以,想着如何通过文件名进行获取byte[],然后通过byte转换为MultipartFile重新进行文件上传,模拟了重命名功能。

解决思路

首先MultipartFile是一个接口,实现它的方法即可。download方法接收参数fileName, 返回值是ResponseEntity类型,从中拿到body里面的数据也就是byte[], 然后实现MultipartFile接口,再重新上传文件即可(上传文件方法需要MultipartFile参数和fileName参数)。

代码实现

image
由于修改文件的分组,具体逻辑是先删除文件在重新上传,我们如果只接收fileName,groupName只能拿到修改后的文件名称和分组名称,并不能拿到原来的文件名和分组名,所以修改后的editFileName,editGroupName也需要这两个字段。

 @Override
    public R editResourceInfo(ResourceInfoDto resourceInfoDto) {

        if (Objects.nonNull(resourceInfoDto)) {

            //先删除MinIo中的文件,在重新上传
            String fileName = resourceInfoDto.getGroupName() + "/" + resourceInfoDto.getFileName();

            ResponseEntity<byte[]> responseEntity = download(fileName);
            byte[] body = responseEntity.getBody();
            MultipartFileImpl multipartFile = new MultipartFileImpl(fileName, body);
            minIoUtils.removeObjects(SystemConstants.BUCKET_NAME, Arrays.asList(fileName));

            //重新上传
            String newFileName = resourceInfoDto.getEditGroupName() + "/" + resourceInfoDto.getEditFileName();
            minIoUtils.upload(multipartFile, newFileName);

            ResourceInfo resourceInfo = BeanCopyUtil.copyBean(resourceInfoDto, ResourceInfo.class);
            resourceInfo.setFileName(resourceInfoDto.getEditFileName());
            resourceInfo.setGroupName(resourceInfoDto.getEditGroupName());
            boolean b = this.updateById(resourceInfo);
            if (!b) {
                return R.errorResult(AppHttpCodeEnum.EDIT_RESOURCE_ERROR.getCode(),AppHttpCodeEnum.EDIT_RESOURCE_ERROR.getMsg());
            }

            return R.okResult();
        }
        return R.errorResult(AppHttpCodeEnum.EDIT_RESOURCE_ERROR.getCode(),AppHttpCodeEnum.EDIT_RESOURCE_ERROR.getMsg());

    }

image

MultipartFile实现类

public class MultipartFileImpl implements MultipartFile {

    private String name;

    private String fileName;

    private String contentType;

    private byte[] content;

    public MultipartFileImpl(String fileName, byte[] content) {

        this(fileName, MediaType.APPLICATION_OCTET_STREAM_VALUE, content);

    }

    public MultipartFileImpl(String fileName, String contentType, byte[] content) {
        this.fileName = fileName;
        this.contentType = contentType;
        this.content = content;
    }


    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public String getOriginalFilename() {
        return this.fileName;
    }

    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public boolean isEmpty() {
        return (this.content.length == 0);
    }

    @Override
    public long getSize() {
        return this.content.length;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return this.content;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(this.content);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        FileCopyUtils.copy(this.content, dest);
    }
}

至于byte[]转InputStream就很简单了

      ByteArrayInputStream bis = new ByteArrayInputStream(body);
posted @ 2023-03-14 22:05  长情c  阅读(1816)  评论(2)    收藏  举报