/
times
/
convert
Обзор
Документация
Войти
/
times
/
convert
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
GServer.java
94 строки
4 KB
times
create: Main.java, GServer.java, TextColorSchema.java, TextGraphicsConverter.java, TextGraphicsConverterImpl.java, BadImageSizeException.java, DefaultTextColorSchema.java
28 апр 2026, 19:49
Верифицирован
28 апр 2026, 19:49
c784040
Код
Авторство
О чём код?
package ru.netology.graphics.server; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpServer; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import ru.netology.graphics.image.TextGraphicsConverter; 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."); } else { this.converter = converter; this.converter.setMaxHeight(300); this.converter.setMaxWidth(300); this.converter.setMaxRatio((double) 4.0F); this.server = HttpServer.create(new InetSocketAddress("localhost", 8888), 0); this.server.createContext("/", this::serveHtml); this.server.createContext("/convert", this::serveConvertion); } } public void start() { System.out.println("Запускаем сервер на порту 8888"); System.out.println("Открой в браузере http://localhost:8888/"); this.server.start(); } protected void serveHtml(HttpExchange h) throws IOException { System.out.println("Serving html.."); try { String htmlContent = Files.readString(Path.of("C:/Users/Jony/IdeaProjects/java-diplom/assets/index.html")); String jsContent = Files.readString(Path.of("C:/Users/Jony/IdeaProjects/java-diplom/assets/my.js")); htmlContent = htmlContent.replace("{{{JS}}}", jsContent); byte[] htmlBytes = htmlContent.getBytes(StandardCharsets.UTF_8); h.sendResponseHeaders(200, (long) htmlBytes.length); h.getResponseBody().write(htmlBytes); System.out.println("HTML отправлен успешно!"); } catch (Exception e) { e.printStackTrace(); String error = "Ошибка: " + e.getMessage(); System.out.println(error); byte[] msgBytes = error.getBytes(StandardCharsets.UTF_8); h.sendResponseHeaders(500, msgBytes.length); h.getResponseBody().write(msgBytes); } finally { h.close(); } } protected void serveConvertion(HttpExchange h) throws IOException { System.out.println("Convert request.."); String url = new String(h.getRequestBody().readAllBytes(), StandardCharsets.UTF_8).trim(); if (url == null || url.isEmpty()) { byte[] msgBytes = "Не указан URL".getBytes(StandardCharsets.UTF_8); h.sendResponseHeaders(400, msgBytes.length); h.getResponseBody().write(msgBytes); h.close(); return; } try { System.out.println("Converting image: " + url); String converted = this.converter.convert(url); Files.writeString(Path.of("C:/Users/Jony/IdeaProjects/java-diplom/assets/img.txt"), converted); byte[] img = converted.getBytes(StandardCharsets.UTF_8); System.out.println("...converted!"); h.sendResponseHeaders(200, img.length); h.getResponseBody().write(img); } catch (Exception e) { e.printStackTrace(); String msg = e.getMessage(); if (msg == null || msg.isEmpty()) { msg = "Произошла ошибка конвертации :'("; } byte[] msgBytes = msg.getBytes(StandardCharsets.UTF_8); h.sendResponseHeaders(500, msgBytes.length); h.getResponseBody().write(msgBytes); } finally { h.close(); } } }