【发布时间】:2014-11-15 22:00:21
【问题描述】:
我想向用户展示一些图片,这些图片位于此处:
C:\Users\advert_images
我正在使用 primefaces 并使用 serverlet 显示:
@WebServlet("/advert_images/*")
public class ImagesServlet extends HttpServlet {
@Override
protected
void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = request.getPathInfo().substring(1);
File file = new File("C:\\Users\\advert_images\\", filename);
response.setHeader("Content-Type", getServletContext().getMimeType(filename));
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
Files.copy(file.toPath(), response.getOutputStream());
}
}
所以当我在浏览器中输入这个:
http://localhost:8080/vas-plus/advert_images/3.jpg
我得到了我的图像。
但是当我尝试在我的 xhtml 中写代码时
<img src ="http://localhost:8080/vas-plus/advert_images/3.jpg"/>
或者:
<h:graphicImage class="test" value="/advert_images/3.jpg"/>
我没有显示图像。还有一些作为 html 输出的东西:
<img src="/vas-plus/advert_images/3.jpg" class="test" width="0" height="0" style="display: none !important; visibility: hidden !important; opacity: 0 !important; background-position: 1px 1px;">
当我使用 Google Chrome developerTools 查看 html 页面的资源时,我看到:
所以我有点困惑任何想法?
【问题讨论】:
-
为什么你不想把它放到你的项目中?