Java 按下按钮后从服务器下载文件
特定位置的文件将添加到服务器项目中的另一个文件夹中 项目文件共享服务器Java 按下按钮后从服务器下载文件,java,button,javafx,file-sharing,Java,Button,Javafx,File Sharing,特定位置的文件将添加到服务器项目中的另一个文件夹中 项目文件共享服务器 private boolean writefiletoServerfolder() throws IOException { String filetype = categorycombo.getValue().toString(); // int i = 0; File file = null; File imagefile = null; String imagename=null
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) {
}
});
}
我已经实现了从服务器下载文件。我只需要在按下按钮时进行下载。在按下按钮时调用您的方法不起作用 比如:
只需调用您编写的方法
public void downloadbuttonAction() {
downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
writefiletoServerfolder();
}
});
}