-1

I'm working on a new function on a system, and at the moment I need to pick a file, and save on a folder in the user C:. The process happens as it follows, the user uploads a file into the system, that file can be anything, text, image, except videos, and now it is saved in the system database, but my boss wants to change that process so I need to save on a specific folder on the user C:, I already created the specific folder, but I don't know how to save the file in that created folder.

So the code for uploading a file as it follows:

public void uploadArquivo(FileUploadEvent event) {
    byte[] bytes = null;

    try {
        File targetFolder = new File(System.getProperty("java.io.tmpdir"));
        if (!targetFolder.exists()) {
            if (targetFolder.mkdirs()) {
                FacesMessageUtil.novaMensagem("Atenção", "Não foi possível criar pasta temporária!");
                return;
            }
        }

        targetFolder.mkdirs();
        OutputStream out;

        try (InputStream inputStream = event.getFile().getInputstream()) {
            out = new FileOutputStream(new File(targetFolder, event.getFile().getFileName()));
            int read;
            bytes = new byte[10485760];

            while ((read = inputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }

            inputStream.close();
        }
        out.flush();
        out.close();

    } catch (IOException e) {
        e.printStackTrace(System.out);
    }
    AnexoEmpreendimento anexo = new AnexoEmpreendimento();
    anexo.setNomeArquivo(event.getFile().getFileName());
    anexo.setTamanhoArquivo(event.getFile().getSize());
    anexo.setArquivo(bytes);
    anexos.add(anexo);
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
rickxzx
  • 3
  • 2
  • What have you tried so far? did you try to search for the solution via Google? any other alternatives? have you spent any time for solving this by yourself? – Giorgi Tsiklauri Mar 29 '21 at 14:10
  • 1
    If you store files on `c:` drive, where operating system is mostly installed, your application will need special permissions. – the Hutt Mar 29 '21 at 14:18
  • 1
    I tried via google, It didnt help me at all, most cases when you google something like that is either copy a file, or create a file from scrach on a specific folder, but I didnt find anywhere a solution where the user submits a file and you save it in a folder. I spend some hours searching before coming here – rickxzx Mar 29 '21 at 14:20

1 Answers1

1

Well, it depends on what you actually 'have'. A web submit form? Which web framework do you have then? A BLOB in a db? Which DB engine do you have and which DB framework are you using in java to interact with it, etcetera.

Most libraries will let you obtain either an InputStream or a byte[] (if they offer both, you want the InputStream).

You can write an inputstream to a file, or a byte[] to a file, as follows:

import java.nio.file.*;

public class Example {
    static final Path MY_DIR = Paths.get("C:/path/to/your/dir");

    void writeByteArr(byte[] data) throws IOException {
        Path toWrite = MY_DIR.resolve("filename.dat");
        Files.write(toWrite, data);
    }

    void writeInputStream(InputStream data) throws IOException {
        Path toWrite = MY_DIR.resolve("filename.dat");
        Files.copy(data, toWrite);
    }
}

In the unlikely case the data you 'get' is neither in byte[] nor InputStream form you're going to have to elaborate quite a bit on how the data gets to your code.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72