Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9211017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:12:11+00:00 2026-06-18T01:12:11+00:00

when I am executing this jsp file, I am able to download the file

  • 0

when I am executing this jsp file, I am able to download the file in pdf format.but when i ma trying to open it is displaying an error that unsupported format. But if i try to open the downloaded the pdf file in notepad I am getting the my desired data.But I should get it in a decrypted form.so please help me out to open the pdf file for viewing data..

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <%  String ID = (String) session.getAttribute("ID");
    String connectionURL = "jdbc:oracle:thin:@localhost:1521:XE";
    String url = request.getParameter("WEB_URL");
    String Content = new String("");
    Statement stmt = null;
    Connection con = null;


String Content = new String("");
                String filename = "data" + ID + ".pdf";

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection(connectionURL, "temp",

            "root");
            stmt = con.createStatement();
            String qry = "select * from form where id='" + ID + "'";
            ResultSet rst = stmt.executeQuery(qry);
            if (rst.next()) {
                Content = "  FirstName: " + rst.getString("FirstName")
                        + "\r\n" + "  LastName: "
                        + rst.getString("LastName") + "\r\n" + "  Age: "
                        + rst.getString("Age") + "\r\n" + "  City: "
                        + rst.getString("City") + "\r\n" + "  Phone: "
                        + rst.getString("Phone") + "\r\n" + "  ID: "
                        + rst.getString("ID");
            }
            //out.println(Content);
            byte requestBytes[] = Content.getBytes();
            response.reset();
            response.setContentType("application/pdf");
            response.setHeader("cache-control", "no-cache");
            response.setHeader("Content-disposition",
                    "attachment; filename=" + filename);
            response.setCharacterEncoding("UTF-8");

            ByteArrayInputStream byteStream = new ByteArrayInputStream(
                    requestBytes);
            BufferedInputStream bufStream = new BufferedInputStream(
                    byteStream);
            ServletOutputStream responseOutputStream = response
                    .getOutputStream();
            int data = bufStream.read();
            while (data != -1) {
                responseOutputStream.write(data);
                data = bufStream.read();
            }

            bufStream.close();
            responseOutputStream.close();


        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(" Exception is:" + e);
        }
    %>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:12:12+00:00Added an answer on June 18, 2026 at 1:12 am

    Mainly what is wrong, you are outputting HTML too around the PDF output. Make sure that you start with <% and end with %> without final newline; that is good style, even if response.reset() might function here.

    Furthermore content.getBytes("Cp1252") (Windows Latin-1, extension of your ISO-8859-1). Otherwise the platform encoding is default.

    And it seems you output the string Content not the pdf (filename).

    File file = request.getServletContext().getRealPath("/pdfsdir/" + filename);
    FileInputStream byteStream = new FileInputStream(file);
    

    As PDF is binary the setCharacterEncoding is not needed.
    For a file you might however set the Content-Length header.

    For filling in the personalized data one would need more.


    Early on do:

            File file = request.getServletContext().getRealPath("/pdfsdir/" + filename);
            if (!file.canRead()) {
                // Log this with filename...
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
            long fileLength = file.length();
            if (fileLength > Integer.MAX_VALUE) { // Too large
                // Log this with filename...
                response.sendError(HttpServletResponse.SC_CONFLICT);
                return;
            }
            int length = (int) fileLength;
    

    After:

            response.setHeader("Content-disposition",
                    "attachment; filename=" + filename);
    

    do

            response.setContentLength(length);
    
            FileInputStream byteStream = new FileInputStream(file);
            BufferedInputStream bufStream = new BufferedInputStream(
                    byteStream);
            ServletOutputStream responseOutputStream = response
                    .getOutputStream();
            final int BLOCK_SIZE = Math.min(16*1024, length);
            byte[] data = new byte[BLOCK_SIZE];
            while (length > 0) {
                int nread = bufStream.read(data, 0, Math.max(length, block.length));
                if (nread <= 0) {
                    // Something fishy happened, upload of other smaller version.
                    break;
                }
                length -= nread;
                responseOutputStream.write(data, 0, nread);
            }
            bufStream.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is it possible to restart Tomcat6 by executing a JSP? This because I would
Need your help figuring this thing out. Scenario: On my JSP, I am trying
getting Error while executing this query , because column text may contain text with
The end user will select an excel file on executing the below jsp program.
Hi I have something like this in struts.xml <action name=LoginAction class=controller.LoginAction> <result name=error>/Error.jsp</result> <result
I have been facing some serious issues while executing this particular JSP page. CODE
i am executing this sql query to update the database $sql_select = UPDATE `database1`.`media`
There is a problem executing this block of code while passing the parameter. Parameter
I am executing this statement under while (($data=fgetcsv($this->fin,5000,;))!==FALSE) Now what I want in else
I'm executing this code: void update_process(PROCSP * process){ int state; printf(process.pid = %d\n, process->pid);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.