【问题标题】:Uploading a file, Spring MVC + CloudBees上传文件,Spring MVC + CloudBees
【发布时间】:2014-02-05 23:14:18
【问题描述】:

我在本地 Tomcat 的服务器上工作过,现在我刚刚将我的应用程序部署到 CloudBees。 除了将文件上传到服务器目录之外,一切都很好。

我的控制器中有这样一段代码,它在本地主机上对我有用:

@RequestMapping("/main/admin/upload")
public ModelAndView fileUploaded(
        @ModelAttribute("uploadedFile") FileUpload uploadedFile,
        final HttpServletRequest request,
        final HttpServletResponse response, BindingResult result) {

    InputStream inputStream = null;
    OutputStream outputStream = null;
    String pdfDirectory = uploadedFile.getPdfDirectory();
    MultipartFile file = uploadedFile.getFile();
    String fileName = file.getOriginalFilename();

    try {
        inputStream = file.getInputStream();

        String pdfFileDirectory = request.getSession().getServletContext()
                .getRealPath("/")
                + "resources\\pdf\\" + pdfDirectory + "\\";
        File newFile = new File(pdfFileDirectory + fileName);
        if (!newFile.exists()) {
            newFile.createNewFile();
        }
        outputStream = new FileOutputStream(newFile);
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return new ModelAndView("redirect:/main/admin");
}

我不确定这条线是否正常工作:

request.getSession().getServletContext().getRealPath("/")

我确定文件夹已创建,因为我已在我的 Eclipse 项目中手动添加它们。在解压到 CloudBees 之前,我还手动将一些文件添加到这些文件夹中,并且这些文件可以访问,但是当我尝试使用上述代码上传文件时,它不起作用。

【问题讨论】:

  • 我发现了一件事......我不确定是否有效的行返回:/mnt/genapp/apps/7c7c897e/staxcat/install/webapp.war/resources/pdf/Facebook/有没有办法将文件添加到提取到war文件的应用程序中?

标签: jakarta-ee spring-mvc file-upload cloudbees


【解决方案1】:

文件系统在云上不是持久的,因此以与硬盘上相同的方式将文件上传到文件系统不是一个好习惯。

正在重新部署/横向扩展的应用程序将在不同的节点上启动,因此您不应将文件存储在那里。我们建议使用 amazon S3(或类似的文件存储)进行存储,并使用本地(“临时”)文件系统作为临时缓存。

您可以使用 System.getProperty("java.io.tempDir") 来获取配置的本地温度。

CloudBees 还提供了两个不同的示例,可以帮助您将文件上传到 Amazon S3 - 您还可以使用 Dropbox、Google Drive、..-.

更多信息here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 2016-06-10
    • 2018-03-20
    相关资源
    最近更新 更多