当前位置: 代码迷 >> Java Web开发 >> 小弟我有一个文件想放到war中,并在Servlet中访问它,应该如何做
  详细解决方案

小弟我有一个文件想放到war中,并在Servlet中访问它,应该如何做

热度:26   发布时间:2016-04-17 14:42:24.0
我有一个文件想放到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());

应该可行...
  相关解决方案