按下按钮从服务器下载文件

时间:2014-09-06 14:59:13

标签: java button javafx file-sharing

某个位置的文件将添加到“服务器”项目中的另一个文件夹中。

项目FileShareServer

private boolean writefiletoServerfolder() throws IOException {
    String filetype = categorycombo.getValue().toString();
    // int i = 0;
    File file = null;
    File imagefile = null;
    String imagename=null;
    String filename=null;
    FileChannel channel = null;
    FileOutputStream fileOutputStream = null;
    FileInputStream fileinputstream = null;
    //map=new HashMap<>();


    for (int i = 0; i < fileList.size(); i++) {
        if (filetype.equals("Apps")) {
            file = new File("D:\\SERVER\\Server Content\\Apps\\" + fileList.get(i).getName());
            imagefile = new File("D:\\SERVER\\Server Content\\Apps\\icons\\" + imagelist.get(i).getName());
            imagename=imagefile.getName();
            filename=file.getName();
            //map.put(filename, imagename);

        } else if (filetype.equals("Games")) {
            file = new File("D:\\SERVER\\Server Content\\Games\\" + fileList.get(i).getName());
            imagefile = new File("D:\\SERVER\\Server Content\\Games\\icons\\" + imagelist.get(i).getName());
           imagename=imagefile.getName();
            filename=file.getName();
           // map.put(filename, imagename);

        } else if (filetype.equals("Movies")) {
            file = new File("D:\\SERVER\\Server Content\\Movies\\" + fileList.get(i).getName());
            imagefile = new File("D:\\SERVER\\Server Content\\Movies\\icons\\" + imagelist.get(i).getName());
            imagename=imagefile.getName();
            filename=file.getName();
            //map.put(filename, imagename);

        } else if (filetype.equals("Songs")) {
            file = new File("D:\\SERVER\\Server Content\\Songs\\" + fileList.get(i).getName());
            imagefile = new File("D:\\SERVER\\Server Content\\Songs\\icons\\" + imagelist.get(i).getName());
            imagename=imagefile.getName();
            filename=file.getName();
           // map.put(filename, imagename);
        }
    }

    List<File> fileandimagedest = new ArrayList();
    fileandimagedest.add(file);
    fileandimagedest.add(imagefile);

    List<String> fileandimagesrc = new ArrayList();
    fileandimagesrc.add(filepath);
    fileandimagesrc.add(imagepath);
    boolean bool = false;

    for (String path : fileandimagesrc) {
        for (File file1 : fileandimagedest) {
            try {
                fileinputstream = new FileInputStream(path);
                fileOutputStream = new FileOutputStream(file1);

                long starttime = System.currentTimeMillis();
                byte[] buf = new byte[1024];
                int byteRead;
                while ((byteRead = fileinputstream.read(buf)) > 0) {
                    fileOutputStream.write(buf, 0, byteRead);
                    bool = true;
                }
            } finally {
                if (bool) {
                    fileinputstream.close();
                    fileOutputStream.close();
                }

            }
        }
    }
    return true;
}

我需要的是在客户端按下按钮时将文件从服务器下载到客户端。

项目文件共享客户端。

public void downloadbuttonAction() {
    downloadbtn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {

        }
    });
}

我已经实现了从服务器下载文件。我只需按下按钮即可。

1 个答案:

答案 0 :(得分:0)

按下按钮调用方法不起作用?

像:

    public void downloadbuttonAction() {
    downloadbtn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            writefiletoServerfolder();
        }
    });
}