从数据库里下载文件

host263 2003-08-18 07:17:46
大家好,请教高手:我想在数据库(db2)里下载文件,通过Servlet到JSP页面。上传已经基本实现了。db2里有ID(主键)字段和filename,content我把他们首先通过主键把要下载的文件查找出来,然后通过getfile(自己的函数实现提取文件内容的)下载。我现在能查找出来,可是不知道是提取流的时候失败了还是别的地方不对,总之没有成功,请高手指点一下。
...全文
62 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cbhyk 2003-08-18
  • 打赏
  • 举报
回复
public void doGet(...)
{
response.setContentType("application/octet-stream");
...
}
host263 2003-08-18
  • 打赏
  • 举报
回复
2.bos.flush();
out.flush();
我已经做到了。
1、虽然差点可以也应该有效果吧!
cbhyk 2003-08-18
  • 打赏
  • 举报
回复
1.sql = "SELECT content FROM storefile WHERE id = ?";

2.
bos.flush();
out.flush();

host263 2003-08-18
  • 打赏
  • 举报
回复
对不起,我没有说明白,代码如下,就是实现下载的代码


import java.io.*;
import java.sql.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DownloadServlet extends HttpServlet{

protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
ServletOutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
int id = Integer.parseInt(request.getParameter("id"));

bos.write(getBlob(id));
out.flush();
out.close();
}
public byte[] getBlob(int id){
String sql = "SELECT * FROM storefile WHERE id = ?";
Blob blob = null;
byte[] bytes = null;
try {
new COM.ibm.db2.jdbc.net.DB2Driver();
String m_url=("jdbc:db2://localhost:6789/webhr",userid,password);
String m_user="";
String m_pass="";
Connection con=DriverManager.getConnection(m_url,m_user,m_pass);

PreparedStatement pstmt = con.prepareStatement(sql);
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;
}
bjwulin 2003-08-18
  • 打赏
  • 举报
回复
对,提供能说明问题的部分原码,以及问题的错误信息,呵呵,大家互相帮忙。
javahui 2003-08-18
  • 打赏
  • 举报
回复
你什么细节信息也没有,高手们怎么指点你。
jhaiyan0879 2003-08-18
  • 打赏
  • 举报
回复
up

81,094

社区成员

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

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