我有一个jsp页面,其中有一个下载链接。在单击链接时,它调用servlet downloadservlet,它从数据库中提取文件以进行下载。 但问题是我发送给servlet的文件的标题包含空格。例如=>大学服务法。所以该字符串将作为College%20Service%20Act进入servlet,并且无法与数据库匹配。怎么办?
我现在使用jsp文件下载pdf。这里pdf已下载但无法打开。任何人都可以建议对此代码进行任何更改。
download.jsp
<%@page import="java.sql.*,java.io.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String connectionURL = "jdbc:oracle:thin:@localhost:1521:xe";
String title = request.getParameter("title");
Statement stmt = null;
Connection con = null;
Blob b=null;
try
{
String filename = title+".pdf";
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(connectionURL, "shivashukla", "system");
stmt = con.createStatement();
String qry = "select * from law where title ='" + title+"'";
ResultSet rst = stmt.executeQuery(qry);
if (rst.next())
{
b = rst.getBlob(6);
}
InputStream in = b.getBinaryStream();
byte b1[] = new byte[(int) b.length()];
in.read(b1);
response.reset();
//response.setContentType("*.pdf");
response.setHeader("cache-control", "no-cache");
//response.setHeader("Content-disposition", "attachment; filename=" + filename);
response.setContentType("application/pdf");
response.setHeader("Content-disposition","attachment; filename="+filename);
}
catch (Exception e)
{
e.printStackTrace();
}
%>
jsp page
<body style="background-color: antiquewhite;">
<%
try {
String cat = (String)session.getAttribute("cat");
String state = (String)session.getAttribute("state");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "shivashukla",
"system");
PreparedStatement ps = con.prepareStatement("select * from law where category=? and state=?");
ps.setString(1, cat);
ps.setString(2, state);
ResultSet rs = ps.executeQuery();
%>
<table class="table table-bordered table-responsive table-hover" >
<tr>
<th>SECTION</th>
<th>CATEGORY</th>
<th>TITLE</th>
<th>STATE</th>
<th>YEAR</th>
<th>PDF</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><a href="downloadservlet?title=<%=rs.getString(3)%>" target="_blank">Download</a>
</td>
</tr>
<%
}
} catch (Exception e) {
System.out.println(e);
}
%>
</table>
</body>
答案 0 :(得分:0)
您可以使用java.net.URLDecoder.decode()
。
尝试更改:
String fn = req.getParameter("title");
为:
String fn = java.net.URLDecoder.decode(req.getParameter("title"));