|
|
@@ -5,6 +5,7 @@ import cn.reghao.bnt.web.admin.service.SiteOptionService;
|
|
|
import cn.reghao.bnt.web.blog.hibernate.HibernateQuery;
|
|
|
import cn.reghao.bnt.web.blog.model.po.AboutView;
|
|
|
import cn.reghao.bnt.web.blog.model.vo.*;
|
|
|
+import cn.reghao.bnt.web.util.JarFileResources;
|
|
|
import cn.reghao.jutil.web.ServletUtil;
|
|
|
import cn.reghao.bnt.web.blog.service.ArticleQuery;
|
|
|
import cn.reghao.bnt.web.blog.model.CategoryType;
|
|
|
@@ -17,8 +18,6 @@ import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
@@ -30,12 +29,9 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
import java.net.URLDecoder;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
@@ -50,16 +46,19 @@ public class ForegroundController extends BaseController {
|
|
|
private final ArticleViewService articleViewService;
|
|
|
private final SiteOptionService siteOptionService;
|
|
|
private final FileService fileService;
|
|
|
+ private final JarFileResources jarFileResources;
|
|
|
|
|
|
public ForegroundController(CategoryService categoryService, ArticleQuery articleQuery,
|
|
|
HibernateQuery hibernateQuery, ArticleViewService articleViewService,
|
|
|
- SiteOptionService siteOptionService, FileService fileService) {
|
|
|
+ SiteOptionService siteOptionService, FileService fileService,
|
|
|
+ JarFileResources jarFileResources) {
|
|
|
this.categoryService = categoryService;
|
|
|
this.articleQuery = articleQuery;
|
|
|
this.hibernateQuery = hibernateQuery;
|
|
|
this.articleViewService = articleViewService;
|
|
|
this.siteOptionService = siteOptionService;
|
|
|
this.fileService = fileService;
|
|
|
+ this.jarFileResources = jarFileResources;
|
|
|
}
|
|
|
|
|
|
@Operation(summary = "前台首页", description = "N")
|
|
|
@@ -208,28 +207,13 @@ public class ForegroundController extends BaseController {
|
|
|
|
|
|
@Operation(summary = "默认头像", description = "N")
|
|
|
@GetMapping(value = "/avatar.jpg", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
- @Deprecated
|
|
|
public ResponseEntity<byte[]> getAvatar() throws IOException {
|
|
|
String avatarResource = "static/dist/images/avatar.jpg";
|
|
|
- byte[] imageBytes = getResourceFromJar(avatarResource);
|
|
|
-
|
|
|
- /*URL resource = Thread.currentThread().getContextClassLoader().getResource(avatarResource);
|
|
|
- String avatarPath = resource.getPath();
|
|
|
- System.out.println(avatarPath);
|
|
|
- File file = new File(avatarPath);
|
|
|
- byte[] imageBytes = Files.readAllBytes(file.toPath());*/
|
|
|
-
|
|
|
- // 构建 HTTP 响应头
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.setContentType(MediaType.IMAGE_JPEG);
|
|
|
- headers.setContentLength(imageBytes.length);
|
|
|
-
|
|
|
+ byte[] imageBytes = jarFileResources.getFileFromResourceAsStream(avatarResource).readAllBytes();
|
|
|
// 返回包含图片字节数组的 ResponseEntity
|
|
|
- return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
|
|
|
- }
|
|
|
-
|
|
|
- private byte[] getResourceFromJar(String resourcePath) throws IOException {
|
|
|
- InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(resourcePath);
|
|
|
- return resourceAsStream.readAllBytes();
|
|
|
+ return ResponseEntity.ok()
|
|
|
+ .contentType(MediaType.IMAGE_JPEG)
|
|
|
+ .contentLength(imageBytes.length)
|
|
|
+ .body(imageBytes);
|
|
|
}
|
|
|
}
|