无法确定如何在字符串中存储“另存为”文本并使用它

时间:2012-04-12 16:54:57

标签: java swing awt jfilechooser

enter image description here

我是Java中UI设计的新手。我正在尝试创建一个GUI来从Internet下载文件并将其保存在硬盘上。我有代码工作,除了我要添加的一件事。我添加了JFileChooser,允许用户选择目标文件夹。但我无法弄清楚如何将filename更改为用户在Save As菜单上的JFileChooser栏中输入的内容。

浏览按钮

browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
    chooser = new JFileChooser();
    chooser.setCurrentDirectory(null);
    chooser.setDialogTitle("Select folder to save");
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.setAcceptAllFileFilterUsed(true);

    //chooser.showDialog(downloadButton, "Save");
    if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
    {
        System.out.println("The location to save is: " + chooser.getCurrentDirectory());
        DESTINATION_FOLDER = chooser.getCurrentDirectory().toString();
    }
}

});

下载按钮

URLConnection connection = downloadUrl.openConnection();

input = new BufferedInputStream(connection.getInputStream());
output = new FileOutputStream(DESTINATION_FOLDER + "/" + filename);

此处filename应该是用户输入的内容。关于如何完成这项工作的指示?

3 个答案:

答案 0 :(得分:1)

实际上,您不需要从JFileChooser中的Save As栏获取FileName。 就这样做:

    browseButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e){
        chooser = new JFileChooser();
        chooser.setCurrentDirectory(null);
        chooser.setDialogTitle("Select folder to save");
        //Don't use the 'FileSelectionMode();'. Let it be Default.
        chooser.setAcceptAllFileFilterUsed(true);
   if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
   {
    file = chooser.getSelectedFile();
    //file should be declared as a File.
     System.out.println("The location to save is: " + chooser.getCurrentDirectory();));
     System.out.println("The FileName is: " + file.getName()); 
   }
} 

下载按钮:

   URLConnection connection = downloadUrl.openConnection();
   input = new BufferedInputStream(connection.getInputStream());
   output = new FileOutputStream(file);  

答案 1 :(得分:0)

如果没有看到更多代码,我建议的最好的方法是在您正在使用的类中创建一个全局字符串。

public class gui extends JFrame{
    public String filePath="";
    public static void main(String args[]){
        //button code
        browseButton.addActionListener(new ActionListener())
        saveAsButton.addActionListener(new ActionListener())
        URLConnection connection = downloadUrl.openConnection();



    }

public void actionPerformed(ActionEvent e)
{
    if (e.getActionCommand().equals("browseButton"){
        chooser = new JFileChooser();
        chooser.setCurrentDirectory(null);
        chooser.setDialogTitle("Select folder to save");
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setAcceptAllFileFilterUsed(true);

        //chooser.showDialog(downloadButton, "Save");
        if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
        {
            System.out.println("The location to save is: "+chooser.getCurrentDirectory());
            filePath = chooser.getCurrentDirectory().toString();
        }
    else{
        //save as button selected
        input = new BufferedInputStream(connection.getInputStream());
        output = new FileOutputStream(filePath);
    }
}

}

答案 2 :(得分:0)

添加以下行:

chooser.setDialogType(JFileChooser.SAVE_DIALOG);

完整代码:

browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
    chooser = new JFileChooser();
    chooser.setCurrentDirectory(null);
    chooser.setDialogTitle("Select folder to save");
    chooser.setDialogType(JFileChooser.SAVE_DIALOG);
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.setAcceptAllFileFilterUsed(true);

    //chooser.showDialog(downloadButton, "Save");
    if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
    {
        System.out.println("The location to save is: " + chooser.getCurrentDirectory());
        DESTINATION_FOLDER = chooser.getCurrentDirectory().toString();
    }
}