I have a website, a zip file is on it. I want to make the user in the program (Java) press the button and download this file, then the user select the save location.

  • What made the site? Jsp? - Chubatiy
  • html and javascript - user257589
  • Then where is the button? In a desktop type application? That is, there is a client for your site? - Chubatiy
  • Yes, in the program Desktop - user257589
  • What is the client made on (Swing, JavaFX, etc.)? - Chubatiy

1 answer 1

You will need a direct link to your file on the server. Here is an example of creating a dialog and saving a file.

  JFileChooser fileChooser = new JFileChooser(); fileChooser.setSelectedFile(new File("myfile.txt")); if (JFileChooser.APPROVE_OPTION == fileChooser.showSaveDialog(null)) { BufferedInputStream bis = null; FileOutputStream fos = null; try { bis = new BufferedInputStream(new URL("http://mysite.com/myfile.txt").openStream()); fos = new FileOutputStream(fileChooser.getSelectedFile()); byte data[] = new byte[1024]; int count; while ((count = bis.read(data, 0, 1024)) != -1) { fos.write(data, 0, count); } } catch (Exception ex) { //TODO: log } finally { if (bis != null) { try { bis.close(); } catch (IOException ex) { //TODO: log } } if (fos != null) { try { fos.close(); } catch (IOException ex) { //TODO: log } } } } 
  • everything works, thank you - user257589
  • not at all. Then mark the answer as true (click the check mark) - Chubatiy