0

I have a jsp page in which there is a link for download. On clicking the link it calls a servlet downloadservlet which fetches the file from database to download. But the problem is that the title of the file which I am sending to servlet contains spaces. eg=> College Service Act. so the string is going to servlet as College%20Service%20Act and not able to match with the database. What to do?

I am now using a jsp file to download the pdf. Here the pdf is downloaded but not able to open. Can anyone suggest any changes in this code.

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>

1 Answers1

0

You can use java.net.URLDecoder.decode().

Try changing from:

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

to:

String fn = java.net.URLDecoder.decode(req.getParameter("title"));
Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
  • thanks it done but one problem is there. I need to download the file in pdf format. It is being downloaded in that but adobe reader is not able to load it. error occurs invalid format. How to correct that? – Shiva Shukla Aug 01 '17 at 13:28
  • Try adding `out.flush();` after `out.write(b1);`. – Kohei TAMURA Aug 01 '17 at 13:41
  • I am now using a jsp file to download a my pdf on the database. But here also same error . file downloaded but not opening in adobe pdf viewer. The measure you suggested didn't work. So give some suggestions on "download.jsp" file. I have updated my code – Shiva Shukla Aug 01 '17 at 15:23
  • I think you should revert to original your question and close this as fixed, and then ask new question. If there are two versions of source code, it gets confusing. – Kohei TAMURA Aug 02 '17 at 02:44
  • i have updated the code. There is only new code now. You can check that. – Shiva Shukla Aug 02 '17 at 07:36
  • its download.jsp file. – Shiva Shukla Aug 02 '17 at 07:36