javascript
springboot 上传文件_基于SpringBoot的文件上传
在實際的企業開發中,文件上傳是最常見的功能之一,SpringBoot集成了SpringMVC常用的功能,當然也包含了文
件上傳的功能,實現起來沒有太多的區別。
下面我們來講解一下,使用SpringBoot如何實現多個文件上傳操作。使用的環境是IntelliJ IDE開發工具。
開發過程如下:
1.1 配置pom.xml文件
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">
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
com.qianfeng.springboot
fileupload
0.0.1-SNAPSHOT
fileupload
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-test
test
1.2 application.properties
1.3 Controller類
org.springframework.boot
spring-boot-maven-plugin
server.port=8080
package com.qianfeng.springboot.fileupload.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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
@Controller
public class UploadFileController {
@RequestMapping("/uploadfile")
public String uploadOneFile(){
return "uploadonefile"; // 最終由ThymeleafView處理,轉發 classpath:templates
}
@RequestMapping("/uploadfiles")
public String uploadMultiFile(){
return "uploadmultifile"; // 最終由ThymeleafView處理,轉發 classpath:templates
}
/**
* 上傳單個文件
* @param file
* @param request
* @return
*/
@RequestMapping(value = "/uploadsimplefile", method = RequestMethod.POST)
@ResponseBody
public String uploadSimpleFile(@RequestParam("attachment") MultipartFile file,
HttpServletRequest request) {
try {
String fileDir =
request.getSession().getServletContext().getRealPath("/upload/");
System.out.println("------->>"+fileDir);
File dir = new File(fileDir);
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = file.getOriginalFilename();
File upload_file = new File(fileDir + fileName);
file.transferTo(upload_file);
} catch (Exception e) {
e.printStackTrace();
return "上傳失敗";
}
return "上傳成功";
}
/**
* 上傳多個文件
* @param file
* @param request
* @return
*/
@RequestMapping(value = "/uploadmultifile", method = RequestMethod.POST)
@ResponseBody
public String uploadMultiFile(@RequestParam("attachment") MultipartFile[] file,
HttpServletRequest request) {
try {
String fileDir =
request.getSession().getServletContext().getRealPath("/upload/");
System.out.println("------->>"+fileDir);
File dir = new File(fileDir);
if (!dir.exists()) {
dir.mkdirs();
}
for (int i=0;i
if (file[i]!=null){
uploadFile(fileDir,file[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
return "上傳失敗";
}
return "上傳成功";
}
1.4 單個文件上傳的頁面
/**
*
* @param uploadDir
* @param file
* @throws IOException
*/
public void uploadFile(String uploadDir,MultipartFile file) throws IOException{
String file_name = file.getOriginalFilename();
File upload_file = new File(uploadDir+file_name);
file.transferTo(upload_file);
}
}
文件上傳單個文件上傳
th:action="@{/uploadsimplefile}" role="form">
文件上傳:
id="attachment" placeholder="請選擇郵件附件"/>
上 傳
1.5 上傳多個文件頁面
文件上傳單個文件上傳
th:action="@{/uploadmultifile}" role="form">
文件上傳:
id="attachment1" placeholder="請選擇郵件附件"/>
文件上傳:
id="attachment2" placeholder="請選擇郵件附件"/>
文件上傳:
id="attachment3" placeholder="請選擇郵件附件"/>
1.6 測試
1.6.1 單文件上傳
啟動項目后,在瀏覽器中輸入:http://localhost:8080/uploadfile,如下所示:
選擇單個文件上傳。
1.6.2 多文件上傳
在瀏覽器中輸入:http://localhost:8080/uploadfiles,如下所示:
選擇多個文件上。
總結
以上是生活随笔為你收集整理的springboot 上传文件_基于SpringBoot的文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言sort函数_C语言的那些经典程序
- 下一篇: Struts2_1_基础案例_配置文件详