我尝试制作从我的数据库(PostgreSQL)获取文件并将其发送到客户端的 servlet。我说:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try
{
Connection ce = ConnectionManager.createConnection();
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Statement sta = null;
sta = ce.createStatement();
String fileName="";
byte[] file=null;
int bufferSize = 8192;
String sql=("SELECT files,filename FROM filestock WHERE num =(SELECT filestock_id FROM parcels_temp WHERE num="+num+")");
ResultSet rs=sta.executeQuery(sql);
while(rs.next()){
file = rs.getBytes("files");
fileName=rs.getString("filename");
}
}
所以我只是创建连接,eclipse 说我要在Try/catch
. 怎么了?在servlet中使用JDBC是个坏主意,我必须在另一个类中处理数据库?
在这种情况下如何将文件发送到 servlet?我会把它作为File
或其他我想用它发送文件从 servlet 发送到客户端的东西。