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

    xiaoxiao2021-04-05  41

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

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

      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, "保存成功!");

    转载请注明原文地址: https://ju.6miu.com/read-666333.html

    最新回复(0)