使用java在httpclient中下载文件?

发布于 2024-10-13 07:59:18 字数 1551 浏览 2 评论 0原文

我的代码是,

import java.io.*;
import java.net.*;

 public class DownloadHttp
 {
public static void main(String a[])
{
    DownloadHttp d  =   new DownloadHttp();
    String addr =   "http://www.gmail.com";
    String file =   "D:/venkatesh/Software/download1.html";
    d.download(addr,file);
}


    public void download(String address, String localFileName) {
   OutputStream out = null;
   URLConnection conn = null;
  InputStream in = null;
   try {
    // Get the URL
    URL url = new URL(address);
    // Open an output stream to the destination file on our local filesystem
    out = new BufferedOutputStream(new FileOutputStream(localFileName));
    conn = url.openConnection();
    in = conn.getInputStream();

    // Get the data
    byte[] buffer = new byte[1024];
    int numRead;
    while ((numRead = in.read(buffer)) != -1) {
        out.write(buffer, 0, numRead);
    }            
    // Done! Just clean up and get out
} catch (Exception exception) {
    exception.printStackTrace();
} finally {
    try {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    } catch (IOException ioe) {
        // Shouldn't happen, maybe add some logging here if you are not 
        // fooling around ;)
    }
  }
 }
 }

这里我想使用 java 使用 httpClient 下载特定文件。 它产生:

"java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)" as error.

如何解决它,帮助我,提前感谢。

My code is,

import java.io.*;
import java.net.*;

 public class DownloadHttp
 {
public static void main(String a[])
{
    DownloadHttp d  =   new DownloadHttp();
    String addr =   "http://www.gmail.com";
    String file =   "D:/venkatesh/Software/download1.html";
    d.download(addr,file);
}


    public void download(String address, String localFileName) {
   OutputStream out = null;
   URLConnection conn = null;
  InputStream in = null;
   try {
    // Get the URL
    URL url = new URL(address);
    // Open an output stream to the destination file on our local filesystem
    out = new BufferedOutputStream(new FileOutputStream(localFileName));
    conn = url.openConnection();
    in = conn.getInputStream();

    // Get the data
    byte[] buffer = new byte[1024];
    int numRead;
    while ((numRead = in.read(buffer)) != -1) {
        out.write(buffer, 0, numRead);
    }            
    // Done! Just clean up and get out
} catch (Exception exception) {
    exception.printStackTrace();
} finally {
    try {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    } catch (IOException ioe) {
        // Shouldn't happen, maybe add some logging here if you are not 
        // fooling around ;)
    }
  }
 }
 }

Here I wants download specific file using httpClient using java.
It produces:

"java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)" as error.

How to resolve it, help me, thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风疏影 2024-10-20 07:59:18

我相信这是一个网络问题。您是否尝试过直接访问该网址还是位于防火墙后面?

I believe it is a network problem. Have you tried to access the url directly or are you behind a firewall?

病毒体 2024-10-20 07:59:18

在我的机器上重新编译你的代码,它运行得很好。我能够从网络上获取文件。

检查您的网络浏览器是否可以为您下载该文件(确保这不是网络问题),

但需要注意的一件事是,在您的finally块中,您可能需要单独关闭流。因此,如果输入流出现任何问题,输出流仍然会关闭。

finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception ignored) {}
        try {
            if (out != null) {
                out.close();
            }
        } catch (Exception ignored) {}
    }

Recompiled your code on my machine, it works perfectly well. I'm able to fetch files from the web.

Check if your web-browser can download the file for you (make sure it's not a network problem)

One thing to notice though, in your finally block you might want to close the streams separately. So if anything goes wrong with the input stream, the output stream will still be closed.

finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception ignored) {}
        try {
            if (out != null) {
                out.close();
            }
        } catch (Exception ignored) {}
    }
于我来说 2024-10-20 07:59:18

我认为您在连接互联网时使用了代理。

在代码中设置这些,然后重试。

System.setProperty("http.proxyHost", *Proxy-IP*);
System.setProperty("http.proxyPort", *Proxy-Port*);

I think you are using a proxy when connecting to internet.

Set these in the code and then retry.

System.setProperty("http.proxyHost", *Proxy-IP*);
System.setProperty("http.proxyPort", *Proxy-Port*);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文