如何使用FileChooser使用动态下载路径名下载文件

时间:2015-04-11 16:15:29

标签: java swing filechooser

我有一个文件追踪器:

JFrame parentFrame = new JFrame();
JFileChooser fileChooser = new JFileChooser();

fileChooser.setDialogTitle(txtPushCode.getText());   

int userSelection = fileChooser.showSaveDialog(parentFrame);

if (userSelection == JFileChooser.APPROVE_OPTION) 
{
    File fileToSave = fileChooser.getSelectedFile();
    System.out.println("Save as file: " + fileToSave.getAbsolutePath());
}

和downloadIcon方法:

public boolean downloadIconUrl(String url) 
{
   try 
   {
      String fileName = "C:\\android\\tempcolor\\icon.png"; 

      URL link = new URL(url); // The file that you want to download

      InputStream in = new BufferedInputStream(link.openStream());
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      byte[] buf = new byte[1024];
      int n = 0;
      while (-1 != (n = in.read(buf))) 
      {
         out.write(buf, 0, n);
      }
      out.close();
      in.close();
      byte[] response = out.toByteArray();

      FileOutputStream fos = new FileOutputStream(fileName);
      fos.write(response);
      fos.close();

      System.out.println("Icon downloaded");

   } 
   catch (IOException ex) 
   {
       ex.printStackTrace();
   }

   return true;

但正如您所看到的,它仅下载到" C:\ android \ tempcolor \ icon.png"。我想将它下载到我在FileChooser中选择的路径,其中包含我选择的名称和扩展名。 简而言之,我如何在应用程序中的任何位置使用动态文件路径名?

0 个答案:

没有答案