這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)使用SpringBoot怎么實(shí)現(xiàn)一個(gè)單文件上傳功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)建站于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元蘆山做網(wǎng)站,已為上家服務(wù),為蘆山各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
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才會(huì)自動(dòng)加上正確的Content-Type
contentType: false,
//必須false才會(huì)避開jquery對(duì) formdata 的默認(rèn)處理
//XMLHttpRequest會(huì)對(duì) formdata 進(jìn)行正確的處理
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>單一文件上傳實(shí)例:</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;
}
}上述就是小編為大家分享的使用SpringBoot怎么實(shí)現(xiàn)一個(gè)單文件上傳功能了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前文章:使用SpringBoot怎么實(shí)現(xiàn)一個(gè)單文件上傳功能
新聞來源:http://www.chinadenli.net/article22/pecgjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站內(nèi)鏈、電子商務(wù)、外貿(mào)建站、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)