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

查看:26
本文介绍了如何通过 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)

返回包含给定虚拟路径的真实路径的字符串.例如,路径 "/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).

那么,有没有其他方法可以将文件存储在这些服务器上,并且为 getServlet().getServletContext().getRealPath("") 返回 null

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天全站免登陆