SpringBoot實(shí)現(xiàn)單文件上傳
更新時間:2019年11月30日 07:41:09 作者:KevinYang-凱
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)單文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
SpringBoot實(shí)現(xiàn)單文件上傳功能,供大家參考,具體內(nèi)容如下
架構(gòu)為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 的默認(rèn)處理 //XMLHttpRequest會對 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; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 詳解SpringBoot下文件上傳與下載的實(shí)現(xiàn)
- SpringBoot 文件上傳和下載的實(shí)現(xiàn)源碼
- SpringBoot后臺實(shí)現(xiàn)文件上傳下載
- springboot 中文件上傳下載實(shí)例代碼
- SpringBoot實(shí)現(xiàn)文件上傳下載功能小結(jié)
- springboot實(shí)現(xiàn)文件上傳和下載功能
- 詳解SpringBoot文件上傳下載和多文件上傳(圖文)
- SpringBoot實(shí)現(xiàn)Excel文件批量上傳導(dǎo)入數(shù)據(jù)庫
- springboot實(shí)現(xiàn)單文件和多文件上傳
- springboot整合vue實(shí)現(xiàn)上傳下載文件
相關(guān)文章
MyBatis-Plus自動填充功能失效導(dǎo)致的原因及解決
這篇文章主要介紹了MyBatis-Plus自動填充功能失效導(dǎo)致的原因及解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02WebSocket整合SSM(Spring,Struts2,Maven)的實(shí)現(xiàn)示例
這篇文章主要介紹了WebSocket整合SSM(Spring,Struts2,Maven)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01Java判斷101-200之間有多少個素?cái)?shù)并輸出
這篇文章主要介紹了Java判斷101-200之間有多少個素?cái)?shù)并輸出,需要的朋友可以參考下2017-02-02Java大數(shù)據(jù)處理的核心技術(shù)MapReduce框架
MapReduce是一種分布式計(jì)算框架,適用于大規(guī)模的數(shù)據(jù)處理。它將大數(shù)據(jù)分成多個小數(shù)據(jù)塊,通過Map和Reduce兩個階段對數(shù)據(jù)進(jìn)行處理和分析。MapReduce框架具有可靠、高效、可擴(kuò)展等特點(diǎn),已經(jīng)成為大數(shù)據(jù)處理的核心技術(shù)2023-05-05