-
Notifications
You must be signed in to change notification settings - Fork 0
ДЗ Загрузка файлов в потоках. #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,70 @@ | ||
| package network; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.URL; | ||
| import java.util.concurrent.Semaphore; | ||
|
|
||
| public class HttpDownload { | ||
| private static final int BUFFER_SIZE = 4096; | ||
|
|
||
| public static void downloadFile(String fileURL, String saveDir) throws IOException { | ||
|
|
||
| URL url = new URL(fileURL); | ||
| HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); | ||
| int responseCode = httpConn.getResponseCode(); | ||
|
|
||
| if (responseCode == HttpURLConnection.HTTP_OK) { | ||
|
|
||
| String fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1); | ||
| String saveFilePath = saveDir + File.separator + fileName; | ||
|
|
||
| InputStream inputStream = httpConn.getInputStream(); | ||
| FileOutputStream outputStream = new FileOutputStream(saveFilePath); | ||
|
|
||
| int bytesRead = -1; | ||
| byte[] buffer = new byte[BUFFER_SIZE]; | ||
|
|
||
| while ((bytesRead = inputStream.read(buffer)) != -1) { | ||
| outputStream.write(buffer, 0, bytesRead); | ||
| } | ||
|
|
||
| outputStream.close(); | ||
| inputStream.close(); | ||
|
|
||
| System.out.println("Файл " + fileName + " загружен!"); | ||
| } else { | ||
| System.out.println("Ошибка загрузки. Код сервера : " + responseCode); | ||
| } | ||
| httpConn.disconnect(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ресурсы стоит всегда освобождать в finally-блоке |
||
| } | ||
|
|
||
| } | ||
|
|
||
| class DownloadtThread implements Runnable { | ||
| Semaphore sem; | ||
| String url; | ||
| String destinationFile; | ||
|
|
||
| public DownloadtThread(String url, Semaphore sem, String destinationFile) { | ||
| this.sem = sem; | ||
| this.url = url; | ||
| this.destinationFile = destinationFile; | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| try { | ||
| sem.acquire(); | ||
| HttpDownload.downloadFile(url, destinationFile); | ||
| } catch (InterruptedException e) { | ||
| e.printStackTrace(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| sem.release(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. И блокировки в finally-блоке |
||
| } | ||
| } | ||
This file contains hidden or 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,23 @@ | ||
| package network; | ||
|
|
||
| import java.util.concurrent.Semaphore; | ||
|
|
||
| public class TestDownload { | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
|
|
||
| Semaphore sem = new Semaphore(2); | ||
|
|
||
| String imageUrl = "https://catspaw.ru/wp-content/uploads/2017/03/Seryiy-kot-Daniel-Felicia.jpg"; | ||
| String destinationFile = "C:/TEMP"; | ||
| new Thread(new DownloadtThread(imageUrl, sem, destinationFile)).start(); | ||
| imageUrl = "https://static.probusiness.io/720x480c/n/03/d/38097027_439276526579800_2735888197547458560_n.jpg"; | ||
| new Thread(new DownloadtThread(imageUrl, sem, destinationFile)).start(); | ||
| imageUrl = "https://cdn2.img.ria.ru/images/148393/86/1483938656.jpg"; | ||
| new Thread(new DownloadtThread(imageUrl, sem, destinationFile)).start(); | ||
| imageUrl = "https://icdn.lenta.ru/images/2018/09/07/09/20180907092850529/pic_32874ef1a26ef118dd32a33a72f277af.jpg"; | ||
| new Thread(new DownloadtThread(imageUrl, sem, destinationFile)).start(); | ||
|
|
||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше бы воспользоваться try-with-resource, а то если outputStream.close() бросил исключение, то inputStream.close() уже не вызовется.
Что-то типа: