Last active
November 27, 2023 21:22
-
-
Save benelog/03f2965ca2838bdcb71d336c54940ee0 to your computer and use it in GitHub Desktop.
URL을 파일로 복사
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
import java.io.IOException; | |
import java.net.URI; | |
import java.net.http.HttpClient; | |
import java.net.http.HttpRequest; | |
import java.net.http.HttpResponse; | |
import java.nio.file.Path; | |
import java.time.Duration; | |
public class RemoteFileClient { | |
private final HttpClient httpClient; | |
private final Duration readTimeout; | |
public RemoteFileClient(Duration connectionTimeout, Duration readTimeout) { | |
this.httpClient = HttpClient.newBuilder() | |
.connectTimeout(connectionTimeout) | |
.build(); | |
this.readTimeout = readTimeout; | |
} | |
public void copyToPath(URI source, Path destination) { | |
var request = HttpRequest.newBuilder() | |
.uri(source) | |
.timeout(this.readTimeout) | |
.build(); | |
try { | |
httpClient.send(request, HttpResponse.BodyHandlers.ofFile(destination)); | |
} catch (IOException | InterruptedException ex) { | |
throw new IllegalStateException(ex); | |
} | |
} | |
} |
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
import static org.assertj.core.api.Assertions.assertThat; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.time.Duration; | |
import org.junit.jupiter.api.Test; | |
import org.junit.jupiter.api.io.TempDir; | |
class RemoteFileClientTest { | |
@Test | |
void copyToPath(@TempDir Path tempPath) throws IOException, URISyntaxException { | |
var source = new URI("https://blog.benelog.net"); | |
Path destination = tempPath.resolve("test.html"); | |
var client = new RemoteFileClient(Duration.ofSeconds(10L), Duration.ofSeconds(10L)); | |
client.copyToPath(source, destination); | |
assertThat(destination).exists(); | |
var content = Files.readString(destination); | |
assertThat(content).contains("개발수양록"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment