Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Lesson1/src/network/HttpDownload.java
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();
Copy link
Copy Markdown
Collaborator

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() уже не вызовется.
Что-то типа:

try (InputStream inputStream = ..., FileOutputStream outputStream = ...) {
}

inputStream.close();

System.out.println("Файл " + fileName + " загружен!");
} else {
System.out.println("Ошибка загрузки. Код сервера : " + responseCode);
}
httpConn.disconnect();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И блокировки в finally-блоке

}
}
23 changes: 23 additions & 0 deletions Lesson1/src/network/TestDownload.java
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();

}

}