Java图形界面(GUI) 动态获取上传或下载文件的路径问题

最近做一个文件的上传与下载时遇到了动态获取路径的问题,在此总结一下。

   //动态获取上传文件路径的代码

  int result = 0;  
 File file = null;  
 String path = null;  
 Component chatFrame = null;
 JFileChooser fileChooser = new JFileChooser();  
 FileSystemView fsv = FileSystemView.getFileSystemView();     //注意了,这里重要的一句  
 System.out.println(fsv.getHomeDirectory());                  //得到桌面路径  
 fileChooser.setCurrentDirectory(fsv.getHomeDirectory());  
 fileChooser.setDialogTitle("请选择要上传文件的路径");  
 fileChooser.setApproveButtonText("确定");  
 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  
 
  result = fileChooser.showOpenDialog(chatFrame);  
 if (JFileChooser.APPROVE_OPTION == result) {  
     path=fileChooser.getSelectedFile().getPath();  
     System.out.println("path: "+path);  

  }



动态获取下载文件的路径代码

        //动态获取保存文件路径
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.showSaveDialog(null);
        String path = chooser.getSelectedFile().getPath();
        System.out.println(path+"\\汇总表.xls");      
        File filewrite=new File(path+"\\汇总表.xls");       
        filewrite.createNewFile();
        //创建文件流
        OutputStream os=new FileOutputStream(filewrite);
        //生成excel
        WriteExcelFileUtil.createExcel(os);    //调用已经写好的文件生成方法
        //提示框
        JOptionPane.showMessageDialog(null, "保存成功!");


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值