上传一个文件,Spring MVC + CloudBees [英] Uploading a file, Spring MVC + CloudBees

查看:230
本文介绍了上传一个文件,Spring MVC + CloudBees的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我在控制器中有这样一段代码,

  @RequestMapping(/ main / admin / upload)
public ModelAndView fileUploaded(
@ModelAttribute (uploadedFile)FileUpload uploadedFile,
final HttpServletRequest请求,
final HttpServletResponse响应,BindingResult结果){

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

尝试{
inputStream = file.getInputStream();

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

while
}
} catch(IOException e){
e.printStackTrace();
}

返回新的ModelAndView(redirect:/ main / admin);
}

我不确定这一行是否正常工作:


$ b $ $ p $ request.getSession()。getServletContext()。getRealPath(/)

我确定文件夹被创建,因为我已经在我的eclipse项目中手动添加了它们。我还在提取到CloudBees之前手动添加了一些文件到这些文件夹,这些文件是可以访问的,但是当我尝试使用上面的代码上传文件时,它不起作用。

  • b
    $ b

    重新部署/向外扩展的应用程序将从一个不同的节点开始,因此您不应该在那里存储文件。我们建议使用amazon S3(或相当的文件存储)进行存储,并使用本地(临时)文件系统作为临时缓存。

    您可以使用系统。 getProperty(java.io.tempDir)获取配置的本地temp。



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



    更多信息 here


    I have worked on local Tomcat's server and now I've just deployed my application to CloudBees. Everything works great apart from uploading a file to the server's directory.

    I have such a piece of code in my controller, which worked for me on localhost:

    @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");
    }
    

    I'm not sure if this line is working correctly:

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

    I'm sure folders are created, because I have added them manually in my eclipse project. I've also added manually some files to these folders before extracting to CloudBees and these files are accessible, but when I try to upload file using above code, it doesn't work.

    解决方案

    Filesystem is not persistent on the cloud, so it is NOT a good practice to upload files to the file system in the same way you on your hard disk.

    Application being redeployed/scale-out will start on a distinct node, so you shouldn't store files there. We recommend to store using amazon S3 (or comparable file store) and use local ("ephemeral") filesystem as a temporary cache.

    You can use System.getProperty("java.io.tempDir") to get the configured local temp.

    CloudBees also provides two different examples which could help you to upload files to Amazon S3 -you can also use Dropbox, Google Drive,..-.

    More info here.

    这篇关于上传一个文件,Spring MVC + CloudBees的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆