Tomcat服务器在war webapp中的绝对文件访问 [英] Tomcat server absolute file access in war webapp
问题描述
我有一个Spring webapp,它的 .war
文件已经上传到Tomcat服务器。大部分基本功能都是按照预期工作 - 页面浏览和表单提交。现在我的问题是,我的web应用程序需要读取和写入文件,我不知道如何实现这一点(文件I / O返回显示java.lang.NullPointerException
)。
我使用以下代码来获取给定文件的绝对路径建议通过 Titi Wangsa Bin Damhore 了解相对于服务器的路径:
HttpSession session = request.getSession();
ServletContext sc = session.getServletContext();
String file = sc.getRealPath(src / test.arff);
logger.info(文件路径:+文件);
以下是输出路径:
/home/username/tomcat/webapps/appname/src/test.arff
但是当我通过 WinSCP 检查文件目录时,文件的实际路径是:
/home/username/tomcat/webapps/appname/WEB-INF/classes/test.arff
以下是我的问题:
- 如何将这些路径转换为像
C:/Users/Workspace/appname/src/test.arff
机器,完美的作品)?它的服务器是 Apache Tomcat 6.0.35
和 Apache Tomcat 6.0.35
。
- 为什么代码返回与实际路径不同的路径?
- 如果文件I / O不适用,我可以使用哪些替代方法?
<我只需要访问两个文件(每个<1MB),所以我不认为我可能需要使用数据库来包含它们,如所示。在这个线程中减去 /减去。
文件I / O
以下是我用来访问我需要的文件的代码。
BufferedWriter writer;
尝试{
URI uri = new URI(/ test.arff);
writer = new BufferedWriter(new FileWriter(
calcModelService.getAbsolutePath()+ uri));
writer.write(data.toString());
writer.flush();
writer.close();
} catch(IOException e){
e.printStackTrace();
} catch(URISyntaxException e){
e.printStackTrace();
解决方案 p>
ServletContext application = ...;
InputStream in = null;
尝试{
in = application.getResourceAtStream(/ WEB-INF / web.xml); //示例
//读取文件
} finally {
if(null!= in)try {in.close(); }
catch(IOException ioe){/ * log this * /}
}
<要写文件:
ServletContext application = ...;
文件tmpdir =(File)application.getAttribute(javax.servlet.context.tempdir);
if(null == tmpdir)
throw new IllegalStateException(容器不提供临时目录); //或者另外处理
文件targetFile = new File(tmpDir,my-temp-filename.txt);
BufferedWriter out = null;
尝试{
out = new BufferedWriter(new FileWriter(targetFile));
//写入输出流
} finally {
if(null!= out)try {out.close(); }
catch(IOException ioe){/ * log this * /}
}
<如果你不想使用servlet容器提供的tmpdir,那么你应该使用完全在servlet上下文的purvue之外的某个地方,比如 / path / to / temporary / files 或类似的东西。你绝对不希望使用容器的临时目录,除了真正的临时文件,可以删除重新部署等。
I have a Spring webapp whose .war
file has been uploaded to a Tomcat server. Most of the basic functions are working as intended - page views and form submission.
My problem now is that my webapp needs to read and write files and I am clueless as to how I can achieve this (the file I/O returns java.lang.NullPointerException
).
I used the following code to get the absolute path of a given file suggested by Titi Wangsa Bin Damhore to know the path relative to the server:
HttpSession session = request.getSession();
ServletContext sc = session.getServletContext();
String file = sc.getRealPath("src/test.arff");
logger.info("File path: " + file);
Here is the output path:
/home/username/tomcat/webapps/appname/src/test.arff
But when I checked the file directory via WinSCP, the file's actual path is:
/home/username/tomcat/webapps/appname/WEB-INF/classes/test.arff
Here are my questions:
- How do I transform these paths into something like
C:/Users/Workspace/appname/src/test.arff
(the original path in my local machine that works perfectly)? It's servers are Apache Tomcat 6.0.35
and Apache Tomcat 6.0.35
.
- Why is the code returning a different path as opposed to the actual path?
- If file I/O is not applicable, what alternatives can I use?
PS I just need to access two files (< 1MB each) so I don't think I may need to use a database to contain them as suggested by minus in this thread.
File I/O
Below is the code I use for accessing the file I need.
BufferedWriter writer;
try {
URI uri = new URI("/test.arff");
writer = new BufferedWriter(new FileWriter(
calcModelService.getAbsolutePath() + uri));
writer.write(data.toString());
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
解决方案 To read files:
ServletContext application = ...;
InputStream in = null;
try {
in = application.getResourceAtStream("/WEB-INF/web.xml"); // example
// read your file
} finally {
if(null != in) try { in.close(); }
catch (IOException ioe) { /* log this */ }
}
To write files:
ServletContext application = ...;
File tmpdir = (File)application.getAttribute("javax.servlet.context.tempdir");
if(null == tmpdir)
throw new IllegalStateException("Container does not provide a temp dir"); // Or handle otherwise
File targetFile = new File(tmpDir, "my-temp-filename.txt");
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(targetFile));
// write to output stream
} finally {
if(null != out) try { out.close(); }
catch (IOException ioe) { /* log this */ }
}
If you don't want to use the tmpdir provided by the servlet container, then you should use someplace that is entirely outside of the servlet context's purvue, like /path/to/temporary/files
or something like that. You definitely don't want to use the container's temporary directory for anything other than truly temporary files which are okay to delete on re-deployment, etc.
这篇关于Tomcat服务器在war webapp中的绝对文件访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!