使用SpringBoot實現文件的上傳html
springboot能夠直接使用 org.springframework.web.multipart.MultipartFilejava
因此很是容易實現web
1、首先是簡單的單文件上傳spring
先在index.html頁面下寫一個簡單的form表單springboot
<h1>單文件</h1>
<form class="form-signin" th:action="@{/SingleFile/upload}" method="post" enctype="multipart/form-data">
<p><input type="file" name="myfile"/></p>
<p><input type="submit" value="上傳"/></p>
<p style="color: red" th:text="${result_singlefile}" th:if="${not #strings.isEmpty(result_singlefile)}"></p>
</form>
注意使用thymeleafapp
而後就到controller中寫實現的代碼post
package com.manager.controller.FileController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;
@Controller
public class FileUploadController {
@PostMapping("/SingleFile/upload")
public String SingleFileUpLoad(@RequestParam("myfile") MultipartFile file, Model model) {
//判斷文件是否爲空
if(file.isEmpty()){
model.addAttribute("result_singlefile", "文件爲空");
return "index";
}
//建立輸入輸出流
InputStream inputStream = null;
OutputStream outputStream = null;
try {
//指定上傳的位置爲 d:/upload/
String path = "d:/upload/";
//獲取文件的輸入流
inputStream = file.getInputStream();
//獲取上傳時的文件名
String fileName = file.getOriginalFilename();
//注意是路徑+文件名
File targetFile = new File(path + fileName);
//若是以前的 String path = "d:/upload/" 沒有在最後加 / ,那就要在 path 後面 + "/"
//判斷文件父目錄是否存在
if(!targetFile.getParentFile().exists()){
//不存在就建立一個
targetFile.getParentFile().mkdir();
}
//獲取文件的輸出流
outputStream = new FileOutputStream(targetFile);
//最後使用資源訪問器FileCopyUtils的copy方法拷貝文件
FileCopyUtils.copy(inputStream, outputStream);
/*參數是經過源碼
public static int copy(InputStream in, OutputStream out) throws IOException {
......
}
而得知的*/
//告訴頁面上傳成功了
model.addAttribute("result_singlefile", "上傳成功");
} catch (IOException e) {
e.printStackTrace();
//出現異常,則告訴頁面失敗
model.addAttribute("result_singlefile", "上傳失敗");
} finally {
//不管成功與否,都有關閉輸入輸出流
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "index";
}
步驟已在註釋中說明,如今運行項目測試測試
選擇文件 1.pngui
點擊上傳spa
成功!
2、多文件上傳
與單文件相似,注意先遍歷再執行
首先仍是index.html
<h1>多文件</h1>
<form class="form-signin" th:action="@{/MultiFile/upload}" method="post" enctype="multipart/form-data">
<p><input type="file" name="myfile"/></p>
<p><input type="file" name="myfile"/></p>
<p><input type="file" name="myfile"/></p>
<p><input type="submit" value="上傳"/></p>
<p style="color: red" th:text="${result_multifile}" th:if="${not #strings.isEmpty(result_multifile)}"></p>
</form>
再在剛纔的controller中配置
@PostMapping("/MultiFile/upload")
public String MultiFileUpload(Model model, HttpServletRequest request) {
List<MultipartFile> list_files=((MultipartHttpServletRequest)request).getFiles("myfile");
if(list_files.isEmpty()){
model.addAttribute("result_multifile", "文件爲空");
return "index";
}
InputStream inputStream = null;
OutputStream outputStream = null;
String path = "d:/upload/";
for (MultipartFile file : list_files) {
try {
inputStream = file.getInputStream();
String fileName = file.getOriginalFilename();
File targetFile = new File(path + fileName);
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdir();
}
outputStream = new FileOutputStream(targetFile);
FileCopyUtils.copy(inputStream, outputStream);
model.addAttribute("result_multifile", "上傳成功");
} catch (IOException e) {
e.printStackTrace();
model.addAttribute("result_multifile", "上傳失敗");
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "index";
}
運行項目測試
選擇1.png a.txt b.txt
成功!
以上就是簡單的文件上傳案例,由於只是簡單的實現,因此沒有將重複代碼整合到utils下,好比關閉流的操做