多文件上传器Java [英] Multiple File Uploader Java

查看:124
本文介绍了多文件上传器Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,可以将图像从一台设备上传到另一台设备.就目前而言,该程序仅允许用户一次上传一个文件.如果我想编辑该程序以允许用户一次上传多个文件,那是最好的方法.

I've created a program to upload images from one device to another. As it stands, the program only allows the user to upload one file at a time. If I wanted to edit the program to allow the user to upload several files at once, what would be the best way of doing it.

String source1 = source.getSelectedFile().getPath();
System.out.println("Source1: " + source1);
String nwdir1 = nwdir.getSelectedFile().getPath() + "\\" + filename;
System.out.println("nwdir1: " + nwdir1);

Path source = Paths.get(source1);
Path nwdir = Paths.get(nwdir1);

try {
    Files.copy(source, nwdir);

我注意到您可以执行.getSelectedFiles(),但是由于该操作不允许.getPath()不确定如何继续.假设您可以执行以下操作:

I've noticed you can do .getSelectedFiles(), but as that doesn't allow .getPath() im unsure how to continue. Assuming you can do this:

File[] source1 = source.getSelectedFiles();

我将如何进行第二行操作:

How would I go about doing the second line:

String nwdir1 = nwdir.getSelectedFile().getPath() + "\\" + filename;

当我用File数组替换行(如上所示)时,在行上出现错误:

When I replace the line with the File array (shown above), I get an error on lines:

Path source = Paths.get(source1);
Path nwdir = Paths.get(nwdir1);

推荐答案

多文件选择

对不起,这就是我想要的,但是您正在使用JFileChooser选择目录,所以我假设您要创建目录列表:P

Sorry, that's what I thought you wanted, but you're using the JFileChooser to select a directory so I assumed you want to do a directory listing :P

使用JFileChooser设置为允许多项选择rel ="nofollow"> setMultiSelectionEnabeld .如果您仍然希望文件选择模式能够选择目录,则可能需要将文件选择模式设置为JFileChooser.FILES_ONLYJFileChooser.FILES_AND_DIRECTORIES.

Set the JFileChooser to allow multiple selections using setMultiSelectionEnabeld. You'll probably want to set the file selection mode to JFileChooser.FILES_ONLY or JFileChooser.FILES_AND_DIRECTORIES if you still want them to be able to select directories.

您可能还希望设置文件过滤器,以允许对话框过滤目录内容,限制用户可以选择的内容,为简单起见,请查看

You will probably also want to set the file filter to allow the dialog to filter the directories contents, restricting what the user can select, for simplicity sake, take a look at FileNameExtensionFilter

已更新

JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "png", "jpg", "jpeg");
chooser.setFileFilter(filter);

switch (chooser.showOpenDialog(null)) {

    case JFileChooser.APPROVE_OPTION:

        String currentPath = chooser.getCurrentDirectory().getPath();
        File[] files = chooser.getSelectedFiles();

        if (files.length > 0) {

            System.out.println("You have choosen " + files.length + " from " + currentPath);

        } else {

            System.out.println("You didn't selected anything");

        }

        break;

}

这篇关于多文件上传器Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆