赞
踩
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-core</artifactId> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!--整合mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--json @responseBody/@requestBody--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.54</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>上传图片展示区</title>
</head>
<body>
选择图片:
<form enctype="multipart/form-data" method="post" action="/doUpload">
<input type="file" name="file"/>
<input type="submit" value="上传">
</form>
<span th:text="${error}"></span>
</body>
</html>
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>上传成功</title> </head> <body> 上传成功 <form method="post" action="/showHead"> 你要查询的id <input name="id" type="text"> <button name="doShow" type="submit" value="查询">查询</button> </form> <div th:each="head:${byId}"> <span th:text="${head.getId()}"></span><br> <span th:text="${head.getHeadAddress()}"></span><br> <img th:src="@{'/picture/'+${head.getHeadName()}}"></img> </div> </body> </html>
@Configuration
public class ImgConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// addResourceHandler: ( 存储图片的虚拟路径,在 static 目录下的 picture 文件夹,用于存储上传图片)
// addResourceLocations: ( file: + 存储图片的路径)
registry.addResourceHandler("/picture/**").addResourceLocations("file:" + "C:/Users/Lzy/Desktop/demo/src/main/resources/static/picture/");
}
}
@Controller public class FileController { @Autowired private AddHeadAddress addHeadAddress; // 1.跳转到上传文件的页面 @GetMapping("/") public String goUploadImg(){ return "index"; } // 2.处理上传的文件 @PostMapping(value = "/doUpload") public String uploadImg( // 定义一个 MultipartFile 类型的参数 file @RequestParam("file") MultipartFile file, Model model){ // 如果图片为空,显示error if ((file.getOriginalFilename().isEmpty() )) { model.addAttribute("error","error"); return "index"; }else{ // 1. 创建一个 head 对象 Head head = new Head(); // 2. 获取图片的名字 String fileName = file.getOriginalFilename(); // 3. 防止图片名字相同而出现bug,使用 uuid 加密 String hToken = UUID.randomUUID().toString(); // 4. 加密后的图片名字:uuid + 图片名字 String HeadName = hToken + fileName; // 5. 图片存放的文件夹地址 String filePath = "C:\\Users\\Lzy\\Desktop\\demo\\src\\main\\resources\\static\\picture\\"; // 6. 图片的路径 = 文件夹地址 + 名字 String fileAddress = filePath + HeadName; try{ // 图片上传的工具类 // 参数一:图片的编码格式的字节数组 // 参数二:图片存放的文件夹地址 // 参数三:图片的名字(加密后) FileUtil.uploadFile(file.getBytes(),filePath,HeadName); // 把图片路径,图片名写入数据库 head.setHeadAddress(fileAddress); head.setHeadName(HeadName); addHeadAddress.insert(head); } catch (Exception e){ } return "success"; } } // 3.跳转到查询页面 @GetMapping("/showHead") public String toShow(){ return "success"; } // 4.通过id查询数据库,返回给前端页面 @PostMapping(value = "/showHead") public String showHead( @RequestParam("id") Integer id, Model model ){ // 通过 id 查询数据库中的数据,返回给前端 List<Head> byId = addHeadAddress.getById(id); model.addAttribute("byId",byId); return "success"; } }
@Mapper@Repository
public interface AddHeadAddress {
@Insert("insert into head(headAddress,headName) values (#{headAddress},#{headName})")
void insert(Head head);
@Select("SELECT * FROM head where (id) = (#{id})")
List<Head> getById(@Param("id") Integer id);
}
public class Head { public String headAddress; public Integer id; public String headName; public String getHeadName() { return headName; } public void setHeadName(String headName) { this.headName = headName; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getHeadAddress() { return headAddress; } public void setHeadAddress(String headAddress) { this.headAddress = headAddress; } }
public class FileUtil {
//上传文件的工具类
public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath+fileName);
out.write(file);
out.flush();
out.close();
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。