版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/catoop/article/details/61415169
使用SpringBoot進行檔案上傳的方法和SpringMVC差不多,本文單獨建立一個最簡單的DEMO來說明一下。
主要步驟包括:
1、建立一個springboot項目工程,本例名稱(demo-uploadfile)。
2、配置 pom.xml 依賴。
3、建立和編寫檔案上傳的 Controller(包含單檔案上傳和多檔案上傳)。
4、建立和編寫檔案上傳的 HTML 測試頁面。
5、檔案上傳相關限制的配置(可選)。
6、運作測試。
項目工程截圖如下:
檔案代碼:
<dependencies>
<!-- spring boot web支援 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thmleaf模闆依賴. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
package com.example.controller;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
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 org.springframework.web.multipart.MultipartHttpServletRequest;
/**
* 檔案上傳的Controller
*
* @author 單紅宇(CSDN CATOOP)
* @create 2017年3月11日
*/
@Controller
public class FileUploadController {
// 通路路徑為:http://ip:port/upload
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public String upload() {
return "/fileupload";
}
// 通路路徑為:http://ip:port/upload/batch
@RequestMapping(value = "/upload/batch", method = RequestMethod.GET)
public String batchUpload() {
return "/mutifileupload";
}
/**
* 檔案上傳具體實作方法(單檔案上傳)
*
* @param file
* @return
*
* @author 單紅宇(CSDN CATOOP)
* @create 2017年3月11日
*/
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
try {
// 這裡隻是簡單例子,檔案直接輸出到項目路徑下。
// 實際項目中,檔案需要輸出到指定位置,需要在增加代碼處理。
// 還有關于檔案格式限制、檔案大小限制,詳見:中配置。
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(file.getOriginalFilename())));
out.write(file.getBytes());
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return "上傳失敗," + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "上傳失敗," + e.getMessage();
}
return "上傳成功";
} else {
return "上傳失敗,因為檔案是空的.";
}
}
/**
* 多檔案上傳 主要是使用了MultipartHttpServletRequest和MultipartFile
*
* @param request
* @return
*
* @author 單紅宇(CSDN CATOOP)
* @create 2017年3月11日
*/
@RequestMapping(value = "/upload/batch", method = RequestMethod.POST)
public @ResponseBody String batchUpload(HttpServletRequest request) {
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i = 0; i < files.size(); ++i) {
file = files.get(i);
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
stream = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
stream.write(bytes);
stream.close();
} catch (Exception e) {
stream = null;
return "You failed to upload " + i + " => " + e.getMessage();
}
} else {
return "You failed to upload " + i + " because the file was empty.";
}
}
return "upload successful";
}
}
package com.example.configuration;
import javax.servlet.MultipartConfigElement;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
/**
* 檔案上傳配置
*
* @author 單紅宇(CSDN CATOOP)
* @create 2017年3月11日
*/
public class FileUploadConfiguration {
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
// 設定檔案大小限制 ,超出設定頁面會抛出異常資訊,
// 這樣在檔案上傳的地方就需要進行異常資訊的處理了;
factory.setMaxFileSize("256KB"); // KB,MB
/// 設定總上傳資料總大小
factory.setMaxRequestSize("512KB");
// Sets the directory location where files will be stored.
// factory.setLocation("路徑位址");
return factory.createMultipartConfig();
}
}
@SpringBootApplication
public class DemoUploadfileApplication {
public static void main(String[] args) {
SpringApplication.run(DemoUploadfileApplication.class, args);
}
}
<!DOCTYPE html>
<html>
<head>
<title>檔案上傳示例</title>
</head>
<body>
<h2>檔案上傳示例</h2>
<hr/>
<form method="POST" enctype="multipart/form-data" action="/upload">
<p>
檔案:<input type="file" name="file" />
</p>
<p>
<input type="submit" value="上傳" />
</p>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>批量檔案上傳示例</title>
</head>
<body>
<h2>批量檔案上傳示例</h2>
<hr/>
<form method="POST" enctype="multipart/form-data"
action="/upload/batch">
<p>
檔案1:<input type="file" name="file" />
</p>
<p>
檔案2:<input type="file" name="file" />
</p>
<p>
檔案3:<input type="file" name="file" />
</p>
<p>
<input type="submit" value="上傳" />
</p>
</form>
</body>
</html>
最後啟動服務,通路
http://localhost:8080/upload和
http://localhost:8080/upload/batch測試檔案上傳。
Demo源代碼下載下傳位址:
http://download.csdn.net/detail/catoop/9777970