Cross Vander
Cross Vander

Reputation: 2157

Upload PDF to database and storage system (folder) in JSP

Just like title, I want to ask, how to adding pdf files with upload form to my storage folder (e.g: uploadData) then its added to database as a file too in JSP.

If it not possible, it's ok to added to database as a text.

If it possible as a file, what type of my table for that pdf? blob? or text?

I accept blog links/other links that relevant for my problem

sorry for bad english.

Upvotes: 1

Views: 4284

Answers (2)

Ramesh PVK
Ramesh PVK

Reputation: 15446

Servlet 3.0 container's has standard support for multipart data. It also has the support to write to local file system. First you should be writing a HTML page which takes the file input along with other input parameters.

<form action="uploadservlet" method="post" enctype="multipart/form-data">
    <input type="text" name="name" />
    <input type="text" name="age" />
    <input type="file" name="photo" />
    <input type="submit" />
</form>

Now write a UploadServlet which uses the Servlet 3.0 Upload API. Here is the code which demonstrates the usage of API. Fist the servlet handling multipart data should define MultiPartConfig using any of the two approaches:

  • @MultiPartConfig annotation on Servlet Class
  • In web.xml, by adding entry inside definition.

Here is the UploadServlet,

@MultipartConfig
public class UploadServlet extends HttpServlet
{
  protected void service(HttpServletRequest request, 
   HttpServletResponse responst) throws ServletException, IOException
  {
  Collection<Part> parts = request.getParts();
  if (parts.size() != 3) {
     //can write error page saying all details are not entered
   }

   Part filePart = httpServletRequest.getPart("photo");
   InputStream imageInputStream = filePart.getInputStream();
   //read imageInputStream
   filePart.write("somefiepath");
   //can also write the photo to local storage

   //Read Name, String Type 
   Part namePart = request.getPart("name");
   if(namePart.getSize() > 20){
       //write name cannot exceed 20 chars
   }
   //use nameInputStream if required        
   InputStream nameInputStream = namePart.getInputStream();
   //name , String type can also obtained using Request parameter 
   String nameParameter = request.getParameter("name");

   //Similialrly can read age properties
   Part agePart = request.getPart("age");
   int ageParameter = Integer.parseInt(request.getParameter("age"));



}

}

If you are not using Sevlet 3.0 Container, you should be truing Apache Commons File Upload. Here are the links for using Apache Commons File Upload:

References:

Upvotes: 2

fvu
fvu

Reputation: 32953

The easiest way I know of to handle file uploads is by using Commons FileUpload. The documentation gives you a step by step overview of how to accept uploaded files, including how to easily copy them into a file.

Should you decide to put the PDF in a database (I wouldn't do that), BLOB is your best choice, a PDF file isn't text.

I would however advise not to cram all that logic in a JSP but in a servlet instead.

Upvotes: 0

Related Questions