0

我正在尝试编写一个小程序,让用户通过输入 jpg 图像的链接列表从 url 下载 jpg 文件。当我在 JCreator 的编译器中使用该过程时,它运行良好。但是,当我将它放入小程序时,我无法处理。我给出了 actionlistener 部分和下面程序的方法。你知道为什么它不工作吗?

动作监听器:

class DownloadListener implements ActionListener 
  {
  public void actionPerformed(ActionEvent event)
  {
    String line = links.getText();
    String[] linkarray = line.split("\n");

    //int numOfLinks = linkarray.length;

    String directoryUser = directory.getText();

    try{
    downloadImg(linkarray, directoryUser);
    }
    catch(IOException ex){
    deneme.setText(ex.toString() + "Could not find file");

    }

  }                   
}

方法:

public void downloadImg(String[] linkarray, String directoryUser) throws IOException
{
    for(int i=0; i<linkarray.length; i++)
    {

        URL url = new URL(linkarray[i]);
        InputStream in = new BufferedInputStream(url.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();

        int j = 1;

        String address = directoryUser + "/" + "oda" + Integer.toString(j) + ".jpg";
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(address));

        fos.write(response);
        fos.close();
        j++;

    }
}    
4

0 回答 0