2017-08-01 60 views
0

我有一個jsp頁面,其中有一個鏈接供下載。點擊鏈接時,它會調用一個servlet downloadservlet,從數據庫中提取文件進行下載。 但問題是我發送給servlet的文件的標題包含空格。例如=>學院服務法。因此字符串將以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頁面

<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

不確定它是否是一個真正的重複,但這個答案應該幫助https://stackoverflow.com/a/15926416/3545273 –

回答

0

您可以使用java.net.URLDecoder.decode()

嘗試從改變:

String fn = req.getParameter("title"); 

到:

String fn = java.net.URLDecoder.decode(req.getParameter("title")); 
+0

感謝它完成,但有一個問題在那裏。我需要以pdf格式下載文件。它正在下載,但Adobe Reader無法加載它。錯誤發生無效格式。如何糾正? –

+0

嘗試在'out.write(b1);'之後添加'out.flush();'。 –

+0

不,它沒有工作。 –

相關問題