简体   繁体   中英

How do i create a new directory where i could upload a file in java using play framework

我正在尝试将文件上传到本地计算机,但是现在我想请用户选择目录或创建一个新目录,以将上传的文件保存在该目录中,任何想法如何在游戏框架中实现。

You really want to show a directory list from your server to the user?? Seems like a really bad plan. I also do not see any use case where this would be acceptable, why bothering the user where you store it? Just present him the link after you have saved it.

Anyway, you can create directories with Java (if you use Play-Java) no problem:

File dir = new File("folderName");
dir.mkdir();

If you persist in asking the user in what folder the file should be saved, I would advise you the following:

Create somewhere outside the play project a folder called uploaded . Within this folder the user can upload the file into his own folder, just show an input field where the user can enter a 'foldername'.

Note that uploaded files in Play are not visible directly after upload, because the image did not exist when Play was starting (in classpath). So the uploaded folder should be managed by Apache or any other server.

EDIT If you want to list all file you can use something like:

File[] listOfFiles = folder.listFiles(); // folder is created folder

for (File file : listOfFiles) {
    if (file.isFile()) {
        System.out.println(file.getName());
        // here you can build array of strings (filenames)
        // that can be passed to your view
    }
}

And the pass the new string array or listOfFiles to your view.

return ok(list.render(listOfFiles));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM