如何在基于servlet的Web应用程序中临时保存生成的文件

时间:2021-09-07 09:54:53

I am trying to generate a XML file and save it in /WEB-INF/pages/.

我正在尝试生成XML文件并将其保存在/ WEB-INF / pages /中。

Below is my code which uses a relative path:

下面是我使用相对路径的代码:

File folder = new File("src/main/webapp/WEB-INF/pages/");StreamResult result = new StreamResult(new File(folder, fileName));

It's working fine when running as an application on my local machine (C:\Users\userName\Desktop\Source\MyProject\src\main\webapp\WEB-INF\pages\myFile.xml).

在我的本地计算机上作为应用程序运行时它工作正常(C:\ Users \ userName \ Desktop \ Source \ MyProject \ src \ main \ webapp \ WEB-INF \ pages \ myFile.xml)。

But when deploying and running on server machine, it throws the below exception:

但是当在服务器机器上部署和运行时,它会引发以下异常:

javax.xml.transform.TransformerException: java.io.FileNotFoundException C:\project\eclipse-jee-luna-R-win32-x86_64\eclipse\src\main\webapp\WEB INF\pages\myFile.xml

javax.xml.transform.TransformerException:java.io.FileNotFoundException C:\ project \ eclipse-jee-luna-R-win32-x86_64 \ eclipse \ src \ main \ webapp \ WEB INF \ pages \ myFile.xml

I tried getServletContext().getRealPath() as well, but it's returning null on my server. Can someone help?

我也尝试了getServletContext()。getRealPath(),但它在我的服务器上返回null。有人可以帮忙吗?

2 个解决方案

#1


22  

Never use relative local disk file system paths in a Java EE web application such as new File("filename.xml"). For an in depth explanation, see also getResourceAsStream() vs FileInputStream.

切勿在Java EE Web应用程序(如新文件(“filename.xml”))中使用相对本地磁盘文件系统路径。有关深入解释,另请参阅getResourceAsStream()与FileInputStream。

Never use getRealPath() with the purpose to obtain a location to write files. For an in depth explanation, see also What does servletcontext.getRealPath("/") mean and when should I use it.

切勿使用getRealPath()来获取写入文件的位置。有关深入解释,请参阅servletcontext.getRealPath(“/”)的含义以及何时使用它。

Never write files to deploy folder anyway. For an in depth explanation, see also Recommended way to save uploaded files in a servlet application.

永远不要将文件写入部署文件夹。有关深入说明,另请参阅在servlet应用程序中保存上载文件的建议方法。

Always write them to an external folder on a predefined absolute path.

始终将它们写入预定义绝对路径上的外部文件夹。

  • Either hardcoded:

    File folder = new File("/absolute/path/to/web/files");File result = new File(folder, "filename.xml");// ...
  • 硬编码:File folder = new File(“/ absolute / path / to / web / files”); File result = new File(folder,“filename.xml”); // ...

  • Or configured in one of many ways:

    或者以多种方式之一配置:

    File folder = new File(System.getProperty("xml.location"));File result = new File(folder, "filename.xml");// ...
  • Or making use of container-managed temp folder:

    或者使用容器管理的临时文件夹:

    File folder = (File) getServletContext().getAttribute(ServletContext.TEMPDIR);File result = new File(folder, "filename.xml");// ...
  • Or making use of OS-managed temp folder:

    或者使用OS管理的临时文件夹:

    File result = File.createTempFile("filename-", ".xml");// ...

The alternative is to use a (embedded) database.

另一种方法是使用(嵌入式)数据库。

See also:

#2


-2  

just use

File relpath = new File(".\pages\");

File relpath = new File(“。\ pages \”);

as application cursor in default stay into web-inf folder.

默认情况下,应用程序游标停留在web-inf文件夹中。

#1


22  

Never use relative local disk file system paths in a Java EE web application such as new File("filename.xml"). For an in depth explanation, see also getResourceAsStream() vs FileInputStream.

切勿在Java EE Web应用程序(如新文件(“filename.xml”))中使用相对本地磁盘文件系统路径。有关深入解释,另请参阅getResourceAsStream()与FileInputStream。

Never use getRealPath() with the purpose to obtain a location to write files. For an in depth explanation, see also What does servletcontext.getRealPath("/") mean and when should I use it.

切勿使用getRealPath()来获取写入文件的位置。有关深入解释,请参阅servletcontext.getRealPath(“/”)的含义以及何时使用它。

Never write files to deploy folder anyway. For an in depth explanation, see also Recommended way to save uploaded files in a servlet application.

永远不要将文件写入部署文件夹。有关深入说明,另请参阅在servlet应用程序中保存上载文件的建议方法。

Always write them to an external folder on a predefined absolute path.

始终将它们写入预定义绝对路径上的外部文件夹。

  • Either hardcoded:

    File folder = new File("/absolute/path/to/web/files");File result = new File(folder, "filename.xml");// ...
  • 硬编码:File folder = new File(“/ absolute / path / to / web / files”); File result = new File(folder,“filename.xml”); // ...

  • Or configured in one of many ways:

    或者以多种方式之一配置:

    File folder = new File(System.getProperty("xml.location"));File result = new File(folder, "filename.xml");// ...
  • Or making use of container-managed temp folder:

    或者使用容器管理的临时文件夹:

    File folder = (File) getServletContext().getAttribute(ServletContext.TEMPDIR);File result = new File(folder, "filename.xml");// ...
  • Or making use of OS-managed temp folder:

    或者使用OS管理的临时文件夹:

    File result = File.createTempFile("filename-", ".xml");// ...

The alternative is to use a (embedded) database.

另一种方法是使用(嵌入式)数据库。

See also:

#2


-2  

just use

File relpath = new File(".\pages\");

File relpath = new File(“。\ pages \”);

as application cursor in default stay into web-inf folder.

默认情况下,应用程序游标停留在web-inf文件夹中。