/
SofiMut
/
2dGraphic
Обзор
Документация
Войти
/
SofiMut
/
2dGraphic
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
GServer.java
78 строк
3 KB
Софья Степанова
upload files
23 авг 2025, 08:14
23 авг 2025, 08:14
1a5ea03
Код
Авторство
О чём код?
package ru.netology.graphics.server; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpServer; import ru.netology.graphics.image.TextGraphicsConverter; import java.io.*; import java.net.InetSocketAddress; import java.net.http.HttpResponse; import java.nio.file.Files; import java.nio.file.Path; /** * Сервер уже за вас написан, его трогать не надо :) */ public class GServer { public static final int PORT = 8888; private HttpServer server; private TextGraphicsConverter converter; public GServer(TextGraphicsConverter converter) throws Exception { if (converter == null) { throw new IllegalArgumentException("Серверу нужно передать в конструктор объект-конвертер, а было передано null."); } this.converter = converter; this.converter.setMaxHeight(300); this.converter.setMaxWidth(300); this.converter.setMaxRatio(4); server = HttpServer.create(new InetSocketAddress("localhost", PORT), 0); server.createContext("/", this::serveHtml); server.createContext("/convert", this::serveConvertion); } public void start() { System.out.println("Запускаем сервер на порту " + PORT); System.out.println("Открой в браузере http://localhost:8888/"); server.start(); } protected void serveHtml(HttpExchange h) throws IOException { System.out.println("Serving html.."); var htmlPath = Path.of("assets/index.html"); var htmlContent = Files.readString(htmlPath); var jsPath = Path.of("assets/my.js"); var jsContent = Files.readString(jsPath); htmlContent = htmlContent.replace("{{{JS}}}", jsContent); var htmlBytes = htmlContent.getBytes(); h.sendResponseHeaders(200, htmlBytes.length); h.getResponseBody().write(htmlBytes); h.close(); } protected void serveConvertion(HttpExchange h) throws IOException { System.out.println("Convert request.."); var url = new BufferedReader(new InputStreamReader(h.getRequestBody())).readLine(); try { System.out.println("Converting image: " + url); Files.write(Path.of("assets/img.txt"), converter.convert(url).getBytes()); var img = converter.convert(url).getBytes(); System.out.println("...converted!"); h.sendResponseHeaders(200, img.length); h.getResponseBody().write(img); } catch (Exception e) { e.printStackTrace(); var msg = e.getMessage(); if (msg.isEmpty()) { msg = "Произошла ошибка конвертации :'("; } var msgBytes = msg.getBytes(); h.sendResponseHeaders(500, msgBytes.length); h.getResponseBody().write(msgBytes); } finally { h.close(); } } }