发表于: 2025-07-01 20:27:09
0 1
今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)
package org.example.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
@RestController
@RequestMapping("/api/images")
public class ImageController {
// @Value("${image.upload.dir}") // 在application.properties中配置
// private String uploadDir;
private static final String UPLOADDIR="C:\\data\\images";
// 上传图片
@PostMapping("/upload")
public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return ResponseEntity.badRequest().body("请选择要上传的文件");
}
try {
// 确保上传目录存在
File dir = new File(UPLOADDIR);
if (!dir.exists()) {
dir.mkdirs();
}
// 生成唯一文件名
String originalFilename = file.getOriginalFilename();
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
String newFilename = UUID.randomUUID().toString() + fileExtension;
// 保存文件
Path path = Paths.get(UPLOADDIR, newFilename);
Files.write(path, file.getBytes());
return ResponseEntity.ok(newFilename); // 返回文件名用于后续访问
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.internalServerError().body("上传失败: " + e.getMessage());
}
}
// 获取图片信息(可选)
@GetMapping("/info/{filename}")
public ResponseEntity<?> getImageInfo(@PathVariable String filename) {
File file = new File(UPLOADDIR, filename);
if (!file.exists()) {
return ResponseEntity.notFound().build();
}
// 这里可以返回图片的元信息,如大小、创建时间等
return ResponseEntity.ok().build();
}
// 注意:我们不提供直接的文件下载接口,因为将通过Nginx直接访问
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>User Management System</display-name>
<!-- 全局 Spring 上下文配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<!-- Spring 容器初始化监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC 核心控制器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<!-- 添加 multipart 配置 -->
<multipart-config>
<location>/tmp</location> <!-- 临时文件目录 -->
<max-file-size>104857600</max-file-size> <!-- 单个文件最大100MB -->
<max-request-size>209715200</max-request-size> <!-- 总请求最大200MB -->
<file-size-threshold>5242880</file-size-threshold> <!-- 超过5MB写入磁盘 -->
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字符编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
明天计划的事情:(一定要写非常细致的内容)
遇到的问题:(遇到什么困难,怎么解决的)
收获:(通过今天的学习,学到了什么知识)
评论