如何使用简单的HttpClient示例下载文件?

时间:2013-01-26 17:19:57

标签: java apache-httpclient-4.x

我是Java和HttpClient的新手,我正在尝试从Dropbox文件中进行简单的下载,但我得到以下异常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:187)
    at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:146)
    at downlaodtest.DownlaodTest.main(DownlaodTest.java:23)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 3 more
Java Result: 1

为什么会抛出异常?

public class DownlaodTest {
  public static void main(String[] args) throws IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet("https://dl.dropbox.com/s/ex4clsfmiu142dy/test.zip");
    HttpResponse response = httpclient.execute(httpget);
    System.out.println(response.getStatusLine());
    HttpEntity entity = response.getEntity();
    if (entity != null) {
      InputStream instream = entity.getContent();
      try {
        BufferedInputStream bis = new BufferedInputStream(instream);
        String filePath = "C:/@Victor";
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
        int inByte;
        while ((inByte = bis.read()) != -1 ) {
          bos.write(inByte);
        }
        bis.close();
        bos.close();
      } catch (IOException ex) {
        throw ex;
      } catch (RuntimeException ex) {
        httpget.abort();
        throw ex;
      } finally {
        instream.close();
      }
      httpclient.getConnectionManager().shutdown();
    }
  }
}

3 个答案:

答案 0 :(得分:3)

如果我将文件路径更改为有效路径并将所需的所有库添加到类路径中,它在我的机器上运行正常。

String filePath = "d:\\test.zip";

库:

commons-codec-1.6.jar
commons-logging-1.1.1.jar
fluent-hc-4.2.3.jar
httpclient-4.2.3.jar
httpclient-cache-4.2.3.jar
httpcore-4.2.2.jar
httpmime-4.2.3.jar

答案 1 :(得分:0)

我认为您必须添加以下行来保存文件。

response.addHeader(“Content-Disposition”, “attachment;filename=\”" + file.getName() + “\”");
response.addHeader(“Content-Transfer-Encoding”, “binary”);`
response.setContentType(“application/octet-stream”);`
response.setContentLength((int) file.length());`
response.getOutputStream().write(buffer);`
response.getOutputStream().flush();`

答案 2 :(得分:0)

首先,如果您是Java新手,您必须学习如何管理Java依赖项。

要么下载带有依赖项的二进制发行版并将它们全部复制到项目中并添加到Eclipse中,要么学会使用 maven

例如,您添加依赖项:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.0-alpha4</version>
</dependency>

并且maven正在为您做其他事情(下载所有依赖项及其依赖项)。