Spring Boot是由Pivotal團隊供給的全新框架,其設計目標是用來簡化新Spring應用的初始搭建以及開辟過程。該框架利用了特心猿意馬的體例來進行設置裝備擺設,從而使開辟人員不再需要界說樣板化的設置裝備擺設。經由過程這種體例,Spring Boot致力于在蓬勃成長的快速應用開辟范疇(rapid application development)當作為帶領者。
第一步:搭建springboot開辟情況。
1、file --》new --》 project.. 打開建立選擇項目類型框
2、選擇建立springboot類型的項目--》進入項目建立框 “upload”項目名稱不克不及大寫會報錯。
3、選擇依靠web下一步完當作。
第二步:在pom.xm文件中添加HTML視圖解析依靠。
<!--添加HTML視圖解析依靠--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
第三步:pom.xm確認。
<modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>upload</artifactId> <version>0.0.1-SNAPSHOT</version> <name>upload</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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--添加HTML視圖解析依靠--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
第四步:編寫代碼。
1、前端代碼
<!DOCTYPE html>
<html>
<head>
<title>圖片上傳</title>
<meta name="keywords" content="keyword1,keyword2,keyword3"></meta>
<meta name="description" content="this is my page"></meta>
<meta name="content-type" content="text/html; charset=UTF-8"></meta>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/testUploadimg">
圖片:<input type="file" name="file" /><br/>
<input type="submit" value="上傳" />.
</form>
</body>
</html>
第五步:節制器UploadController實現。
UploadController 本家兒要分為3部門
2.1 調整頁面請求goUploadImg
2.2 上傳請求方式uploadImg
2.3 存儲圖片方式uploadFile
@Controllerpublic class UploadController {
//跳轉到上傳文件的頁面
@RequestMapping(value = "/gouploadimg", method = RequestMethod.GET)
public String goUploadImg() {
//跳轉到 templates 目次下的 uploadimg.html
return "uploadimg";
}
//處置文件上傳
@ResponseBody //返回json數據
@RequestMapping(value = "/testUploadimg", method = RequestMethod.POST)
public String uploadImg(@RequestParam("file") MultipartFile file,
HttpServletRequest request) {
tring contentType = file.getContentType();
String fileName = file.getOriginalFilename();
String filePath = "D:/img";
if (file.isEmpty()) {
return "文件為空!";
}
try {
uploadFile(file.getBytes(), filePath, fileName);
} catch (Exception e) {
// TODO: handle exception
}
//返回json
return "上傳當作功";
}
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();
}
}
第六步:測試。
打開頁面地址如下圖所示:
0 篇文章
如果覺得我的文章對您有用,請隨意打賞。你的支持將鼓勵我繼續創作!