Rodik
Rodik

Reputation: 271

Creating a file in linux server Java

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.

Upvotes: 0

Views: 747

Answers (4)

Rodik
Rodik

Reputation: 271

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

Upvotes: 0

Stephen C
Stephen C

Reputation: 718788

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.

Upvotes: 0

Niamat H.
Niamat H.

Reputation: 134

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?

Upvotes: 2

MaxPower
MaxPower

Reputation: 871

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.

Upvotes: 1

Related Questions