How to upload a temporary file to an ftp server?

0

I have the following situation, I want to upload a temporary file to an FTP server, I have the following code:

public static void uploadFileFtp(String file,FTPClient client,String name) throws IOException {
        File files = File.createTempFile(name, ".txt");
        String[] lines = file.split("\n");
        int length = lines.length;
        BufferedWriter bw = new BufferedWriter(new FileWriter(files));
        for(int i=0;i<length;i++) {
            bw.write(lines[i]);
        }
        bw.close();
        InputStream stream = new FileInputStream(files.getName());
        boolean upload = client.storeFile(files.getName(), stream);
        if(upload)
            System.out.println("Hecho, numero de saltos de linea : "+length);
    }

File is a string that I want to write in my temporary file (which it already does), then I try to get the name of that file and upload it to my ftp, but I get the following exception:

java.io.FileNotFoundException: 103279143710072458372941584.txt (El sistema no puede encontrar el archivo especificado)

They could help me know how I can get the route where the storm is stored and can get that file.

Edit 1 I could find the file, I had to find it in the following way:

InputStream stream = new FileInputStream(files.getAbsolutePath());

Now my next question, is there any way I can change the name of that temporary file?

Thank you.

    
asked by 5frags 27.10.2018 в 19:10
source

1 answer

1
  

Is there a way to change the name to a temporary file using the File.createTempFile(...) method?

No, it is not possible since this method has some restrictions to avoid collisions between files with the same name.

The only thing that this method allows is to add prefix ( prefix ), a suffix ( suffix ) and in case of calling it with a third parameter a directory where to save the file.

For example:

tmpFile = File.createTempFile("tmp-", ".txt");
System.out.println("Ruta Archivo: " + tmpFile.getAbsolutePath());
// Ruta Archivo: X:\WINNT\TEMP\tmp-78345734.txt

More information about this method can be seen here:

createTempFile(String prefix, String suffix, File directory)

By the way, the number between the prefix and the suffix is generated using the class SecureRandom that provides a cryptographically strong random number generator.

private static final SecureRandom random = new SecureRandom();
static File generateFile(String prefix, String suffix, File dir)
    throws IOException
{
    long n = random.nextLong();
    if (n == Long.MIN_VALUE) {
        n = 0;      // corner case
    } else {
        n = Math.abs(n);
    }

    // Use only the file name from the supplied prefix
    prefix = (new File(prefix)).getName();

    String name = prefix + Long.toString(n) + suffix;
...

On the other hand, if you are interested in trying to emulate this behavior and you control how the temporary file is created as well as the name you can choose to recover the system property java.io.tmpdir of the following two ways:

System.out.println(System.getProperty("java.io.tmpdir"));
System.out.println(System.getenv("temp"));
// Puedes recibir algo como esto:
// X:\WINNT\TEMP\
// X:\WINNT\TEMP

And I continued to control the way you create the file, in case you want to see how to do this you can choose to see this answer link

    
answered by 27.10.2018 / 21:42
source