如何把word文档整个保存到mysql数据库中 并可以下载到本地

napsterchou 2010-04-09 12:37:16
公司服务器托管 ,所以把word文档保存到服务器 空间需求太大,想把word文档保存到 mysql的一个字段中, 实现上传和下载 。非常急 。。。在线等
...全文
1468 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
prenzhi 2010-04-09
  • 打赏
  • 举报
回复
保存到数据库中不也是放在服务器上吗,不也是同样要占用空间吗?
burgessbo 2010-04-09
  • 打赏
  • 举报
回复
学习中。。。。
hailang7210 2010-04-09
  • 打赏
  • 举报
回复
网上的ADODB数据流可以实现文件无提示上传下载。具体方法你自己找了
NickCheng 2010-04-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 whut_lcy 的回复:]

mysql表里面搞个longblob字段保存word
代码:
1)上传
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url ="jdbc:mysql://localhost/test?user=root&password=root&useUnic……
[/Quote]
就是写流读流吧~我记得做过类似的~!
justwalking 2010-04-09
  • 打赏
  • 举报
回复
学习.......
star19860411 2010-04-09
  • 打赏
  • 举报
回复
楼上说的很完整,也对,楼主看看吧...
whut_lcy 2010-04-09
  • 打赏
  • 举报
回复
mysql表里面搞个longblob字段保存word
代码:
1)上传
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url ="jdbc:mysql://localhost/test?user=root&password=root&useUnicode=true&characterEncoding=gbk";

Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
stmt.execute("insert into test(myid) values (5)");
stmt.close();
PreparedStatement pstmt = null;
String sql = "";
File file = new File("c:\\kick.jpg");
InputStream photoStream = new FileInputStream(file);
sql = " UPDATE test SET photo = ? WHERE myid = 5" ;

pstmt = conn.prepareStatement(sql);
pstmt.setBinaryStream(1, photoStream, (int)file.length());
pstmt.executeUpdate();
pstmt.close();

conn.close();
} catch (Exception e) {
e.printStackTrace();
}

2)下载:
PreparedStatement pst = ..... //省略获取Connection及查询的sql
ResultSet rs = pst.executeQuery();
InputStream is = rs.getBinaryStream(1); //1表示你的word字段在结果集中的索引号

FileOutputStream fos = new FileOutputStream("path");
byte [] buf = new byte[1024];

while(is.read(buf)!=-1){
fos.write(buf);
}

//close省略


大概思路,自己完善
2010-04-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 prenzhi 的回复:]
保存到数据库中不也是放在服务器上吗,不也是同样要占用空间吗?
[/Quote]
同问

81,094

社区成员

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

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