putObject语句无法将文件上传到阿里云OSS

时间:2019-10-19 09:47:10

标签: java tomcat war

我编写了一个将视频文件上传到阿里云OSS的功能。该功能在春季启动时工作正常,但在独立的tomcat中无法使用。该问题是由语句ossClient.putObject(bucket,Objectname,file.getInputStream());

引起的

有人可以告诉我哪里出了问题吗?

在春季启动中,我运行此功能并将文件成功上传到阿里云OSS。然后我将代码导出到war文件并部署到tomcat,然后无法正常工作。 没有抛出异常时,它将引发500个内部服务器错误。

@Controller
public class UploadVideoController {
    private final static org.slf4j.Logger log = LoggerFactory.getLogger(UploadVideoController.class);
    @Autowired
    private VideoFileRepository videoFileRepository;
    @Autowired
    private ApplicationUserRepository applicationUserRepository;
    @Autowired
    private VideoFileInSolrRepository videoSolrRepo;

    String endpoint = "http://oss-cn-zhangjiakou.aliyuncs.com";
    String accessKeyId = "my id";
    String accessKeySecret = "my secret";
    String bucket = "studytech";
    String aliyunURL = "https://studytech.oss-cn-zhangjiakou.aliyuncs.com/";

    @RequestMapping("/videoUpload")
    public String UploadPage(Model model) {
        return "uploadVideoPage";
    }

    @RequestMapping("/doVideoUpload")
    public String doupload(Model model, @RequestParam("files") MultipartFile[] files) throws IOException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        ApplicationUser au = applicationUserRepository.findByUsername(authentication.getName());
        StringBuilder fileNames = new StringBuilder();
        StringBuilder fileNotUpload = new StringBuilder();
        Path path;

        log.info("realPathtoUploads = {}", endpoint);

        for (MultipartFile file : files) {
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            String Objectname = file.getOriginalFilename();

            path = Paths.get(Objectname);
            String filetype = Files.probeContentType(path);
            VideoFile vf = videoFileRepository.findByFileName(Objectname);
            if (filetype.equals("video/mp4") && vf == null) {
                try {
                    ossClient.putObject(bucket, Objectname, file.getInputStream());
                    VideoFile document = new VideoFile(file.getOriginalFilename(), file.getContentType(),
                            aliyunURL + file.getOriginalFilename(), "test");
                    document.setAppuser(au);
                    au.getVideoFilelist().add(document);
                    videoFileRepository.save(document);
                    VideoFileInSolr solrdocument = new VideoFileInSolr(file.getOriginalFilename(),
                            aliyunURL + file.getOriginalFilename());
                    videoSolrRepo.save(solrdocument);
                    fileNames.append(file.getOriginalFilename() + " ");

                } catch (Exception ex) {
                    ex.printStackTrace();
                    System.out.println("user or file not found");

                } catch (Throwable e) {
                    e.printStackTrace();

                } finally {
                    ossClient.shutdown();
                }

            } else {
                fileNotUpload.append(file.getOriginalFilename() + " ");
            }
        }
        if (fileNames != null) {
            model.addAttribute("msg1", "Successfully uploaded files: " + fileNames.toString());
        }
        if (fileNotUpload != null) {
            model.addAttribute("msg2", "Files alread exist or file extension is not .mp4: " + fileNotUpload.toString());
        }
        return "uploadVideoPage";
    }
}

0 个答案:

没有答案