How to get the real path to a file

Asked

Viewed 33 times

0

I upload files to a folder, which works when running in eclipse. But when I try to upload through some browser, I am not allowed due to the security of it.

My question is, is there any way to get the browser to return the file location? Or if there is any way to upload the file to copy it to the destination folder?

If necessary to prepare a reply I leave below my code.

//caminho relativo da pasta destino
final String Dest = System.getProperty("user.dir") + "/";

public void addMusica(String musica_nome, Part musica, String ano) throws IOException {

    String fileName = getFileName(musica);

    File data = null;

    try {

        File music = new File(fileName);

        data = new File(Dest + music.getName());

        Files.copy(music.toPath(), data.toPath());

    } catch (Exception e) {
        e.printStackTrace();
    }

}

//tratamento do path
private String getFileName(Part part) {

    final String partHeader = part.getHeader("content-disposition");

    for (String content : part.getHeader("content-disposition").split(";")) {
        if (content.trim().startsWith("filename")) {
            return content.substring(content.indexOf('=') + 1).trim().replace("\"", "");
        }
    }
}
  • 2

    No. This is a Feature browser precisely to protect the user from having the disk structure accessed. I, for example, don’t want any website to know about my family photos stored in C: notporn reallynotporn Juju.png. If you need access to the user’s disk, maybe you should rethink the design of your application, Maybe web is not the way.

  • You want a website to access/control the user’s machine?

  • @Renan sometimes I see a text of you and I wonder if it was I who wrote it?

  • If I type @Renan I index who?

  • @Guilhermenascimento not only want to upload files, I’ve never done it before but I have a project in mind that might be something for me

  • 1

    @Everson I received the notification.

  • 1

    But if you want to upload then what is the need to get the location?

  • Ou se existe alguma forma de enviar o file para poder copiá-lo para a pasta destino A control <input type="file"> does just that.

  • This using Servlet?

  • @Renan if that is so, then the problem is in the back-end, the JSP does not treat the upload "alone" has to write the doPOST

  • @Guilhermenascimento Yes I’m using Servlet.

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.