Uploading a file with Java Servlet

File(s) are uploaded via HTML form tag. A servlet can be used with an HTML form to allow users to upload files to the server.

Uploading file

While uploading a file using HTML form using the POST method, it's important to define enctype. It enables the browser to send the form field as a series of "parts" which we access using the Java Part class. Following the HTML code to upload a file.

<form action="fileUpload" method="POST" enctype="multipart/form-data">
     <input type="file" name="someFile" accept="image/png, image/jpg" />
     <input type="submit" value="Upload" />
</form>

The accept attribute specifies what type of file can be uploaded through the form. Following are the values accepted:

1. Audio Files

  • audio/*: For all audio files
  • audio/extension: Specifying specific extension files. Eg. audio/mp3, audio/wav, etc.
  • Example: <input type="file" accept="audio/mp3, audio/wav" />

2. Video Files

  • video/*: For all video files
  • video/extension: Specifying specific extension video file(s)
  • Example: <input type="file" accept="video/mp4, audio/webm" />

3. Image Files

  • image/*: For all image files
  • image/extension: Specifying specific extension image file(s)
  • Example: <input type="file" accept="image/jpg, image/png" />

4. File extension

  • .*: All extension files
  • .extension: Specifying specific extension files. It can be any of the image, text, video, audio, etc. files
  • Example: <input type="file" accept=".mp3, .jpg, .txt" />

It's important to specify the name attribute as it is used to receive the file in the servlet.

Receiving and Saving file

As specified previously, the enctype="multipart/form-data" sends a series of "parts" of the file which is received using the Parts class. We're going to upload an image file but any type of file can be uploaded following this method.

@MultipartConfig
Part fileParts = request.getPart("someFile"); //getPart() method accepts the value of name of the input tag.
String filename = fileParts.getSubmittedFileName(); //getSubmittedFileName() method returns the name of the file along with its extension

/**
* The code below creates a path to the UploadedImgs folder which is inside Images as shown below.
* Note that getRealPath() method gives the build path. Eg.: 
   D:\Projects\ImgUpload\build\web\Images\UploadedImgs\
* Folder structure:
◼ Images
  ▪ UploadedImgs
*/
String path = request.getRealPath("Images") + File.separator + "UploadedImgs" + File.separator;

//Writing the file into the above-mentioned path.
FileOutputStream fos = new FileOutputStream(path + filename);
InputStream ips = filePart.getInputStream();

byte[] data = new byte[ips.available()];
ips.read(data);
fos.write(data);
ips.close();
fos.close();

The code above writes the uploaded file inside the UploadedImgs Folder. The code above requires you to import following classes: javax.servlet.http.Part, java.io.FileOutputStream, java.io.InputStream and javax.servlet.annotation.MultipartConfig and since it throws IOException and ServletException , the above block of code should be inside try...catch() block.

try{

//The Code Above...

}catch(IOException | ServletException ex){
System.out.print(ex.printStackTrace);
}