Wie kann ich eine Datei herunterladen, mit einem einfachen HttpClient Beispiel?

Ich bin neu mit Java HttpClient, und ich bin versucht zu tun, ein einfacher download von einem Dropbox-Datei, aber ich bekomme nur die folgende Ausnahme:

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

Warum ist die Ausnahme, die ausgelöst?

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();
    }
  }
}
  • "funktioniert nicht". Das ist eine unzulässige Beschreibung eines Problems, es sei denn, Sie sind ein Programmierer nicht.
  • was tut es?
  • Der trick ist, Lesen Sie die Ausnahme. ClassNotFoundException bedeutet, dass es nicht finden können, eine Klasse. org.apache.commons.logging.LogFactory ist die Klasse nicht gefunden werden kann. Weil es in einer anderen .jar-Datei, die apache-commons-httpclient hängt.
Schreibe einen Kommentar