1
  1. I am trying to upload multiple files via JSP to servlet.
  2. In servlet, trying to read the files one by one to get the file content and do some logic with the contents. when i try to read the file i am seeing below issue.
  3. Please note the issue is happening when i try to upload accessing the local host url in chrome/IE but code is working perfectly when i do the same within eclipse acessing the local host url

here is my code : String text =readstring(file) line causing issue with message FileNotFoundException .

if (ServletFileUpload.isMultipartContent(request)) {
    try {

        List < FileItem > multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
        System.out.println("get the multiparts" + multiparts.get(0));
        for (FileItem item: multiparts) {
            if (!item.isFormField()) {
                name = new File(item.getName()).getName();

                File file = new File(item.getName()); **
                String text = readstring(file); **
                //System.out.println("get the text"+text);
                message = GetAndPost(text);
                finaloutput = finaloutput + "************" + "\n" + name + " --->" + message + "\n" + "************";

                item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
            }


            System.out.println("get the message" + name);


        }


        request.setAttribute("message", "File uploaded successfully.");
    } catch (Exception ex) {
        request.setAttribute("message", "File upload failed due to : " + ex);
    }
    FileUtils.writeStringToFile(new File("C:\\test\\test.txt"), finaloutput);

}
Bashir
  • 2,057
  • 5
  • 19
  • 44
TTT
  • 113
  • 2
  • 3
  • 11
  • add the `readstring(file) ` method code – Anish B. Jun 11 '20 at 14:54
  • private String readstring(File file) throws IOException { String st; StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(new FileReader(file)); st = br.readLine(); while (st != null) { sb.append(st); st =br.readLine(); } System.out.println("get the string"+sb.toString()); return sb.toString(); } – TTT Jun 11 '20 at 14:56
  • 1
    @AnishB. i couldnt add the code in main section thats why added here in comment. sorry. please let me know if u need more details.Thanks – TTT Jun 11 '20 at 14:57
  • ok no problem. I think file is not uploaded. – Anish B. Jun 11 '20 at 14:58
  • sorry meaning? when i upload the file and submit itcausing the issue.. but the same code working fine in eclipse local url – TTT Jun 11 '20 at 15:01

0 Answers0