Giter Site home page Giter Site logo

Comments (2)

small-howe avatar small-howe commented on May 27, 2024

解决方案
`package tangweihao.test.config;

import cn.hutool.core.io.FileUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.dromara.x.file.storage.core.exception.FileStorageRuntimeException;
import org.dromara.x.file.storage.core.file.FileWrapper;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**

  • Multipart 文件包装类
    */
    @getter
    @Setter
    @NoArgsConstructor
    public class MultipartFileWrapper implements FileWrapper {
    private MultipartFile file;
    private String name;
    private String contentType;
    private InputStream inputStream;
    private Long size;

    public MultipartFileWrapper(MultipartFile file,String name,String contentType,Long size) {
    this.file = file;
    this.name = name;
    this.contentType = contentType;
    this.size = size;
    }

    @OverRide
    public InputStream getInputStream() throws IOException {
    if (inputStream == null) {
    inputStream = new BufferedInputStream(file.getInputStream());
    }
    return inputStream;
    }

    @OverRide
    public void transferTo(File dest) {
    // 在某些 SpringBoot 版本中,例如 2.4.6,此方法会调用失败,
    // 此时尝试手动将 InputStream 写入指定文件,
    // 根据文档来看 MultipartFile 最终都会由框架从临时目录中删除
    try {
    file.transferTo(dest);
    } catch (Exception ignored) {
    try {
    FileUtil.writeFromStream(getInputStream(),dest);
    } catch (Exception e) {
    throw new FileStorageRuntimeException("文件移动失败",e);
    }
    }
    }

    @OverRide
    public boolean supportTransfer() {
    return true;
    }

}
`

`package tangweihao.test.config;

import lombok.Getter;
import lombok.Setter;
import org.dromara.x.file.storage.core.file.ByteFileWrapperAdapter;
import org.dromara.x.file.storage.core.file.FileWrapper;
import org.dromara.x.file.storage.core.file.FileWrapperAdapter;
import org.dromara.x.file.storage.core.tika.ContentTypeDetect;
import org.springframework.context.annotation.Bean;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**

  • Multipart 文件包装适配器
    */
    @getter
    @Setter
    public class MultipartFileWrapperAdapter implements FileWrapperAdapter {

    @OverRide
    public boolean isSupport(Object source) {
    return source instanceof MultipartFile || source instanceof MultipartFileWrapper;
    }

    @OverRide
    public FileWrapper getFileWrapper(Object source,String name,String contentType,Long size) throws IOException {
    if (source instanceof MultipartFileWrapper) {
    return updateFileWrapper((MultipartFileWrapper) source,name,contentType,size);
    } else {
    MultipartFile file = (MultipartFile) source;
    if (name == null) {
    name = file.getOriginalFilename();
    }
    if (contentType == null) {
    contentType = file.getContentType();
    }
    if (size == null) {
    size = file.getSize();
    }
    return new MultipartFileWrapper(file,name,contentType,size);
    }
    }
    }

package tangweihao.test.config;

import org.dromara.x.file.storage.core.FileStorageProperties;
import org.dromara.x.file.storage.core.FileStorageService;
import org.dromara.x.file.storage.core.FileStorageServiceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;

@configuration
public class FileStorageServiceConfig {

@Bean
public FileStorageService fileStorageService() {
    // 在这里进行初始化操作,可以使用你提供的代码
    FileStorageProperties properties = new FileStorageProperties();
    properties.setDefaultPlatform("huawei-obs-1");
    FileStorageProperties.HuaweiObsConfig huaweiObsConfig = new FileStorageProperties.HuaweiObsConfig();
    huaweiObsConfig.setPlatform("huawei-obs-1");
    huaweiObsConfig.setAccessKey("T**********N");
    huaweiObsConfig.setSecretKey("J******************************ry");
    huaweiObsConfig.setBucketName("smal-lhowe");
    huaweiObsConfig.setEndPoint("https://obs.cn-east-3.myhuaweicloud.com");
    huaweiObsConfig.setDomain("http://abc.obs.com/");
    properties.setHuaweiObs(Collections.singletonList(huaweiObsConfig));
    FileStorageServiceBuilder fileStorageServiceBuilder = FileStorageServiceBuilder.create(properties);
    fileStorageServiceBuilder.getFileWrapperAdapterList().add(new MultipartFileWrapperAdapter());
    fileStorageServiceBuilder.addFileWrapperAdapter(new MultipartFileWrapperAdapter());
    return fileStorageServiceBuilder.useDefault().build();
}

}
`

from x-file-storage.

1171736840 avatar 1171736840 commented on May 27, 2024

你这样,还是引入 x-file-storage-spring 这个依赖,这里有 MultipartFileWrapper 以及适配器,然后不要使用 @EnableFileStorage 这个注解,再手动初始化就行

先在启动类上引入以下注解

@Import({SpringFileStorageProperties.class})

再初始化

@Configuration
public class FileStorageAutoConfiguration2 {

    @Autowired
    private SpringFileStorageProperties properties;

    @Bean
    public FileStorageService fileStorageService() {

        // 创建,自定义存储平台、 Client 工厂、切面等功能都有对应的添加方法
        return FileStorageServiceBuilder.create(properties.toFileStorageProperties())
                .useDefault()
                .addFileWrapperAdapter(new MultipartFileWrapperAdapter())
                .build();
    }
}

from x-file-storage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.