Reputation: 6556
I'm new with Java and HttpClient, and I'm trying to do a simple download from a Dropbox file but I just get the following exception:
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
Why is the exception thrown?
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();
}
}
}
Upvotes: 0
Views: 20971
Reputation: 1323
I think you have to add the lines below to save your file.
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();`
Upvotes: 0
Reputation: 22847
First of all, if you're new to Java, you must learn about managing Java dependencies.
Either you download binary distribution with dependencies and copy them all to your project and add to Eclipse, or you learn to use maven.
For example, you add the dependency:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.0-alpha4</version>
</dependency>
and maven is doing everything else for you (downloading all dependencies with their dependencies).
Upvotes: 0
Reputation: 27104
It works fine on my machine if I change the file path to a valid path and add all of the libraries it needs to the classpath.
String filePath = "d:\\test.zip";
Libraries:
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
Upvotes: 3