1. 簡單文件上傳實現
1. 部分項目目錄結構
1. 配置文件上傳路徑
-
application.properties 文件中添加如下配置
web.upload-file-path = D:/Workspaces/SpringBootWorkspaces/upload-file-test/ spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-file-path}
2. 添加文件上傳配置(此處爲SdSpringBootApplication)
// 需要將其配置到有@Configuration註解的類中
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
// 單個文件最大
factory.setMaxFileSize("201400KB"); // KB, MB
// 設置上傳數據總大小
factory.setMaxRequestSize("2014000KB");
return factory.createMultipartConfig();
}
3. 添加封裝Json數據的實體類(此處爲JsonData.java)
package com.test.spring_boot_demo.domain;
public class JsonData {
private int code;
private String msg;
private String data;
public JsonData(int code, String data) {
this.code = code;
this.data = data;
}
public JsonData(int code, String msg, String data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
4. 添加文件上傳接口(此處爲FileController中)
@RestController
public class FileController {
private static final String filePath = "D:/Workspaces/SpringBootWorkspaces/upload-file-test/";
@PostMapping("/file/upload")
public Object fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
// 其他參數
String name = request.getParameter("name");
System.out.println("用戶名:" + name);
// 獲取文件名
String fileName = file.getOriginalFilename();
System.out.println("上傳的文件名爲:" + fileName);
// 獲取文件的後綴,比如圖片的jpeg,png
String suffixName = fileName.substring(fileName.lastIndexOf("."));
System.out.println("上傳文件的後綴名爲:" + suffixName);
// 文件上傳後的路徑
String newFileName = UUID.randomUUID() + suffixName;
System.out.println("轉換後的文件名:" + newFileName);
File dest = new File(filePath + newFileName);
try {
file.transferTo(dest);
return new JsonData(1, newFileName);
} catch (IOException e) {
e.printStackTrace();
}
return new JsonData(0, "fail to save", null);
}
}
5. 添加靜態頁面以及css文件
-
file_upload_test.html
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>Title</title> <meta name="Keywords" content=""> <meta name="sdescription" content=""> <link rel="stylesheet" type="text/css" href="/css/main.css"> <style type="text/css"> * {margin: 0; padding: 0;} a {text-decoration: none; color: #111;} li {list-style: none;} body {background: #FFF; font-size: 14px; font-family: "微軟雅黑"; color: #666;} </style> </head> <body> <form class="file-form" enctype="multipart/form-data" method="post" action="/file/upload"> <input type="file" name="file" /> <br /> <input type="text" name="name" /> <br /> <button type="submit">提交</button> </form> </body> </html>
-
main.css
.file-form {width: 400px; border: 1px solid #550044; text-align: center; margin: 100px auto; padding: 50px 30px;} input {margin: 10px; width: 200px; height: 30px;} button {background: #2894FF; color: #111; width: 204px; height: 30px; border: 2px solid #2894FF;}
- 此處html、css文件都放在static目錄下,已經被加載。
- 此處css文件僅僅是爲了驗證可被html引用,不做專門的效果。
2. jar包方式運行項目
1. 添加Maven依賴(pom.xml)
<!--
打包成jar包,需要添加該maven依賴
如果沒有添加相關依賴,執行maven打包,運行後會報錯:no main manifest attribute, in XXX.jar
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2. 打包jar包
- Eclipse:右鍵點擊pom.xml,選擇 run as -> maven install
- IDEA:選擇 View -> Tool Windows -> Maven Projects -> 項目 -> Lifecycle -> install,然後點擊Maven Projects窗體上方的運行按鈕
3. 運行項目jar包(命令行命令)
> java -jar xxx.jar
4. 測試
-
訪問localhost:8080/html/file_upload_test.html
-
添加文件上傳,假如上傳成功,會返回新的文件名字
-
使用 localhost:8080/新文件名 訪問圖片