日期:2014-05-18  浏览次数:20633 次

我有一个文件想放到war中,并在Servlet中访问它,应该怎么做

我有一个文件想放到war中,并在Servlet程序中访问它,应该怎么做


谢谢

------解决方案--------------------
如果lz的war包已经建好,想添加文件可以直接使用winrar。
Servlet程序访问要看你的文件路径是如何建立的。
------解决方案--------------------
war包名称/xxx.pdf
------解决方案--------------------
lz使用filePath路径写入文件:

String realPath = this.getServletContext().getRealPath(
this.getServletName());
realPath = realPath.substring(0, realPath.lastIndexOf( "\\ "));
String filePath = realPath + "\\xxx.pdf ";
System.out.println(filePath);
------解决方案--------------------
Servlet中读取.pdf文件。

String realPath = this.getServletContext().getRealPath(
this.getServletName());
realPath = realPath.substring(0, realPath.lastIndexOf( "\\ "));
String filePath = realPath + "\\xxx.pdf ";
//System.out.println(filePath);

FileInputStream fstream = null;
try {
fstream = new FileInputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

DataInputStream in = new DataInputStream(fstream);

try {
while (in.available() != 0)
{
System.out.println(in.readLine());
}
} catch (IOException e)
{
e.printStackTrace();
}

try {
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
------解决方案--------------------
String realPath = this.getServletContext().getRealPath(this.getServletName());

应该可行...