我有问题。我需要从servlet向jsp发送一张图片。 我在jsp中得到一张照片,但每张照片都被剪掉了。不完整的图片,但像80-90%,它可以是什么? TY。
这是一个servlet代码:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String login = request.getParameter("login");
String password = request.getParameter("password");
System.out.println(login);
System.out.println(password);
FileInputStream fis = new FileInputStream(new File("D:\\Projects\\Java\\WebApplication4\\web\\gavno.jpg"));
BufferedInputStream bis = new BufferedInputStream(fis);
response.setContentType("image/jpeg");
BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());
for (int data; (data = bis.read()) != -1;) {
output.write(data);
}
request.getRequestDispatcher("WEB-INF/page.jsp").forward(request, response);
}
答案 0 :(得分:1)
找到答案:
response.setContentType("image/jpeg");
ServletOutputStream out;
out = response.getOutputStream();
FileInputStream fin = new FileInputStream("c:\\test\\java.jpg");
BufferedInputStream bin = new BufferedInputStream(fin);
BufferedOutputStream bout = new BufferedOutputStream(out);
int ch =0; ;
while((ch=bin.read())!=-1)
{
bout.write(ch);
}
bin.close();
fin.close();
bout.close();
out.close();