From 8eef40a88552d9df3fd5c433b94bb27e7a839327 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sun, 13 Mar 2022 14:47:49 +0400 Subject: [PATCH] #5 -- can upload file --- data/db.mv.db | Bin 36864 -> 61440 bytes .../ru/ulstu/controller/NewsController.java | 8 +++-- src/main/java/ru/ulstu/model/News.java | 23 ++++++++++++++ src/main/java/ru/ulstu/service/FileUtil.java | 29 ++++++++++++++++++ .../java/ru/ulstu/service/NewsService.java | 10 +++++- src/main/resources/application.properties | 3 +- src/main/resources/templates/editNews.html | 10 +++++- 7 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 src/main/java/ru/ulstu/service/FileUtil.java diff --git a/data/db.mv.db b/data/db.mv.db index c0b97943b57bc34165d26eb084f622a9a6e80f64..49a6a54388a3b0c978a5bd5d7c4a5f5f1c7f537f 100644 GIT binary patch literal 61440 zcmeG_ZEPIJbw`RPOOz;yvMpJ%q^!@DEhXpk?%sZ~3G5^9PCSVp5|5;%v}?< zBX!?=?am$Vj+8{jQarN3y_wyadGp?zH}l@SnKwS7#tWs2`<@YtJoEmqc_WG}%W?O7 zqx_x`*E8a7)pP1zaYQi}4aag8J;RPKR;tTReME_0@TxVxQpO)Hmb|)q-m8u{wxM}W zQ6G$me*yyn0|EmA0|EmA0|EmA0|EmA0|EmA0|EmA18+D6#)ba>hSNX*ATS^>ATS^> zATS^>ATS^>ATS^>ATS^>ATV&XF+ex@i;L6?K#iBY@<=ki?5vD97CasrNouwnFMAKw zM~d+kXUVI9dqBKesnkc3Nz(;S0oih0Eg7%-%iuX+>89Z-PUsc@Rj>Hvu}Y&{ACaZa z{Ri#^d)|BKz?LHsMNMkDVMez_w?_w~JEA+IL(yH)-O)YK;ppDzjnSK;H%IqH_eXDu z9*7={-Woj=Jsdp}86LPzy7O#4Gb@eFWKy%4h0(d(L|Rg1d3Ycu#fIMBNAjh)d}c<< zWu>X;0(p|QuS%#}yo}${ep%m_t##8xm=`PcBRnIaNWl^_P zMbR`(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V}AyJbPqKT(kJrsJ;sYJ1{B%j?*? z9jN7F9ir8e&C$Cg*zeS~3mxB+vs;%L-C6>6?B7FKdNu<~rxwf7`dKVZ&BxLCL%WT2EM7#aIdmOV4Fu=`q66hqGAv^V}?L%*fJqH%osYES;n* zo#gxs1Fqa*sjJPm392uKpzqq)vb)Tg)S9EApRd0pUG_Xpx9;Lb=JuX< z_xGv)s=v>OD7ziEFljwepyxC6M3E7S=oi1J&FTL{p*pY#3K1|e3Snhah$u!OtZWKV zMJYrSqYzb;LPRkNA?2nJ8Dtb9icyFvN+GOl3Snha2rK6I#8(+gsA3ed&ZHD_SQ5og zdW MAX_NEWS_TEXT_PREVIEW_LENGTH ? text.substring(0, MAX_NEWS_TEXT_PREVIEW_LENGTH) + "..." diff --git a/src/main/java/ru/ulstu/service/FileUtil.java b/src/main/java/ru/ulstu/service/FileUtil.java new file mode 100644 index 0000000..af9fb4a --- /dev/null +++ b/src/main/java/ru/ulstu/service/FileUtil.java @@ -0,0 +1,29 @@ +package ru.ulstu.service; + +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; + +public class FileUtil { + + public static void saveFile(String uploadDir, String fileName, + MultipartFile multipartFile) throws IOException { + Path uploadPath = Paths.get(uploadDir); + + if (!Files.exists(uploadPath)) { + Files.createDirectories(uploadPath); + } + + try (InputStream inputStream = multipartFile.getInputStream()) { + Path filePath = uploadPath.resolve(fileName); + Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException ioe) { + throw new IOException("Could not save image file: " + fileName, ioe); + } + } +} diff --git a/src/main/java/ru/ulstu/service/NewsService.java b/src/main/java/ru/ulstu/service/NewsService.java index f49dbdd..feac00a 100644 --- a/src/main/java/ru/ulstu/service/NewsService.java +++ b/src/main/java/ru/ulstu/service/NewsService.java @@ -7,6 +7,7 @@ import ru.ulstu.model.News; import ru.ulstu.repository.NewsRepository; import javax.validation.constraints.NotNull; +import java.io.IOException; import java.util.Date; import java.util.List; @@ -27,12 +28,19 @@ public class NewsService { newsRepository.save(news); } - public void save(News news) { + public void save(News news) throws IOException { + String fileName = System.currentTimeMillis() + ""; + news.setImageFileName(fileName); + if (news.getId() != null && (news.getId() != 0)) { newsRepository.save(news); } else { create(news); } + + String uploadDir = "news-photos/"; + FileUtil.saveFile(uploadDir, fileName, news.getImageFile()); + } public News getById(@NotNull Integer id) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 14058f2..89e854d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,11 +3,12 @@ # You may use, distribute and modify this code, please write to: romanov73@gmail.com. # # - spring.main.banner-mode=off logging.level.tech.athene=DEBUG server.port=8080 spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false +spring.servlet.multipart.max-file-size=100000000 +spring.servlet.multipart.max-request-size=100000000 # go to http://localhost:8080/h2-console spring.datasource.url=jdbc:h2:file:./data/db spring.datasource.driverClassName=org.h2.Driver diff --git a/src/main/resources/templates/editNews.html b/src/main/resources/templates/editNews.html index 71a8aad..d448036 100644 --- a/src/main/resources/templates/editNews.html +++ b/src/main/resources/templates/editNews.html @@ -10,7 +10,7 @@ layout:decorate="~{default}">

Редактирование новости:

-
+ @@ -27,6 +27,14 @@

Не может быть пустым

+
+ + + +

+ Ошибка

+
Отмена