0

I am having a servlet program below for uploading a file and saving it to web location path. I am running below program in NetBeans with Glassfish Server. But when I am taking client input it is giving me exception as

java.io.FileNotFoundException: C:\Users\422405\personal_domain\generated\jsp\HelloWorld\tmp\C:\Users\422405\Desktop\Mohit_prac\HelloWorld\build\web\uploadFiles\sas.txt (The filename, directory name, or volume label syntax is incorrect)

Here is the code:

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
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;

@WebServlet("/upl")
@MultipartConfig(location="/tmp",fileSizeThreshold=1024*1024*2, // 2MB
      maxFileSize=1024*1024*10,      // 10MB
                maxRequestSize=1024*1024*50
               )   // 50MB
public class upl extends HttpServlet {

    /**
     * Name of the directory where uploaded files will be saved, relative to
     * the web application directory.
     */
    private static final String SAVE_DIR = "uploadFiles";

    /**
     * handles file upload
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

// gets absolute path of the web application
        String appPath = request.getServletContext().getRealPath("");
        // constructs path of the directory to save uploaded file
        String savePath = appPath+ File.separator + SAVE_DIR;



        // creates the save directory if it does not exists
        File fileSaveDir = new File(savePath);
        if (!fileSaveDir.exists()) {
            fileSaveDir.mkdir();
             out.println("hisucess");
        }


          Part part=request.getPart("file");

          out.println("hi"+part);
          out.println("initial_star"+appPath);
           out.println("koooooo"+savePath);
          String fileName=extractFileName(part);
           out.println("nameeeeeeeeeeeeeeee"+fileName);

          part.write(savePath + File.separator + "sas.txt");

       // for (Part part : request.getParts()) {
        //    String fileName = extractFileName(part);
         ///   part.write(savePath + File.separator + fileName);
      //  }

        //request.setAttribute("message", "Upload has been done successfully!");
       // getServletContext().getRequestDispatcher("/message.jsp").forward(
      //          request, response);
    }

    /**
     * Extracts file name from HTTP header content-disposition
     */


       private String extractFileName(Part part) {

        String contentDisp = part.getHeader("content-disposition");

        String[] items = contentDisp.split(";");
        for (String s : items) {
            if (s.trim().startsWith("filename")) {
                return s.substring(s.indexOf("=") + 2, s.length()-1);
            }
        }
        return "";
    }


}
halfer
  • 19,824
  • 17
  • 99
  • 186
Mohit Darmwal
  • 275
  • 1
  • 5
  • 21
  • 2
    _C:\Users\422405\personal_domain\generated\jsp\HelloWorld\tmp\C:\Users\422405\Desktop\Mohit_prac\HelloWorld\build\web\uploadFiles\sas.txt_ This is definitely wrong – Java_User Aug 16 '15 at 13:16
  • 1
    Your code is not able to find file to upload ;-) Check the path of your file. – Amit Bhati Aug 16 '15 at 13:18
  • To above commenters: please carefully read the answer to the duplicate question yourself as well in case you didn't spot the actual mistake (those smartass comments indicate you both really didn't). – BalusC Aug 16 '15 at 21:42
  • 1
    @BalusC : Didn't expected a comment like this from a reputed contributor. We might missed out that this question is a duplicate, just that I personally didn't wanted to spoon feed the OP. Peace! – Java_User Aug 17 '15 at 12:41

0 Answers0