:http://localhost:8080/tryupload/downloadservlet?bookid=15bkbookid=15bk(15bk是我通过servlet上的getString从数据库上的bookid获得的)
然后,下面是我在servlet(而不是jsp)上的显示
out.println("<TD><a href=downloadservlet?bookId=" + rs.getString(1)+ ">Download</A></TD>");
然后它转到downloadservlet.java//,但它不起作用。空白页。
如何阅读此链接中的"boookid"http://localhost:8080/tryupload/downloadservlet?bookid=15bk到我的servlet?并在我的downloadservlet.java上执行?
下载servlet.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String bookId = request.getParameter("bookId");
Connection conn = null; // connection to the database
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
// queries the database
String sql = "SELECT * FROM books WHERE bookId = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, bookId);
ResultSet result = statement.executeQuery();
if (result.next()) {
// gets file name and file blob data
String fileName = result.getString("BookContent");
Blob blob = result.getBlob("BookContent");
InputStream inputStream = blob.getBinaryStream();
int fileLength = inputStream.available();
System.out.println("fileLength = " + fileLength);
ServletContext context = getServletContext();
// sets MIME type for the file download
String mimeType = context.getMimeType(fileName);
if (mimeType == null) {
mimeType = "application/octet-stream";
}
// set content properties and header attributes for the response
response.setContentType(mimeType);
response.setContentLength(fileLength);
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename="%s"", fileName);
response.setHeader(headerKey, headerValue);
// writes the file to the client
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outStream.close();
} else {
// no file found
response.getWriter().print("File not found for the id: " + bookId);
}
} catch (SQLException ex) {
ex.printStackTrace();
response.getWriter().print("SQL Error: " + ex.getMessage());
} catch (IOException ex) {
ex.printStackTrace();
response.getWriter().print("IO Error: " + ex.getMessage());
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
使用正确读取参数
request.getParameter("bookid")
然而,当您读取15bk时,您不能直接应用Integer.parseInt
,因为它不是整数,如果您需要15bk中的15,则需要额外的解析步骤。
在你上次编辑之后,也是同样的问题-你不能进行
statement.setInt(1, bookid);
bookid不会是int。你需要解析,不确定,但例如,如果你想消除最后2个字符,你可以进行
bookIntId = bookId.substrings(0, bookId.length - 2)
String bookId = request.getParameter("bookid");
如果你的参数值是15bk
,那么你的代码行:
int bookid = Integer.parseInt(request.getParameter("bookid"));
将抛出NumberFormatException
,因为bk
无法转换为整数值。