出现错误情况是几十K的小文件可以下载,几M的大文件就不能下载了,报错如下:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]IO Error creat
ing temp file: null
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source
)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.submitRequest(Unknown
Source)
at com.microsoft.jdbc.sqlserver.tds.TDSCursorRequest.sendCursorFetch(Unk
nown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSCursorRequest.openCursor(Unknown
Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.execute(Unknown S
ource)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown So
urce)
at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown So
urce)
at mylib.DBFile.getContent(DBFile.java:113)
at org.apache.jsp.downLoad_jsp._jspService(downLoad_jsp.java:78)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)下面是我写的一个方法
/**通过文件id从数据库中把文件内容取出来
* @param detial
* @param f
*/
public static void getContent(int detial, File f) {
RDBMServices rdbm = new RDBMServices();
Connection conn = RDBMServices.getConnection();
System.out.println(detial);
try {
//取出
PreparedStatement ps = conn.prepareStatement("select * from UP_FILE where id = ?");
ps.setInt(1, detial);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
InputStream in = rs.getBinaryStream("filecontent");
//System.out.println(in.available());
FileOutputStream out = new FileOutputStream(f);
byte[] b = new byte[1024];
int len = 0;
while ( (len = in.read(b)) != -1) {
out.write(b, 0, len);
out.flush();
}
out.close();
in.close();
} rs.close();
ps.close();
}catch (Exception ex) {
ex.printStackTrace(System.out);
}
finally {
try { RDBMServices.releaseConnection(conn);}
catch (Exception ex) { }
}
}然后在JSP中如下:
File f = File.createTempFile("pudong",null,new File(System.getProperty("user.dir")));
//把文件内容从数据库中读出到临时文件中
DBFile.getContent(Integer.parseInt(fileid),f);
//读取文件流
FileInputStream in = new FileInputStream(f);
byte[] m_data=new byte[in.available()];
int length = in.read(m_data);
response.setContentLength(length);
in.close();
//删除临时文件
try{
boolean b = f.delete();
}catch(Exception e){
}
//输出
ServletOutputStream servletOut = response.getOutputStream();
servletOut.write(m_data);
servletOut.close();
%>资源下载成功!<%
}catch(Exception e){
%>资源下载时出现异常!<%
e.printStackTrace();
}
%>
请大家看看我错在哪里,谢谢!!
ing temp file: null
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source
)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.submitRequest(Unknown
Source)
at com.microsoft.jdbc.sqlserver.tds.TDSCursorRequest.sendCursorFetch(Unk
nown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSCursorRequest.openCursor(Unknown
Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.execute(Unknown S
ource)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown So
urce)
at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown So
urce)
at mylib.DBFile.getContent(DBFile.java:113)
at org.apache.jsp.downLoad_jsp._jspService(downLoad_jsp.java:78)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)下面是我写的一个方法
/**通过文件id从数据库中把文件内容取出来
* @param detial
* @param f
*/
public static void getContent(int detial, File f) {
RDBMServices rdbm = new RDBMServices();
Connection conn = RDBMServices.getConnection();
System.out.println(detial);
try {
//取出
PreparedStatement ps = conn.prepareStatement("select * from UP_FILE where id = ?");
ps.setInt(1, detial);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
InputStream in = rs.getBinaryStream("filecontent");
//System.out.println(in.available());
FileOutputStream out = new FileOutputStream(f);
byte[] b = new byte[1024];
int len = 0;
while ( (len = in.read(b)) != -1) {
out.write(b, 0, len);
out.flush();
}
out.close();
in.close();
} rs.close();
ps.close();
}catch (Exception ex) {
ex.printStackTrace(System.out);
}
finally {
try { RDBMServices.releaseConnection(conn);}
catch (Exception ex) { }
}
}然后在JSP中如下:
File f = File.createTempFile("pudong",null,new File(System.getProperty("user.dir")));
//把文件内容从数据库中读出到临时文件中
DBFile.getContent(Integer.parseInt(fileid),f);
//读取文件流
FileInputStream in = new FileInputStream(f);
byte[] m_data=new byte[in.available()];
int length = in.read(m_data);
response.setContentLength(length);
in.close();
//删除临时文件
try{
boolean b = f.delete();
}catch(Exception e){
}
//输出
ServletOutputStream servletOut = response.getOutputStream();
servletOut.write(m_data);
servletOut.close();
%>资源下载成功!<%
}catch(Exception e){
%>资源下载时出现异常!<%
e.printStackTrace();
}
%>
请大家看看我错在哪里,谢谢!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货