整合作业

This commit is contained in:
2026-11-03 09:53:11 +08:00
parent b756b46ca3
commit 059295ef61
5 changed files with 69 additions and 4 deletions
@@ -12,6 +12,7 @@ import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.sql.DataSource;
@@ -54,4 +55,10 @@ public class WebAppConfig implements WebMvcConfigurer {
.allowCredentials(true)
.maxAge(3600);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
}
}
@@ -8,8 +8,15 @@ import com.ycjy.mapper.ProductMapper;
import com.ycjy.service.ProductService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
@RestController
@@ -45,4 +52,50 @@ public class ProductController {
public Result<java.util.List<com.ycjy.dao.DeleteLog>> logs() {
return Result.success(logMapper.getAll());
}
/**
* 下载商品图片
* GET /api/product/{id}/download
*/
@GetMapping("/product/{id}/download")
public ResponseEntity<byte[]> download(@PathVariable("id") Integer id,
@RequestHeader(value = "Authorization", required = false) String auth) {
// 1. 校验登录
if (auth == null || !auth.startsWith("Bearer ")) {
return ResponseEntity.status(401).build();
}
Claims claims = JwtUtil.parse(auth.substring(7));
if (claims == null) {
return ResponseEntity.status(401).build();
}
// 2. 查商品
Product product = productService.getById(id);
if (product == null || product.getImage() == null) {
return ResponseEntity.notFound().build();
}
// 3. 从 classpath 读文件
// product.getImage() 形如 "/a.png",去掉开头的 "/" 变成 "a.png"
String fileName = product.getImage().replaceFirst("^/", "");
ClassPathResource resource = new ClassPathResource("static/" + fileName);
if (!resource.exists()) {
return ResponseEntity.notFound().build();
}
try (InputStream in = resource.getInputStream()) {
byte[] bytes = in.readAllBytes();
// 4. 告诉浏览器"这是附件,请下载"
String downloadName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", downloadName);
headers.setContentLength(bytes.length);
return ResponseEntity.ok().headers(headers).body(bytes);
} catch (Exception e) {
return ResponseEntity.status(500).build();
}
}
}
@@ -27,4 +27,8 @@ public class ProductService {
logMapper.insert(username, String.valueOf(id));
return true;
}
public Product getById(Integer id) {
return getProdect.getById(id);
}
}
+5 -4
View File
@@ -30,11 +30,12 @@ create table demo_product
-- 模拟商品数据
INSERT INTO demo_product (name, p_desc, price, image)
VALUES ('iPhone 15', 'Apple最新款智能手机,A16芯片', 5999.00, 'https://example.com/iphone15.jpg'),
('华为 Mate 60', '华为旗舰手机,卫星通信', 4999.00, 'https://example.com/mate60.jpg'),
('MacBook Air', '轻薄便携笔记本电脑,M2芯片', 7999.00, 'https://example.com/macbook.jpg'),
('索尼降噪耳机', '主动降噪,30小时续航', 299.00, 'https://example.com/earphone.jpg');
VALUES ('iPhone 15', 'Apple最新款智能手机,A16芯片', 5999.00, '/a.png'),
('华为 Mate 60', '华为旗舰手机,卫星通信', 4999.00, '/a.png'),
('MacBook Air', '轻薄便携笔记本电脑,M2芯片', 7999.00, '/a.png'),
('索尼降噪耳机', '主动降噪,30小时续航', 299.00, '/a.png');
UPDATE demo_product SET image = '/a.png';
-- 日志表
create table demo_log
(
Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB