0

I am trying to upload a .xls file which is in my Downloads drive. The file link is C:\Users\pch\Downloads. When I move it to my C:\ and upload it, it runs fine but when it's in my download drive it doesn't. I am getting a "File not found" error.

Here is what I tried.

               //Create a new file upload handler.
                ServletFileUpload upload = new ServletFileUpload();
               //Parse the request
                FileItemIterator iter = upload.getItemIterator(request);
                while (iter.hasNext()) {
                    FileItemStream item = iter.next();                           
                    String name = item.getFieldName();
                    InputStream stream = item.openStream();
                    System.out.println("File field " + name + " with file name " + item.getName() + " detected.");

                       String filename = item.getName();
                       
                       // Process the input stream
                       File f = new File("/"+item.getName());
                       System.out.println(f.getAbsolutePath()); 

Process Excel file:

                       if (filename.endsWith(".xls") || filename.endsWith(".XLS")){    
                                FileInputStream inputStream = new FileInputStream(f.getAbsolutePath());     //code breaks right here                                
                                
                                //FileInputStream inputStream = new FileInputStream(new File(filename));                                
                                HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
                                HSSFSheet firstSheet = workbook.getSheetAt(0);                                                                                                 
                                                                                
                                Iterator<Row> rows = firstSheet.rowIterator();
                                while (rows.hasNext()) {
                                    HSSFRow row = (HSSFRow) rows.next();
                                    Iterator<Cell> cells = row.cellIterator();
                                    List cellData = new ArrayList();
                                                                                    
                                    //Skip the first row beacause it will be header                                  
                                    if (row.getRowNum() == 0) {                                                                                                                  
                                        maxDataCount = row.getLastCellNum();
                                        continue;                                   
                                    }                      
                                        while (cells.hasNext()) {                                       
                                            HSSFCell cell = (HSSFCell) cells.next();                            
                                             //Cell cell = (Cell) cells.next();                                                     
                                                if(cell.getCellType() == Cell.CELL_TYPE_STRING) {                                              
                                                    cellData.add(cell.getStringCellValue());
                                                } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){                                                                               
                                                    cellData.add((int)cell.getNumericCellValue());
                                                }
                                            }                          
                                        excelData.add(cellData);
                                    }           
                                   inputStream.close();       
                         else {                                                      
                            out.println("<span style=\"color:red;\">File needs to have an extension of '.xlsx or .xls'.  Hit <a href=\"javascript:window.history.go(-1)\">back</a> and try a different file.</span>");
                        }
                   }

enter image description here

Any help is greatly appreciated. Thank you in advance.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ace23
  • 142
  • 1
  • 16
  • Are you getting any exceptions? Does the program have permissions to read the directory? – aksappy Mar 18 '21 at 16:37
  • @aksappy yes it has the permission. I attached the exception to my post. Also it's not getting the full file path! – ace23 Mar 18 '21 at 16:47
  • You are appending just a forward slash with the `item.getName()`. My guess it is taking the default drive, which is C: in your case. Can you try hard coding the correct directory and see if that works? – aksappy Mar 18 '21 at 16:52
  • @aksappy It looks like it's getting the file path wrong. Instead of Downloads it's setting it to Desktop. It works when i hardcode it. – ace23 Mar 18 '21 at 17:20
  • Okay, either pass the base directory as a property to the application or get the user home directory (if you are using JDK 8) .. And then construct the absolute path to the file – aksappy Mar 18 '21 at 17:43
  • @BalusC Hey! Thanx so much. That actually worked for me. – ace23 Mar 19 '21 at 17:12

0 Answers0