I want to download picture from URL - direct link to a picture. It is somehow protected, when I try :
InputStream in = new URL("http://www.somesite.sk/somepicture.jpg")
Files.copy(in, Paths.get("C:/picture.jpg"));
it downloads, but not as the original picture, the file "picture.jpg" has only this text inside :
<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="http://www.somesite.sk/somepicture.jpg">here</a></body>
when I try direct download - right mouse-click and Save Picture, it works, if I try some download manager, it works. Some idea?
Just tested it. Put these dependencies into your pom.xml file.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.2</version>
</dependency>
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.File;
public class ImageDownloader {
public static void main(String[] args) {
HttpClient instance = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet("https://yourhost.blah/yuorfile.jpg");
try {
HttpResponse response = instance.execute(httpGet);
FileUtils.copyInputStreamToFile(response.getEntity().getContent(), new File("output.jpg"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
hope it helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.