SpringBoot實現(xiàn)單文件上傳
更新時間:2019年11月30日 07:41:09 作者:KevinYang-凱
這篇文章主要為大家詳細介紹了SpringBoot實現(xiàn)單文件上傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
SpringBoot實現(xiàn)單文件上傳功能,供大家參考,具體內(nèi)容如下
架構為springboot+thymeleaf,采用ajax方式提交
1. 頁面testFile.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>測試文件上傳</title>
<script src="../static/jquery/jquery-2.1.1.min.js" th:src="@{/jquery/jquery-2.1.1.min.js}"></script>
<script type="text/javascript">
$(function () {
$("#upload1").click(function () {
var formData = new FormData();
formData.append("file", document.getElementById("file").files[0]);
$.ajax({
url: "/file/upload1",
type: "POST",
data: formData,
//必須false才會自動加上正確的Content-Type
contentType: false,
//必須false才會避開jquery對 formdata 的默認處理
//XMLHttpRequest會對 formdata 進行正確的處理
processData: false,
success: function (data) {
if (data.status == "true") {
alert("上傳成功!");
}
if (data.status == "error") {
alert(data.msg);
}
},
error: function () {
alert("上傳失?。?);
}
});
});
});
</script>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="/file/upload1">
<fieldset>
<legend>單一文件上傳實例:</legend>
文件1:<input type="file" name="file" id="file"/><br/>
<input type="button" id="upload1" value="上傳"/><br/>
</fieldset>
</form>
</body>
</html>
2. FileController.java
package com.stormkai.controller;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import lombok.extern.slf4j.Slf4j;
@Controller
@RequestMapping("/file")
@Slf4j
public class FileController {
@GetMapping("/index")
public String index() {
return "testFile";
}
@PostMapping("/upload1")
@ResponseBody
public Map<String, Object> upload1(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
log.info("系統(tǒng)路徑={}",request.getSession().getServletContext().getRealPath(""));
String path = "F:\\uploadfile\\";
if(!new File(path).exists()){
new File(path).mkdirs();
}
file.transferTo(new File(path + file.getOriginalFilename()));
Map<String, Object> result = new HashMap<>();
result.put("status", "true");
result.put("data", null);
return result;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
WebSocket整合SSM(Spring,Struts2,Maven)的實現(xiàn)示例
這篇文章主要介紹了WebSocket整合SSM(Spring,Struts2,Maven)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01
Java大數(shù)據(jù)處理的核心技術MapReduce框架
MapReduce是一種分布式計算框架,適用于大規(guī)模的數(shù)據(jù)處理。它將大數(shù)據(jù)分成多個小數(shù)據(jù)塊,通過Map和Reduce兩個階段對數(shù)據(jù)進行處理和分析。MapReduce框架具有可靠、高效、可擴展等特點,已經(jīng)成為大數(shù)據(jù)處理的核心技術2023-05-05

