0

This following scippet works well, and I have been using it across many projects. However, for this one project, I get a file not found exception.

try {
        FileInputStream is = new FileInputStream(file);
        String original = file.getName();

        Logger.debug("Filename in upload pf %s ", original);
        IOUtils.copy(is, new FileOutputStream(Play.getFile(original)));

        PfParser p1 = new PfParser();
        p1.read(original, month, year);

        Payroll.index();

    } catch (FileNotFoundException e) {
        Logger.error(e, "Exception in uploadSheet: ");
        e.printStackTrace();
    } catch (IOException e) {
        Logger.error(e, "Exception in uploadSheet: ");
        e.printStackTrace();
    }

This is the read method, where I have tried a few combinations, which are commented out:

FileInputStream myInput = new FileInputStream(
                System.getProperty("user.dir") + inputFile);
        w = Workbook.getWorkbook(myInput);
        // w = Workbook.getWorkbook(new File(inputFile));
        // w = Workbook.getWorkbook(new File(System.getProperty("user.dir"),
        // inputFile));

This uploads the file to the C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps\ROOT\WEB-INF\application folder.

I am trying to read an excel file using Jexcel. The error I get on my server:

java.io.FileNotFoundException: foo.xls (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)

Trying the other (commented out) lines, only gives a variation in the error.

java.io.FileNotFoundException: C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\foo.xls (The system cannot find the file specified)

I understand its a problem related with absolute and relative paths, but cant seem to find a solution. I do not get any errors, while coding and testing on my local machine which is Ubuntu. Its only when I deploy to a Windows server, do I get these problems.

Thanks.

theTuxRacer
  • 12,329
  • 7
  • 39
  • 58

2 Answers2

0

The FileNotFound exception come when the file is not located on the absolute/relative paths that you provide in your FileInputStream constructor.

FileInputStream is = new FileInputStream(file);

I doubt you are not giving the right location in your FileInputStream constructor. It would be good if you write the complete spinet including file location etc.

KAS
  • 5,132
  • 10
  • 35
  • 65
  • The file is successfully uploaded to `C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps\ROOT\WEB-INF\application` – theTuxRacer Sep 24 '12 at 06:55
  • Just the name of the file, without any path. – theTuxRacer Sep 24 '12 at 07:03
  • It could be the variation/change in the `System.getProperty("user.dir")` which may be different in the **Ubuntu** and **Windows** Platform. – KAS Sep 24 '12 at 07:10
  • Also please take care about the File System of both platform. As **"/"** is used on UNIX and **"\"** on Windows to refer the files. – KAS Sep 24 '12 at 07:18
  • I read on SO, that Java File methods manage the separators path very well. – theTuxRacer Sep 24 '12 at 07:21
0

I changed the line from

IOUtils.copy(is, new FileOutputStream(Play.getFile(original)));

to

IOUtils.copy(is, new FileOutputStream("./" + original));

Upload now works.

theTuxRacer
  • 12,329
  • 7
  • 39
  • 58