当前位置: 代码迷 >> J2SE >> ,关于servlet输出图象的有关问题
  详细解决方案

,关于servlet输出图象的有关问题

热度:53   发布时间:2016-04-24 14:38:43.0
求助,关于servlet输出图象的问题
我这样写:

public   void   doGet(HttpServletRequest   request,   HttpServletResponse   response)throws   ServletException,   IOException   {

response.setHeader( "Pragma ",   "no-cache ");
response.setHeader( "Cache-Control ",   "no-cache ");
response.setDateHeader( "Expires ",   0);
response.setContentType( "image/jpeg ");

ServletOutputStream   sos   =   response.getOutputStream();
BufferedImage   bi=ImageIO.read(new   File( "imageNotFound.jpg "));
File   f   =   new   File( "abc.jpg ");  

ImageIO.write(bi, "JPG ",sos);
ImageIO.write(bi, "JPG ",f);
sos.close();
}

输入6.41k的jpeg图片
输出到文件里变成2.56k的jpeg图片
输出到ServletOutputStream里,却变为了25.8k的bmp图片

请问为什么啊?怎么解决?谢谢

------解决方案--------------------
不要用Image,直接用FileInputStream读入后写出到你的sos里面去。
------解决方案--------------------
try this.没有测试过,以前也没有写过
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setHeader( "Pragma ", "no-cache ");
response.setHeader( "Cache-Control ", "no-cache ");
response.setDateHeader( "Expires ", 0);
response.setContentType( "image/jpeg ");

ServletOutputStream sos = response.getOutputStream();
FileInputStream bis = new FileInputStream(new File( "imageNotFound.jpg ")));

Byte c;
while((c=bis.readByte()) != -1)
sos.write(c);
}
bis.close();
sos.close();
  相关解决方案