这是我的view.jsp ..
代码<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.io.File,java.util.*,java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<html><body>
<h1> List of All Files to download </h1>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "admin");
PreparedStatement ps = conn.prepareStatement("select * from employeereg1");
ResultSet rs=ps.executeQuery();
%>
<table border=1>
<tr><td>Employee Name </td><td>Employee Address </td><td>Employee Resume </td><td>Employee Photo </td></tr>
<%
while(rs.next()){
%>
<tr>
<td><%=rs.getString(2)%> </td>
<td><%=rs.getString(3)%> </td>
<td><a href="Download.jsp?resumeId=<%=rs.getString(1)%>"> Download Here</a> </td>
<td><a href="Download.jsp?photoId=<%=rs.getString(1)%>"> Download Here</a> </td>
<%
}
%>
</table>
</body>
这是download.jsp
<body>
<%
String fileName="";
String querytext="";
ServletOutputStream op1= null;
byte barray[] =null;
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "admin");
if(request.getParameter("resumeId")!=null)
{
querytext="select emp_resume from employeereg1 where emp_id="+request.getParameter("resumeId");
}
else{
querytext="select emp_pic from employeereg1 where emp_id="+request.getParameter("photoId");
}
PreparedStatement ps = conn.prepareStatement(querytext);
ResultSet rs=ps.executeQuery();
while(rs.next()) {
fileName=rs.getString(1);
}
String mimetype=application.getMimeType(fileName);
response.setContentType((mimetype != null) ? mimetype : "application/octet-stream");
response.addHeader("Content-Disposition","attachment; filename="+fileName);
Blob blb = rs.getBlob(1);
byte[] bdata = blb.getBytes(1, (int) blb.length());
OutputStream output = response.getOutputStream();
output.write(bdata);
output.flush();
output.close();
%>
</body>
将显示此错误 严重:带有路径[/ DBFileDownload1]的上下文中servlet [jsp]的Servlet.service()引发异常[在第63行处理JSP页面/Download.jsp时发生异常
60:response.setContentType((mimetype!= null)?mimetype:“application / octet-stream”); 61:response.addHeader(“Content-Disposition”,“attachment; filename =”+ fileName); 62://从Blob创建字节数组 63:Blob blb = rs.getBlob(1); 64:byte [] bdata = blb.getBytes(1,(int)blb.length()); 65: 66://获取响应输出流对象,将文件内容写入标题
Stacktrace:]有根本原因 java.sql.SQLException:结果集结束后` 在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910) 在com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:726) 在com.mysql.jdbc.ResultSet.getBlob(ResultSet.java:1416) 在org.apache.jsp.Download_jsp._jspService(Download_jsp.java:157)