圖片上傳:
我們在開發WEB項目時必然要上傳文件,所以我們以上傳圖片爲例,做一次圖片上傳。上傳圖片有兩種方式:1. 放到tomcat的臨時目錄下,重啓一次就沒有了。還有一個就是上傳到絕對目錄也就是本地計算機的存儲目錄,後期無論怎麼重啓都不會丟失。
上傳到tomcat->temp:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
# #上傳文件大小
#單個
spring.http.multipart.max-file-size=500Mb
#全部
spring.http.multipart.max-request-size=500Mb
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="/upload">
文件
<input type="file" name="file">
<input type="submit" value="上傳">
</form>
</body>
</html>
package cn.org.easycoding.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;
/**
* Created by Guanzhong Hu
* Date :2020/2/8
* Description : 設置控制器
* Version :1.0
*/
@Controller
public class SettingController {
@ResponseBody
@RequestMapping(value = "/upload" ,method = RequestMethod.POST)
public String uploadfile(MultipartFile file, HttpServletRequest request){
try {
// 創建文件在服務器端存放路徑
String dir = request.getServletContext().getRealPath("/upload");
// 如果沒有文件則創建
File fileDir = new File(dir);
if (!fileDir.exists()) fileDir.mkdirs();
System.out.println(dir);
//原始名
String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
String fileName = UUID.randomUUID().toString()+fileSuffix;
File files = new File(fileDir+"/"+fileName);
file.transferTo(files);
}catch (Exception e){
return "上傳失敗";
}
return "上傳成功!";
}
}
上傳本地目錄:
package cn.org.easycoding.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;
/**
* Created by Guanzhong Hu
* Date :2020/2/8
* Description : 設置控制器
* Version :1.0
*/
@Controller
public class SettingController {
@ResponseBody
@RequestMapping(value = "/upload" ,method = RequestMethod.POST)
public String uploadfile(MultipartFile file, HttpServletRequest request){
try {
// 創建文件在服務器端存放路徑
String dir = "d://imgs/";
// 如果沒有文件則創建
File fileDir = new File(dir);
if (!fileDir.exists()) fileDir.mkdirs();
System.out.println(dir);
//原始名
String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
String fileName = UUID.randomUUID().toString()+fileSuffix;
File files = new File(fileDir+"/"+fileName);
file.transferTo(files);
}catch (Exception e){
return "上傳失敗";
}
return "上傳成功!";
}
}
我們使用UUID對圖片名進行重命名,避免文件夾重複名導致xxx.png後加(1)最終導致圖片找不到。 因爲Springboot已經幫我們集成好了基礎的依賴包,裏面包含了圖片上傳,所以我們只需要依賴一條就OK!