java applet HTTP下载文件无法正常工作

时间:2015-01-12 15:46:55

标签: java applet

我编写简单的applet来从HTTP URL下载文件。 在Eclipse或Netbeans中,它运行良好,可以将文件下载到我的硬盘上的d://abc//123.iso。 这是我的代码:

public class download {
public static void saveUrl(final String filename, final String urlString)
        throws MalformedURLException, IOException {
    BufferedInputStream in = null;
    FileOutputStream fout = null;
    try {
        in = new BufferedInputStream(new URL(urlString).openStream());
        fout = new FileOutputStream(filename,true);

        final byte data[] = new byte[1024];
        int count;
            fout.write(data, 0, count);

    } finally {
        if (in != null) {
            in.close();
        }
        if (fout != null) {
            fout.close();
        }
    }
}

}

public class HelloWorldApplet extends Applet
{
   public void paint (Graphics g)
   {
      g.drawString ("Download file", 25, 50);
String url ="http://downloads.asterisk.org/pub/telephony/asterisk-now/AsteriskNOW-612-current-32.iso";
                    String file_out = "d:\\abc\\123.iso"; 
                        download.saveUrl(file_out, url);

   }
}

==========================

但是当导出到jar文件并使用html运行时,浏览器可以在我的硬盘上创建新文件123.iso,但此文件的大小始终为2 Kbps。我认为它不下载任何东西。  请帮我  非常感谢 P / s:我尝试用jarsigner签署jar文件,但它没有解决问题

2 个答案:

答案 0 :(得分:0)

您正在写入输入中的第一个读取。您需要编写文件,直到输入为空。

在你的代码中试试这个

while ((count = in.read(data)) != -1) { fout.write(data, 0, count); ... }

答案 1 :(得分:0)

虽然我对上面的代码所做的任何事情都持怀疑态度,如果发布的话,即使是编译,这里也是我用来自动更新下载大型(> 100 MB)文件的解决方案:

            HttpGet httpGet;
            RequestConfig requestConfig;
            getProxySettings();

            //Check to see if there is a proxy availabble. 
            if (!LicensePreloader.proxyAddr.equals("")) {
                requestConfig = RequestConfig.custom()
                        .setSocketTimeout(5000)
                        .setConnectTimeout(5000)
                        .setConnectionRequestTimeout(5000)
                        .setProxy(new HttpHost(LicensePreloader.proxyAddr, LicensePreloader.proxyPort))
                        .build();

            } else {
                //No proxy was available, just use regular internet. 
                requestConfig = RequestConfig.custom()
                        .setSocketTimeout(5000)
                        .setConnectTimeout(5000)
                        .setConnectionRequestTimeout(5000)
                        .build();
            }
            httpGet = new HttpGet(this.remoteUrl);
            HttpResponse response;
            InputStream remoteContentStream = null;
            OutputStream localFileStream = null;
            try {
                httpGet.setConfig(requestConfig);
                response = httpClient.execute(httpGet);

                //This builds the content of our file we're downloading. 
                remoteContentStream = response.getEntity().getContent();
                long fileSize = response.getEntity().getContentLength();
                File dir = localFile.getParentFile();
                dir.mkdirs();

                localFileStream = new FileOutputStream(localFile);
                //Set the buffer, in our use case, it's always the deafult 8192 bytes.
                byte[] buffer = new byte[bufferSize];
                int sizeOfChunk;
                int amountComplete = 0;
                //Simply loop through and download the file in 'chunks' 
                while ((sizeOfChunk = remoteContentStream.read(buffer)) != -1) {
                    localFileStream.write(buffer, 0, sizeOfChunk);
                    amountComplete += sizeOfChunk;
                    updateProgress(amountComplete, fileSize);

                }
                return localFile;
            } finally {
                //Make sure to clean everything up. 
                try {
                    if (remoteContentStream != null) {
                        remoteContentStream.close();
                    }
                    if (localFileStream != null) {
                        localFileStream.close();
                    }
                } catch (IOException ex) {
                    //If we're here, it's likely because the internet conneciton
                    //couldn't be established, or it was cut short in the middle. 
                    ex.printStackTrace(System.out);
                    failed();
                }
            }
        }

这显然对你的应用程序来说太过分了,你可能会忘记所有的代理业务,但为了完整起见我把它保存在那里。有一些我没有包含的辅助方法,但同样,它们几乎全部用于代理处理。

祝你好运!