如何从文件系统调用图像?
我正在使用eclipse WTP在部署在tomcat服务器上的Ubuntu OS上开发Web应用程序。 我想在Web应用程序中使用我的文件系统中的图像(显示它们)。 我怎样才能有效地做到这一点? 是通过使用上下文路径到驱动器上的位置? 或者是通过使用流媒体加载它们(或类似的东西)? 此外,我在WTP项目中找不到任何web.xml或server.xml文件(因为较新的版本甚至不需要它们)。
改述:我想在我的网络应用程序中使用文件系统中的图像(静态内容)。 在前端使用JSTL。
编辑:
如果网络应用程序是xyz
那么它的位置是: /home/webaapp/xyz/.....
并且图像位于/home/akshay/images/.......
我想从Web应用程序访问远离(在相同的硬盘驱动器中)的文件夹
您可以使用Tomcat Default Servlet
来提供静态资源。
web.xml中:
default /resources/*
最好使用c:url
并在值中使用prefix /
来使url相对于上下文路径。
动态项目结构:
WebContent | |__resources | | | |__images | | | |__logo.png | |__WEB-INF | |__web.xml
编辑
由于图像不是战争的一部分,因此您可以尝试使用Servlet。 只需将路径存储在属性文件中的某个位置,或将其作为VM参数传递或使其保持不变。
JSP:
Servlet的:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String imageName=request.getParameter("name") String path = "absolute path of the image directory"+imageName; BufferedInputStream inputStream = null; try { inputStream = new BufferedInputStream(new FileInputStream(new File(path))); OutputStream outputStream = response.getOutputStream(); byte[] bytes = new byte[1024 * 2]; int bytesRead = -1; while ((bytesRead = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, bytesRead); } outputStream.flush(); } finally { if (inputStream != null) { inputStream.close(); } } }
如果您使用的是Java 7,请使用try-with-resources语句来处理资源。