How to get current directory of a web application?

2

I need to know how I get the current directory of my application, since I need to save some PDF files. I've tried it a few times, but to no avail. Can anyone give me a light? Look how I'm doing:

public String salvarPDF() throws IOException {
    String nomeArquivo = FilenameUtils.getName(arquivoUploadPDF.getFileName());
    InputStream input = arquivoUploadPDF.getInputstream();
    String caminhoCompleto = System.getProperty("user.dir") + "\src\main\webapp\uploads\pdfs";
    String caminho = "uploads\pdfs";
    OutputStream output = new FileOutputStream(new File(caminhoCompleto, nomeArquivo));
    caminho += "\" + nomeArquivo;
    
asked by anonymous 19.08.2016 / 05:42

2 answers

3

Another alternative:

URL location = getClass().getProtectionDomain().getCodeSource().getLocation();
System.out.println(location.getFile());

The advantage of this version is that it also works with paths within packages (in jar , war , ear , etc).

Source : SOen - Getting the Current Working Directory in Java

    
19.08.2016 / 14:59
2

It's quite simple:

String caminhoApp = new File("").getAbsolutePath();
    
19.08.2016 / 14:45