-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
User Agent and Referer in FileUtils.copyURLToFile(URL, File)
- Loading branch information
1 parent
5066acc
commit 2dfcade
Showing
6 changed files
with
97 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
public class saveFile { | ||
public static boolean saveFile(URL imgURL, String imgSavePath) { | ||
|
||
boolean isSucceed = true; | ||
|
||
CloseableHttpClient httpClient = HttpClients.createDefault(); | ||
|
||
HttpGet httpGet = new HttpGet(imgURL.toString()); | ||
httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"); | ||
httpGet.addHeader("Referer", "https://www.google.com"); | ||
|
||
try { | ||
CloseableHttpResponse httpResponse = httpClient.execute(httpGet); | ||
HttpEntity imageEntity = httpResponse.getEntity(); | ||
|
||
if (imageEntity != null) { | ||
FileUtils.copyInputStreamToFile(imageEntity.getContent(), new File(imgSavePath)); | ||
} | ||
|
||
} catch (IOException e) { | ||
isSucceed = false; | ||
} | ||
|
||
httpGet.releaseConnection(); | ||
|
||
return isSucceed; | ||
} | ||
} |
Binary file modified
BIN
+1.43 KB
(140%)
Spider/target/classes/GOTOP_book_attachment_downloader.class
Binary file not shown.
Binary file not shown.