为什么jpg的图像从数据库里下载下来后不可显示,而gif的可以?谢谢 把response.setContentType("text/html");改成response.setContentType("image/jpeg");试试看 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java里图片显示,目前似乎不支持.jpg各式,而.gif格式可以。呵呵! esponse.setContentType("image/jpeg;charset=GB2312"); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output); encoder.encode(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); } } 你可以参考一下。 把response.setContentType("text/html");改成response.setContentType("image/jpeg");我用过的,可以。 回复人: 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); } } 你可以参考一下。 response.setContentType("image/jpeg")这个我也用过但是好象不行,请高手指教 对不起,是我的问题写错了, 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?快帮帮我呀! 刚才忘记说明了。 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对象 我加上了import java.awt.image.*;但仍然提示can't resolve symbol? symbol:class JPEGImageEncoder 怎么回事呢?martin2002,能不能要加你为好友呀?QQ:183934798 我的msn:[email protected]我还以为你用的是一些IDE工具呢,JPEGImageEncoder是在import com.sun.image.codec.jpeg.*;包中的一个类 可能是我真的很笨吧,现在没语法错误了,但是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; }} 我知道你是什么错误了,你在代码中没有创建BufferedImage的实例对象,而只是给了一个null,可能是我误导你了。你应该先创建一个BufferedImage对象BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);就像这样,具体的方法,你可以采用其他构造函数,查阅相应的API 具体使用的例子可以看:http://www.yesky.com/20030402/1660717.shtml 谢谢martin,我关键是不太明白你写的那段程序的用意何在 你有msn吗,在这里交流太麻烦了, 那篇文章说的是画图吧,如果在数据库里已经存有blob图像数据的话,还用建bufferedimage吗? 会不会是向里面存数据的时候需要经过一些处理,例如判断它的格式,然后存一些标记,然后读的时候再读这些标记,根据标记进行处理呢,我以前用PB,这样做过,但java就不清楚了。 谢谢大家,我的问题解决了。向oracle里面写blob数据有四种方法吧,有些方法写的大小有限制。而我用的正好是这种方法。改成别的方法就行了。与图片是否是gif和jpg没有关系。jpg照样能显示。当然别的也行,像word,pdf等。 能告诉我解决方法吗??谢谢了[email protected] SVN点击同步、更新报错 ssh架构,mysql数据库,页面插入后数据库为空 跪求《Java专业编程指南》(电子工业出版社 Brett Spell著)一书的源代码 救急!怎样用将文字转成ASCII码!在线等待。 文件判断的问题 javascript问题 看看这段代码:建立数据库连接的javabeans,哪里错了????????? 是编码问题吗? jsp网站编程的安全控制如何进行? 各位大虾:急!急!急! jsp的bean文件应该放在哪里啊??? 数据库乱码
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(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);
}
}
你可以参考一下。
response.setContentType("image/jpeg");我用过的,可以。
如果按照上面的做法,我想,对于其它格式的文件,就显示不出来了。所以,偶觉得这种做法值得商榷。
以下是一段显示图片的函数
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);
}
}
你可以参考一下。
这个我也用过但是好象不行,请高手指教
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?
快帮帮我呀!
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对象
symbol:class JPEGImageEncoder
怎么回事呢?martin2002,能不能要加你为好友呀?QQ:183934798
我还以为你用的是一些IDE工具呢,
JPEGImageEncoder是在import com.sun.image.codec.jpeg.*;包中的一个类
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;
}
}
BufferedImage image = new BufferedImage(width,
height, BufferedImage.TYPE_INT_RGB);
就像这样,具体的方法,你可以采用其他构造函数,查阅相应的API
http://www.yesky.com/20030402/1660717.shtml
向oracle里面写blob数据有四种方法吧,有些方法写的大小有限制。而我用的正好是这种方法。改成别的方法就行了。与图片是否是gif和jpg没有关系。jpg照样能显示。当然别的也行,像word,pdf等。
谢谢了
[email protected]