把response.setContentType("text/html");改成
response.setContentType("image/jpeg");试试看

解决方案 »

  1.   

    java里图片显示,目前似乎不支持.jpg各式,而.gif格式可以。呵呵!
      

  2.   

    esponse.setContentType("image/jpeg;charset=GB2312");
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
                encoder.encode(image);
      

  3.   

    如果按照上面的做法,我想,对于其它格式的文件,就显示不出来了。所以,偶觉得这种做法值得商榷。
       以下是一段显示图片的函数
     public void createImage(String fileLocation) { 
      try { 
       FileOutputStream fos = new FileOutputStream(fileLocation); 
       BufferedOutputStream bos = new BufferedOutputStream(fos); 
       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos); 
       encoder.encode(image); 
       bos.close(); 
      } catch(Exception e) { 
       System.out.println(e); 
      } 
     } 
    你可以参考一下。
      

  4.   

    把response.setContentType("text/html");改成
    response.setContentType("image/jpeg");我用过的,可以。
      

  5.   

    回复人: Martin2002() ( ) 信誉:100  2003-08-09 07:53:00  得分:0 
    如果按照上面的做法,我想,对于其它格式的文件,就显示不出来了。所以,偶觉得这种做法值得商榷。
       以下是一段显示图片的函数
     public void createImage(String fileLocation) { 
      try { 
       FileOutputStream fos = new FileOutputStream(fileLocation); 
       BufferedOutputStream bos = new BufferedOutputStream(fos); 
       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos); 
       encoder.encode(image);               //image?
       bos.close();                      ---------------
      } catch(Exception e) { 
       System.out.println(e); 
      } 
     } 
    你可以参考一下。
      

  6.   

    response.setContentType("image/jpeg")
    这个我也用过但是好象不行,请高手指教
      

  7.   

    对不起,是我的问题写错了,
       response.setContentType("text/html");
    应是response.setContentType("image/jpeg"); 但这样也是不行的。
    另外scbb(星际宝贝)说的 
       esponse.setContentType("image/jpeg;charset=GB2312");
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
                encoder.encode(image);
    编译时提示:can't resolve symbol?
    快帮帮我呀!
      

  8.   

    刚才忘记说明了。
     BufferedImage image; 
     public void createImage(String fileLocation) { 
      try { 
       FileOutputStream fos = new FileOutputStream(fileLocation); 
       BufferedOutputStream bos = new BufferedOutputStream(fos); 
       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos); 
       encoder.encode(image); 
       bos.close(); 
      } catch(Exception e) { 
       System.out.println(e); 
      } 
     } 
    其中image是java.awt.image包中的BufferdImage对象
      

  9.   

    我加上了import java.awt.image.*;但仍然提示can't  resolve  symbol? 
     symbol:class JPEGImageEncoder  
    怎么回事呢?martin2002,能不能要加你为好友呀?QQ:183934798
      

  10.   

    我的msn:[email protected]
    我还以为你用的是一些IDE工具呢,
    JPEGImageEncoder是在import com.sun.image.codec.jpeg.*;包中的一个类
      

  11.   

    可能是我真的很笨吧,现在没语法错误了,但是tomcat提示null pointer exception.我把源程序写出来吧,帮我看一下,这个servlet就是想把jsp传过来的id对应的图片显示出来。其中注释的为我以前的代码,看到的是经指导后修改的代码。
    import java.io.*; 
    import java.sql.*; 
    import javax.sql.*; 
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.awt.image.*;
    import com.sun.image.codec.jpeg.*;
    public class LobServlet extends HttpServlet{
      
      protected void doGet(HttpServletRequest request,HttpServletResponse response)
      throws ServletException, IOException
      {
        ServletOutputStream out = response.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(out);     String dataType = request.getParameter("type");
        int id = Integer.parseInt(request.getParameter("id"));
        BufferedImage  image=null;        
        if(dataType.equalsIgnoreCase("blob")){
          //response.setContentType("image/jpeg");
          response.setContentType("image/jpeg;charset=GB2312");
                JPEGImageEncoder  encoder  =  JPEGCodec.createJPEGEncoder(bos);    
                encoder.encode(image);
                      bos.write(getBlob(id));
          
        }else if(dataType.equalsIgnoreCase("clob")){
          response.setContentType("text/html");
          out.write(getClob(id));
        } 
        out.flush();
        out.close();
      }
      public byte[] getBlob(int id){
        String query = "SELECT Image FROM Photo WHERE imageid = ?";
        Blob blob = null; 
        byte[] bytes = null;
        String description = "";
        try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      String m_url="jdbc:oracle:thin:@localhost:1521:mydb";
      String m_user="yz_postboard";
      String m_pass="yz";
      Connection con=DriverManager.getConnection(m_url,m_user,m_pass);
          
          PreparedStatement pstmt = con.prepareStatement(query);
          pstmt.setInt(1, id);  
             
          ResultSet rs = pstmt.executeQuery();
          ResultSetMetaData md = rs.getMetaData();
          while (rs.next()) {
            blob = rs.getBlob(1);
          }
          bytes = blob.getBytes( 1, (int)(blob.length()));  
          con.close();
        }
        catch(ClassNotFoundException e){
          e.printStackTrace();
        }
        catch(SQLException e){
          e.printStackTrace();
        }
        return bytes;
      }
      public byte[] getClob(int id){
        String query = "SELECT Document FROM Documents WHERE Id = ?";
        Clob clob = null; 
        String text = null;
        try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
      String m_url="jdbc:oracle:thin:@localhost:1521:mydb";
      String m_user="yz_postboard";
      String m_pass="yz";
      Connection con=DriverManager.getConnection(m_url,m_user,m_pass);
          
          PreparedStatement pstmt = con.prepareStatement(query);
          pstmt.setInt(1, id);  
             
          ResultSet rs = pstmt.executeQuery();
          ResultSetMetaData md = rs.getMetaData();
         
          while (rs.next()) {
            clob = rs.getClob(1);
          }
          text = clob.getSubString(1,((int)clob.length()));
          con.close();
        }
        catch(ClassNotFoundException e){
          e.printStackTrace();
        }
        catch(SQLException e){
          e.printStackTrace();
        }
        byte[] bytes = null;
        if(text!=null)bytes = text.getBytes();
        return bytes;
      }
    }  
      

  12.   

    我知道你是什么错误了,你在代码中没有创建BufferedImage的实例对象,而只是给了一个null,可能是我误导你了。你应该先创建一个BufferedImage对象
    BufferedImage image = new BufferedImage(width, 
    height, BufferedImage.TYPE_INT_RGB);
    就像这样,具体的方法,你可以采用其他构造函数,查阅相应的API
      

  13.   

    具体使用的例子可以看:
    http://www.yesky.com/20030402/1660717.shtml
      

  14.   

    谢谢martin,我关键是不太明白你写的那段程序的用意何在
      

  15.   

    你有msn吗,在这里交流太麻烦了,
      

  16.   

    那篇文章说的是画图吧,如果在数据库里已经存有blob图像数据的话,还用建bufferedimage吗?
      

  17.   

    会不会是向里面存数据的时候需要经过一些处理,例如判断它的格式,然后存一些标记,然后读的时候再读这些标记,根据标记进行处理呢,我以前用PB,这样做过,但java就不清楚了。
      

  18.   

    谢谢大家,我的问题解决了。
    向oracle里面写blob数据有四种方法吧,有些方法写的大小有限制。而我用的正好是这种方法。改成别的方法就行了。与图片是否是gif和jpg没有关系。jpg照样能显示。当然别的也行,像word,pdf等。
      

  19.   

    能告诉我解决方法吗??
    谢谢了
    [email protected]