JPG用JAVA下载Applet

时间:2013-07-16 17:59:49

标签: java applet jpeg

我正在尝试编写一个小程序,供用户通过输入jpg图像的链接列表从网址下载jpg文件。当我在JCreator的编译器中使用该过程时,它运行良好。但是,当我把它放入applet时,我无法处理。我给出了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++;

    }
}    

0 个答案:

没有答案