如何通过Java EE Web应用程序在服务器(Web容器)上存储文件? [英] How to store a file on a server(web container) through a Java EE web application?

查看:151
本文介绍了如何通过Java EE Web应用程序在服务器(Web容器)上存储文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个Java EE Web应用程序。该应用程序允许用户在浏览器的帮助下上传文件。一旦用户上传了他的文件,该应用程序首先将上传的文件存储在服务器上(运行它),然后对其进行处理。

I have developed a Java EE web application. This application allows a user to upload a file with the help of a browser. Once the user has uploaded his file, this application first stores the uploaded file on the server (on which it is running) and then processes it.

目前,我是将文件存储在服务器上,如下所示:

At present, I am storing the file on the server as follows:

try {
    // formFile represents the uploaded file
    FormFile formFile = programForm.getTheFile();
    String path = getServlet().getServletContext().getRealPath("") + "/"
        + formFile.getFileName();
    System.out.println(path);
    file = new File(path);
    outputStream = new FileOutputStream(file);
    outputStream.write(formFile.getFileData());
}

其中, formFile 表示上传的文件。

where, the formFile represents the uploaded file.

现在,问题是它在某些服务器上运行正常,但在某些服务器上运行 getServlet()。getServletContext ()。getRealPath()返回 null 所以我得到的最终路径是 null / filename 并且文件没有存储在服务器上。

Now, the problem is that it is running fine on some servers but on some servers the getServlet().getServletContext().getRealPath("") is returning null so the final path that I am getting is null/filename and the file doesn't store on the server.

当我检查API ServletContext.getRealPath() 方法,我发现了以下内容:

When I checked the API for ServletContext.getRealPath() method, I found the following:


public java.lang.String getRealPath(java。 lang.String path)

返回包含给定虚拟路径的实际路径的String。例如,路径/ index.html返回服务器文件系统上的绝对文件路径,该请求将由http:/ /host/contextPath/index.html,其中contextPath是此ServletContext的上下文路径。

Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext.

返回的实际路径将位于适用于运行servlet容器的计算机和操作系统的表单,包括正确的路径分隔符。如果servlet容器由于任何原因(例如,当.war存档中提供内容时)无法将虚拟路径转换为实际路径,则此方法返回null。

The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

那么,还有其他方法我可以在那些服务器上存储文件吗? null getServlet()。getServletContext()。getRealPath()

So, Is there any other way by which I can store files on those servers also which is returning null for getServlet().getServletContext().getRealPath("")

推荐答案

写入文件不建议使用Java EE容器中的系统,特别是如果您需要处理书面数据:

Writing to the file system from a Java EE container is not really recommended, especially if you need to process the written data:


  • 它不是事务性的

  • 它会损害可移植性(如果您在群集环境中会怎样)

  • 它需要为目标位置设置外部参数

如果这是一个选项,我会将文件存储在数据库中或使用JCR存储库(如 Jackrabbit

If this is an option, I would store the files in database or use a JCR repository (like Jackrabbit).

这篇关于如何通过Java EE Web应用程序在服务器(Web容器)上存储文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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