1

I am developing a web application using jsp and servlets. I am trying to upload a file and then process the data of that file. for this purpose my jsp code is

<form action="LoadFile" method="post" enctype="multipart/form-data" accept-charset="UTF-8"> <input type="file" name="file" id="file" size="50" accept=".doc, .docx, .txt"/> <br /> <input type="submit" value="Check Now" name="upload" id="upload"/> </form> 

Java servlet named "LoadFile.java" contains the following code in processRequest method

request.setCharacterEncoding("UTF-8"); Part filePart = request.getPart("file"); String fileName = getFileName(filePart); OutputStream outStream = null; InputStream filecontent = null; final PrintWriter writer = response.getWriter(); try { outStream = new FileOutputStream(new File(File.separator + fileName)); filecontent = filePart.getInputStream(); int read = 0; final byte[] bytes = new byte[1024]; while ((read = filecontent.read(bytes)) != -1) { outStream.write(bytes, 0, read); } writer.println("New file " + fileName + " created at " + filePath); } catch (FileNotFoundException fne) { writer.println("You either did not specify a file to upload or are " + "trying to upload a file to a protected or nonexistent " + "location."); writer.println("<br/> ERROR: " + fne.getMessage()); } 

Whenever I tries to upload a file, it gives the FileNotFoundExceptin. What do I need to do?

3
  • Who is throwing that exception? FileOutputStream constructor? Commented Jun 29, 2015 at 12:39
  • Please give us the exact stacktrace of the FNFE Commented Jun 29, 2015 at 12:43
  • Access is denied message is shown Commented Jun 29, 2015 at 12:50

1 Answer 1

1

In your Web Application WEB-INF folder create a folder called files and change the code of FileOutputStream as below.

outStream = new FileOutputStream(new File(request.getRealPath("/WEB-INF/")+ "files"+ File.separator + fileName)); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.