0

I am trying to creating to File upload and store into local directory by using java servlet .I added the cos.jr file .I added a new folder into roor direcorty name is new . but when i click the button file upload its giving me following errors .

java.lang.IllegalArgumentException: Not a directory: d:/new

Here is the html .

<html>  
<body>  
<form action="FileUploadServlet" method="post" enctype="multipart/form-data">  
Select File:<input type="file" name="fname"/><br/>  
<input type="submit" value="upload"/>  
</form>  
</body>  
</html> 

Here is the servlet code .

package Servlet.org;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest; 


public class FileUploadServlet extends HttpServlet {

        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");

        }


        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
            response.setContentType("text/html");  
             PrintWriter out = response.getWriter();  

           MultipartRequest m=new MultipartRequest(request,"d:/JAVAServlet/new");  
           out.print("successfully uploaded");
        }


        @Override
        public String getServletInfo() {
            return "Short description";
        }// </editor-fold>

    }

Here is the result when i clicked the upload button .

enter image description here

Mohammad
  • 1,019
  • 8
  • 24
  • isn't the message clear? *Not a directory: d:/new* – Jens Jan 11 '20 at 16:32
  • Also if you get an 500 (Internal Server Error) it is helpful to look into the server logfiles for more informations – Jens Jan 11 '20 at 16:33
  • Possibly related: [How to upload files to server using JSP/Servlet?](https://stackoverflow.com/q/2422468) – Pshemo Jan 11 '20 at 16:41

1 Answers1

1

I think it may be path or some permission issue.. not sure why it is showing as d:/new instead of d:/JAVAServlet/new. try below code and see if the directory is created.

 public static void main(String[] args) {
      String saveDirectory = "d:" + File.separator + "JAVAServlet" + File.separator+ "new";
      File dir = new File(saveDirectory);

      if(!dir.isDirectory()){
         throw new IllegalArgumentException("Not a directory: " + saveDirectory);
      }
   }

it may be issue with directory creation permission or path. or try using File.separator instead of / "d:" + File.separator + "JAVAServlet" + File.separator+ "new"

Narendra Reddy
  • 358
  • 2
  • 6