0

i upload a csv file from client side and i want to create this file in server side.

Here is my function

    public void uploadFile(FileUploadEvent e) throws IOException{

    UploadedFile uploadedCsv=e.getFile();

    String filePath="//ipAdress:/home/cg/Temp/input/ressource.csv"; 

    byte[] bytes=null;

    if(uploadedCsv != null){
        bytes=uploadedCsv.getContents();
        BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
        String filename = FilenameUtils.getName(uploadedCsv.getFileName());
        stream.write(bytes);
        stream.close();
    }
    }

When I want to write the file I get this exception (No such file or directory)

SEVERE: java.io.FileNotFoundException: /ipAdress:/home/cg/Temp/input/ressource.csv (No such file or directory)

Knowing that the / home / cg / Temp / input path is created on the server.

Rodik
  • 271
  • 2
  • 19
  • Which machines are Linux? The client? The server? Both? – Stephen C Aug 07 '17 at 15:42
  • @StephenC server machine are Linux And The client can interact from any machine from a web browser – Rodik Aug 16 '17 at 13:40
  • 1
    In that case ... why are you trying to use a **Windows** UNC pathname syntax on Linux? Linux doesn't support UNC paths. – Stephen C Aug 16 '17 at 13:50
  • @StephenC I use the path of the file on the server are. The path is ipAdress/home/cg/Temp/input/ – Rodik Aug 16 '17 at 13:53
  • So what is that colon doing in the pathname in your example? And in the error message? – Stephen C Aug 16 '17 at 14:04
  • @StephenC trying this new File(new URI(filePath)) instead of new File(filePath) i get this erreur. SEVERE: java.lang.IllegalArgumentException: URI is not absolute – Rodik Aug 16 '17 at 14:13

4 Answers4

2

Could you try:

String filePath="////ipAdress/home/cg/Temp/input/ressource.csv";

Instead of:

String filePath="//ipAdress:/home/cg/Temp/input/ressource.csv";

And this:

new File(new URI(filePath))

Instead of:

new File(filePath)

Or you can use jcif API How can I open a UNC path from Linux in Java?

Niamat H.
  • 114
  • 7
  • I would like to thank you for your reply first but unfortunately the problem still persists. – Rodik Aug 07 '17 at 14:49
  • 1
    How about String filePath="////ipAdress/home/cg/Temp/input/ressource.csv"; ? – Niamat H. Aug 07 '17 at 15:01
  • Problem still appear, but with // or //// In the trace I always have only one "/" – Rodik Aug 07 '17 at 15:09
  • 1
    You can open the file from the terminal, if so try to test that you can its existence with File.exists() follow the java doc http://docs.oracle.com/javase/6/docs/api/java/io/File.html#exists() – Niamat H. Aug 07 '17 at 15:32
  • when i try it i get this erreur SEVERE: java.lang.IllegalArgumentException: URI is not absolute – Rodik Aug 16 '17 at 13:06
1

I would use the <file>.mkdirs(); at one level above the file itself.

So do String filePath="//ipAdress:/home/cg/Temp/input File directory = new File(filePath); directory.mkdirs();

You can then make the file File tempFile = new File(directory + "/ressource.csv);

Or a cleaner solution all around is just use Files.createTempFile(prefix, suffix) this will create a file in the temp directory of the system.

MaxPower
  • 881
  • 1
  • 8
  • 25
0

The reason that your code does not work is that you are trying to use a UNC pathname on Linux. Linux does not support UNC pathnames ... natively. They are a Windows-ism.

Here's your example

"//ipAdress:/home/cg/Temp/input/ressource.csv";

If you try to use that on Linux, the OS will look for a directory in the root directory of the file system. The directory it will look for will have the name ipaddress: ... noting that there is a colon in the directory name!

That will most likely fail ... because no directory with that name exists in the / directory.. And the exception message you are getting is consistent with this diagnosis.

If you are doing this because you are trying to push files out to other systems then you are going to do it some other way. For example:

(Which ever way you do it, there are security issues to consider!)

trying this new File(new URI(filePath)) instead of new File(filePath) i get this erreur. SEVERE: java.lang.IllegalArgumentException: URI is not absolute

It won't work. A UNC name is NOT a valid URL or URI.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

I have found a solution for this problem, but it's not smart and still and it works

    String fileName="ressource.csv";

    File f = new File(System.getProperty("user.home") + "/Temp/input",fileName);
    if (f.exists() && !f.canWrite())
        throw new IOException("erreur " + f.getAbsolutePath());
    if (!f.exists())
        Files.createFile(f.toPath());
    if (!f.isFile()) {
        f.createNewFile(); // Exception here
    } else {
        f.setLastModified(System.currentTimeMillis());
    }

Pending a more intelligent solution

Rodik
  • 271
  • 2
  • 19