当前位置: 首页 > 知识库问答 >
问题:

无法实例化Service.FileService构造函数引发异常;

易俊驰
2023-03-14

-----------这是我的文件服务---------------------我使用的是spring boot 2.2.2

@Service  public class FileService {

    public final Path fileStoreLocation;

    @Autowired
    public FileService(File file) throws FileStorageExecption {
        this.fileStoreLocation= Paths.get(file.getFile_path()).toAbsolutePath().normalize();
        try{
            Files.createDirectories(this.fileStoreLocation);
        }
        catch (Exception e){

                throw  new FileStorageExecption("--------Create directories exception-------");

        }
    }


    public String storeFile(MultipartFile file) throws FileStorageExecption {
        String fileName= StringUtils.cleanPath(file.getOriginalFilename());
        try {
            Path targetPath=this.fileStoreLocation.resolve(fileName);
            Files.copy(file.getInputStream(),targetPath, StandardCopyOption.REPLACE_EXISTING);
            return fileName;
        }
        catch (Exception e){
               throw new  FileStorageExecption("---------Cann't copy file----");


        }
    } }

-------这是我的文件控制器--------

 @RestController
 public class FileController {
    @Autowired
    FileService fileService ;

    @RequestMapping(method = RequestMethod.POST,value = "/file/upload")
    public String fileUpload(@RequestParam MultipartFile file) throws FileStorageExecption {
            fileService.storeFile(file);
        return "ok";

    }

}

进程已完成,退出代码为%1

共有1个答案

尤俊誉
2023-03-14

FileService的构造函数应具有FileStorageProperties而不是File

@Autowired
public FileService(FileStorageProperties fileStorageProperties) {
 类似资料: