Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
AddPhotoServlet.java

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class AddPhotoServlet extends HttpServlet {

    private final String path = "C:/Users/admin/Documents/NetBeansProjects/PDFUploading/PdfFiles";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    String pdf= "";

    if(ServletFileUpload.isMultipartContent(request)){

    try {

          List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
         for(FileItem item : multiparts){
             if(!item.isFormField())
                    {
                        pdf = new File(item.getName()).getName();
                        item.write( new File(path + File.separator + pdf));
                    }
               }


          Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","root");
            con.setAutoCommit(false);

            PreparedStatement ps = con.prepareStatement("insert into pdf values(null,?)");

             ps.setString(1, pdf);
            ps.executeUpdate();

            String sql = "select resume from pdf where resume='"+pdf+"' ";
            ResultSet rs = ps.executeQuery(sql);
            while(rs.next())
            {
                byte[] b1 = rs.getBytes("resume");

                String s1 = new String(b1);
                out.println(s1);
            }


//
//            while(rs.next())
//            {
//                byte[] file= rs.getBytes("resume");
//                String str1 = new String(file);
//                out.println(str1);
//            }
            con.commit();
            con.close();
            out.println("Proto Added Successfully. <p> <a href='listphotos'>List Photos </a>");

        }catch(Exception ee){
          out.println(ee.getMessage());
          out.println(ee.getCause());
            }
       }
    }
}




index.jsp

&lt;html>
    &lt;head>
        &lt;title>Add Photo&lt;/title>
    &lt;/head>
    &lt;body>
        <h2>Add Photo</h2>
        &lt;form id="form1" enctype="multipart/form-data" action="addphotos" method="post">
            <table>
<!--                <tr>
                    <td>Enter Photo Id :</td>
                    <td>&lt;input  type="text"  name="id"/></td>
                </tr>
                <tr>
                    <td>Enter Title For Photo :</td>
                    <td>&lt;input  type="text"  name="title"/></td>
                </tr>
                <tr>-->
                    <td>Select Photo  </td>
                    <td>&lt;input type="file"  name="photo" />
                </tr>
            </table>
            <p/>
            &lt;input type="submit" value="Add Photo"/>
        &lt;/form>

        <p/>
        <a href="listphotos">List Photos </a>
    &lt;/body>
&lt;/html>
Posted
Comments
Richard MacCutchan 24-Apr-15 3:39am    
What is your question?
Abhishek Rana 24-Apr-15 5:50am    
I want to show that PDF file in my browser

1 solution

So you get the PDF out of your DB?
Then just throw it at your system and let the system decide how to deal with PDF files:

Java
try{ Desktop.getDesktop().open(myFile); }
catch(IOException ex){ /* Logging */ }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900