把图片存到mysql数据库中,然后再把图片读出来输出到文件中.
为什么打开这个图片文件后显示的是乱码if (rs.next()) {
Blob blb = rs.getBlob("图片");
new FileOutputStream(
"C:\\Documents and Settings\\Administrator\\桌面\\123.gif")
.write(blb.getBytes(1l, (int)blb.length()));
}
为什么打开这个图片文件后显示的是乱码if (rs.next()) {
Blob blb = rs.getBlob("图片");
new FileOutputStream(
"C:\\Documents and Settings\\Administrator\\桌面\\123.gif")
.write(blb.getBytes(1l, (int)blb.length()));
}
解决方案 »
- String.format()方法处理中文字符
- java泛型
- 关于MouseMoveListener监测到的鼠标坐标的问题?
- java split 分割问题
- 同类却不同转换 java.lang.ClassCastException: dynamic.test2.Personal cannot be cast to dynamic.test2.Personal
- 高分求救,关于byte转换char问题
- 100分求代码!从文本文件中按照时间间隔(不定长)一行行读出,并在TextArea中依次显示出来(注:功能像迷你歌词一样)
- 菜鸟:replaceAll如何替换"aaa[bb"中的"["啊!
- 哪里可以下载教JAVA的课堂录音
- 谁知道有工控仪表类的Applet吗?急啊
- 监听器只能监听一次的问题
- 一个死锁的实例,关于管程(锁)的疑问。我太晕了。。
FileOutputStream out=new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\123.gif");
// File file = new File("newFile.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //JPEG编码
out.close();
File f = new File("a.jpg");
OutPutStream out = new BufferedOutPutStream(new FileOutPutStream(f));
byte[] buff = new byte[1024];
for(int i =0;(i=in.read(buff))>0;)
{
out.write(buff,0,i);
}
out.close();
in.close();
con = JDBC.getConnection();
String sql = "insert into blob_test(big_byte)values(?)";
ps = con.prepareStatement(sql);
File f = new File("c:\\af.jpg");
InputStream in = new BufferedInputStream(new FileInputStream(f)); ps.setBinaryStream(1,in,f.length()); int i = ps.executeUpdate();
in.close();
错了,应该是con = JDBC.getConnection();
String sql = "select big_int from blob_test";
st = con.createStatement();
rs = st.executeQuery(sql);
while(rs.next())
{
InputStream in = rs.getBinaryStream(1);
File f = new File("c:\\af.jpg");
OutputStream out = new BufferedOutputStream(new FileOutputStream(f));
byte[] b = new byte[1024];
for(int i = 0; (i = in.read(b)) > 0 ;)
{
out.write(b,0,i);
}
out.close();
in.close();
}
刚才试了,好像还是不行.我把我的代码贴出来吧
public class Test { public static void main(String[] args) throws Exception { // Class.forName("org.gjt.mm.mysql.Driver"); String jdbc = "jdbc:mysql://localhost:3306/gxjsql";
String user = "root";
String password = "xiao";
Connection conn = null;
String sql1 = "insert into prosen1 (name,age) values (?,?);"; // 增加记录
String sql2 = "select name from prosen1 where id = 1;"; // 查询记录
conn = java.sql.DriverManager.getConnection(jdbc, user, password);
//存储部份
/*
PreparedStatement ps = conn.prepareStatement(sql1); File f = new File(
"C:\\Documents and Settings\\Administrator\\桌面\\暴风截屏.bmp");
InputStream in1 = new BufferedInputStream(new FileInputStream(f)); ps.setBinaryStream(1, in1, f.length()); // 设置longblob类型字段name的值
ps.setInt(2, 13); // 设置int 类型字段age的值 int y = ps.executeUpdate();
in1.close();
*/
// 上面部份是将桌面图片存入到数据库中,下面部份是将数据库中图片读取出来输出到桌面上
//读取部份
Statement st = conn.createStatement(); //创建对象
ResultSet rs = st.executeQuery(sql2); //查询记录
while (rs.next()) {
InputStream in2 = rs.getBinaryStream(1);
File f2 = new File(
"C:\\Documents and Settings\\Administrator\\桌面\\213.BMP");
OutputStream out = new BufferedOutputStream(new FileOutputStream(f2));
byte[] b = new byte[1024];
for (int i = 0; (i = in2.read(b)) > 0;) {
out.write(b, 0, i);
}
out.close();
in2.close();
}
conn.close();
}
}