uploading of pdf file

Solution 1:

This is clearly a Roseindia code snippet. First of all, it is the worst learning resource ever. Don't use it. It only teaches bad practices. Add that site to your blacklist. In fact, any "tutorial" site which is littered with advertisement banners and hopelessly outdated low quality code snippets are clearly maintained by amateurs with primary focus on advertisement income instead of on serious teaching. Other examples of such crap "tutorial" sites are javabeat, tutorialspoint, journaldev, javatpoint, etc. Remarkable common thing which those sites have is that they are originated in India.

Apart from the fact that you incorrectly used .html file extension instead of .jsp (even though they presented their examples correctly with .jsp extensions), there are several major problems with the code snippet:

  • The HTML is using '90s style uppercased tags. This is discouraged.
  • The HTML is using <font> and <center> tags which are deprecated since 1998.
  • The business logic is mingled with the presentation logic in a single JSP file. The Java code belongs in a Java class, not in a JSP file.
  • The parser is relying on Content-Length request header which is not always present per se. If this header is absent, the code breaks.
  • The parser is creating a byte array of that length. This may crash the server when the content length is larger than available server memory.
  • The parser is creating a String based on the byte array using server platform default character encoding instead of the one specified in multi part header. This may malform/corrupt the result bytes.
  • The DataInputStream wrapper is unnecessary, the code is not taking any benefit of it.
  • Etc..
  • Etc..

It's simply terrible.


The right way to upload a file from JSP is to submit the form to a @MultipartConfig annotated servlet class and then use request.getPart() to get the file. You can find a snippet in this answer: How to upload files to server using JSP/Servlet?

The right way to learn Java EE is elaborated in this answer: Java EE web development, where do I start and what skills do I need?