如何把数据库中的文件流读出来重新转为文件形式放到固定目录下?

netmobile 2003-10-18 12:25:35
比如我像SQL SERVER 中存了图片,如何重新读出放到c:\...呢?
...全文
32 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhanghuazhanghome 2003-10-18
  • 打赏
  • 举报
回复
import java.io.*;
import java.sql.*;

public class GetImage
{
public static void main(String args[])
{

String username;
String password;
String url;
String dropString;
String createString;

username = "root";
password = "root";
url = "jdbc:mysql://localhost/dangan";

try
{
Class.forName("org.gjt.mm.mysql.Driver");
} catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try
{
Connection con;
Statement stmt;
InputStream in=null;
con = DriverManager.getConnection(url, username, password);
System.out.println ("Ok, connection to the DB worked. Let's see if we can insert something:");
Statement stat=con.createStatement();
String sql="select size from image";
ResultSet rs=stat.executeQuery(sql);
while(rs.next())
{
in=rs.getBinaryStream("size");
}
FileOutputStream fileout=new FileOutputStream(new File("c:\\22.jpg"));
int size=in.available();
byte[] by=new byte[size];
in.read(by);
fileout.write(by,0,size);
fileout.close();
in.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
netmobile 2003-10-18
  • 打赏
  • 举报
回复
OK,已经解决了,感谢zhanghuazhanghome(zhanghuazhanghome),分不多了,只给这么多分,见谅了,以后有问题还请多多指教。
netmobile 2003-10-18
  • 打赏
  • 举报
回复
好像文件大小读出来时错的,怎么回事?
milkbread 2003-10-18
  • 打赏
  • 举报
回复
图片是在c盘生成了,但怎么打开时说不能确定类型,我存入数据库的也时.jpg格式,现在在c盘生成的也时.jpg格式呀,不明白?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧