我的图片放在C:/UploadedFiles/IMG.JPG中,我想在JSP上显示它,但我无法做到。请尽快给我一个解决方案。我试过但仍然无法正常工作。我不知道是否有任何联系问题。我正在使用NetBeans。
response.setContentType("image/jpeg");
OutputStream os =response.getOutputStream();
FileInputStream in=new FileInputStream(new File("D:\\pictures\\aayush.JPG"));
byte[] buf= new byte[2048];
int ch=in.read();
while(ch >= 0){
os.write(buf);
ch=in.read(buf);
}
in.close();
os.close();
答案 0 :(得分:0)
你的阅读不正确。首先读取1个字节,但写入整个缓冲区。然后你在缓冲区中读取字节,但不知道有多少,但你仍然写入整个缓冲区。
帮自己一个忙,并使用一种方法为你完成所有这些:Files.copy()。
答案 1 :(得分:0)
这是 servlet类 doGet
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("success.jsp");
request.setAttribute("imagePath", request.getContextPath() + "/image/test.jpg");
rd.forward(request, response);
}
这是您显示图片的 index.jsp
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<img src="${imagePath}" />
</body>
</html>
您的 图片 文件夹应位于 WebContent(此目录为ContextRoot)/image/test.jpg
下