Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将文件复制到文件夹_Java_File_Jsp_File Upload - Fatal编程技术网

Java 将文件复制到文件夹

Java 将文件复制到文件夹,java,file,jsp,file-upload,Java,File,Jsp,File Upload,我有这个: 在addMusic.jsp中: <form method="post" action="Handler" enctype="multipart/form-data"> <div class="form-group"> <label>Musica:</label> <input type="text" class="form-control" name="add_musica_nome" p

我有这个:

在addMusic.jsp中:

<form method="post" action="Handler" enctype="multipart/form-data">
    <div class="form-group">
        <label>Musica:</label> 
        <input type="text" class="form-control" name="add_musica_nome" placeholder="Nome Musica">
    </div>
    <div class="form-group">
        <label>Localização:</label>
        <input type="file" class="form-control" name="add_musica_path" accept="audio/*">
    </div>
    <div class="form-group">
        <label>Ano:</label>
        <input type="text" class="form-control" name="add_musica_ano" pattern="[0-9]{4,4}">
    </div>
    <div class="form-group">
        <input type="hidden" name="logica" id="logica" value="SMusica"/>
        <input  type="hidden" name="acao" id="acao" value="addMusica"/> 
        <input class="btn btn-success" type="submit" value="Inserir" name="inserir"/>
    </div>
</form>
我打印了音乐的绝对路径(文件),它返回的是C:\Users\Fabio\Desktop\eclipse\ProfJam\uu--\u vertichas.mp3,而不是真正的路径


我的问题是如何获得文件的实际路径?

路径中有一个方法称为

  Path _rp = p.toRealPath();

也许你可以朝这个方向搜索

我搜索了这个方法,我不知道,我想出了这个方法来测试:Path p=Path.get(music.getPath(),music.getName());System.out.println(“路径:+p”);在相同的方法中,在try catch中打印:usersfabiodesktoprofjam_u2;-manterhas.mp3\usersfabiodesktoprofjam--u manterhas.mp3但是如果您的文件绝对数学不正确,那么路径是什么?如果您想复制文件,应该使用如下类文件:Files.copy(source,dest)
final String Dest = "/data/";

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

    String fileName = Paths.get(musica.getSubmittedFileName()).getFileName().toString(); // MSIE fix.

    try {

        File music = new File(fileName);

        System.out.println("Path: " + music.getAbsolutePath());

        if (music.renameTo(new File(Dest + music.getName()))) {
            System.out.println("File is moved successful!");
        } else {
            System.out.println("File is failed to move!");
        }

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

}

@Override
public String executa(HttpServletRequest req, HttpServletResponse res) throws Exception {

    if (req.getParameter("acao").equals("addMusica")) {
        addMusica(req.getParameter("add_musica_nome"), req.getPart("add_musica_path"),
                req.getParameter("add_musica_ano"));
    }
    return "/index.jsp";
}
  Path _rp = p.toRealPath();