1

I am following a file upload tutorial in Servlet

My system configuration is

  • Eclipse
  • Apache Tomcat 8.5
  • Jdk:1.8

My index.html(form code):

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="UploadFileServlet" method="post">
    Select a file to upload 
    <input type="file" value="file" name="source"/> 
    <input type="submit" value="Upload"/>
</form>
</body>
</html>

UploadFileServlet.java:

package com.upload;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


    /**
     * Servlet implementation class UploadFileServlet
     */
    @WebServlet("/UploadFileServlet")
    public class UploadFileServlet extends HttpServlet 
    {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        {
            try
            {
            ServletFileUpload sf=new ServletFileUpload(new DiskFileItemFactory());
            List<FileItem> multFileItems=sf.parseRequest(request);
            System.out.println(multFileItems.isEmpty());
            for(FileItem item:multFileItems)
            {
                item.write(new File("E:\\aman\\"+item.getName()));
            }
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }//End of doPost method
    }

I do not understand why It does not work. It does not produce any exception.

Expected output:

I expected this code to upload the file which I had selected in my form in e:\aman\ folder, but it does not upload any file

You can reproduce this error by

https://drive.google.com/open?id=1oHnkV5Tmrp2jX8SU6Ilw_EYPWXQbsvv9

Reporter
  • 3,897
  • 5
  • 33
  • 47
Aman
  • 586
  • 8
  • 20
  • 1
    You are blindly catching all exceptions in your `doPost`. It might help to do something with the exception, such as printing it out to a log. That way you will at least know if an exception occurred. – DanielBarbarian Apr 21 '20 at 08:37
  • I have edited my question. No exception occured.@DanielBarbarian – Aman Apr 21 '20 at 08:39
  • 1
    Then please include in the question what does happen and why it differs from your expected result. – DanielBarbarian Apr 21 '20 at 08:42
  • I expected this code to upload a file in my e:\aman\ folder. But it does not. I have edited my post. @DanielBarbarian – Aman Apr 21 '20 at 08:46
  • 1
    Look here [BallusC](https://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet). It covers a lot of things you should look into. – rhenesys Apr 21 '20 at 09:19
  • @Aman That much I gathered, but you haven't provided any input about if there are any error messages, if the requests reach the servlet at all and if so what it logs. – DanielBarbarian Apr 21 '20 at 09:54
  • @DanielBarbarian thanks for spending time on my post. For a professional developer, I know it was a silly error. But I think this will help others if they did the same mistake. That's why I want to keep this post. I have spent time on answering my own post. If someone search the same thing on google this post might help them saving some time. – Aman Apr 25 '20 at 17:16

1 Answers1

1

I changed the index.html

I added

enctype="multipart/form-data" to my form

<form action="UploadFileServlet" method="post" enctype="multipart/form-data">

As stated in the HTML specification you have to use the POST method and the enctype attribute of the form has to be set to "multipart/form-data".

Aman
  • 586
  • 8
  • 20