servlet怎么访问classes文件夹中的文件?

cityroom 2004-02-01 03:02:15
servlet与一个要读的文件同在classes文件夹中。

相对路径怎么写?

默认的是resin的安装目录,可是我自建了一个文件夹。
...全文
94 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Danken 2004-02-01
  • 打赏
  • 举报
回复
建议你使用我那样的,这样代码和资源就相对独立。
而如果你用相对路径,你代码换了位置,相对路径也就变了
cityroom 2004-02-01
  • 打赏
  • 举报
回复
哦?如果我的文件不在classes里面,

在我自建的根目录呢?

资料上介绍的:
Note : PATH variable in typo.java is the path to .ffi files.
It should be setup.(Depending on your servlet engine configuration)
Default is "./". (Do not forget the slash at the end).

是什么意思?必须象你那样吗?
Danken 2004-02-01
  • 打赏
  • 举报
回复
这样啊。
URL res = typo.class.getResource("/fileName");
String externalForm = res.toExternalForm(); //formatter "file:***"
File file = new File(externalForm.substring(5));
cityroom 2004-02-01
  • 打赏
  • 举报
回复
不可以啊

public class typo extends HttpServlet
{

public static final String PATH="/";
------------------------------------^^^^^^^^^^//这里
private fontgrabber _fg = null;
private fontinfo _fi = null;
private GifEncoder _gifEncoder = null;


/**
* Service
* Handles GET request-response
* @param HttpServletRequest, HttpServletResponse
*/
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,

IOException
{
res.setContentType("image/gif");
ServletOutputStream out=res.getOutputStream();
String fontName = req.getParameter("font");
String text = req.getParameter("text");
if ((text != null) && (fontName != null) && (!fontName.equals("")) && (!text.equals("")))
{
String Error = null;
Error = readBitmap(PATH+fontName+".ttf");
if (Error == null)
{
//Transparent => color < 0x08000000
_fg = new fontgrabber(_fi, text.toUpperCase(), 0x07000000);
_gifEncoder=new GifEncoder(_fg.getImageProd(),out);
_gifEncoder.encode();
}
else out.println(Error);
}
else out.println();
}

/**
* Reads Bitmap and font descriptor.
*/
private String readBitmap(String fn)
{
/*-- Load font descriptor --*/
FileInputStream fis = null;
ObjectInputStream ois = null;
try
{
fis = new FileInputStream(fn);
ois = new ObjectInputStream(fis);
_fi = (fontinfo)ois.readObject();
ois.close();
fis.close();
} catch (Exception e)
{
return "Cannot load font descriptor !";
}
return null;
}
}
Danken 2004-02-01
  • 打赏
  • 举报
回复
Class.getResource("/filename");

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧