Skip to content
Snippets Groups Projects
Commit a2b2fcf3 authored by koz01's avatar koz01
Browse files

solution

parent d2ed0691
No related merge requests found
...@@ -33,5 +33,12 @@ ...@@ -33,5 +33,12 @@
<version>5.5.2</version> <version>5.5.2</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.17.1</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
package lab12;
public class App {
public static void main(String[] args) {
while(true) {
String val = System.in.toString();
System.out.println(val);
}
}
}
package lab12;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ClientApp {
public static void main(String[] args) {
try (Socket soc = new Socket(args[0],Integer.parseInt(args[1]));
PrintWriter pw = new PrintWriter(soc.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(soc.getInputStream()));)
{
BufferedReader brc = new BufferedReader(new InputStreamReader(System.in));
String line;
do {
line = brc.readLine();
pw.println(line);
pw.flush();
String lineFromServer = br.readLine();
System.out.println(lineFromServer);
} while (!"quit".equals(line));
System.out.println(br.readLine());
} catch (NumberFormatException | IOException e) {
e.printStackTrace();
}
}
}
package lab12;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class ImgDownloader {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect(args[0]).get();
Elements elements = doc.select("img[src]");
for (Element el: elements) {
String imgUrl = el.attr("abs:src");
System.out.println(imgUrl);
new Thread(() -> downloadImg(imgUrl, args[1]));
}
}
private static void downloadImg(String imgUrl, String pathDir) {
URL url;
try {
url = new URL(imgUrl);
String fileName =url.getPath();
fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
URLConnection connection = url.openConnection();
try (InputStream is = connection.getInputStream() ){
Path path = Paths.get(pathDir, fileName);
try (OutputStream os = new FileOutputStream(path.toFile())) {
byte[] buffer = new byte[2048];
int len;
while (-1 != (len = is.read(buffer))) {
os.write(buffer, 0, len);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
...@@ -24,9 +24,10 @@ public class ServerApp { ...@@ -24,9 +24,10 @@ public class ServerApp {
protected void listen() { protected void listen() {
System.out.println("Server started and ready to connection"); System.out.println("Server started and ready to connection");
while (true) { while (true) {
try(Socket socketFromClient = server.accept()) { try {
Socket socketFromClient = server.accept();
System.out.println("Client connected: " + socketFromClient.getInetAddress()); System.out.println("Client connected: " + socketFromClient.getInetAddress());
new ServerComunicator(socketFromClient).handle(); new Thread(() -> new ServerComunicator(socketFromClient).handle()).start();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -31,6 +31,7 @@ public class ServerComunicator { ...@@ -31,6 +31,7 @@ public class ServerComunicator {
writerToClentSocket.write("May the Java be with you."); writerToClentSocket.write("May the Java be with you.");
writerToClentSocket.newLine(); writerToClentSocket.newLine();
writerToClentSocket.flush(); writerToClentSocket.flush();
this.socket.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
module cz.jezek.lab12 { module cz.jezek.lab12 {
opens lab12 to javafx.fxml; opens lab12 to javafx.fxml;
exports lab12; exports lab12;
requires org.jsoup;
} }
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment