package com.javarush.task.task31.task3112;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
/*
Загрузчик файлов
*/
public class Solution {
public static void main(String[] args) throws IOException {
Path passwords = downloadFile("https://yastatic.net/morda-logo/i/citylogos/yandex19-logo-ru.png",
Paths.get("/home/flowmaster/Projects/java/test/"));
// for (String line : Files.readAllLines(passwords)) {
// System.out.println(line);
// }
}
public static Path downloadFile(String urlString, Path downloadDirectory) throws IOException {
// implement this method
URL urlSource = new URL(urlString);
Path tempFile;
try (InputStream urlStream = urlSource.openStream()) {
tempFile = Files.createTempFile("temp-", "");
Files.copy(urlStream, tempFile);
}
String fileName = Paths.get(urlString).getFileName().toString();
Path target = Paths.get(downloadDirectory.toString(), fileName);
Files.move(tempFile, target);
return target;
}
}
Что я делаю не так?package com.javarush.task.task31.task3112;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
/*
Загрузчик файлов
*/
public class Solution {
public static void main(String[] args) throws IOException {
Path passwords = downloadFile("https://yastatic.net/morda-logo/i/citylogos/yandex19-logo-ru.png",
Paths.get("/home/flowmaster/Projects/java/test/"));
// for (String line : Files.readAllLines(passwords)) {
// System.out.println(line);
// }
}
public static Path downloadFile(String urlString, Path downloadDirectory) throws IOException {
// implement this method
URL url=new URL(urlString);
InputStream inputStream=url.openStream();
Path tmp=Files.createTempFile("temp-",".tmp");
Files.copy(inputStream,tmp);
String fieName=urlString.substring(urlString.lastIndexOf("/"));
Path destPath=Paths.get(downloadDirectory.toString(), fieName);
Files.move(tmp,destPath);
return destPath;
}
}