SpringBoot實(shí)現(xiàn)簡(jiǎn)單文件上傳功能
通過(guò) SpringBoot 實(shí)現(xiàn)了表單下的文件上傳,前后端分離情況下的文件上傳。本案例不連接數(shù)據(jù)庫(kù),只做基本的文件上傳操作。
在 SpringBoot 中不需要額外導(dǎo)入其他依賴(lài),正常引入即可。
后端 controller 的寫(xiě)法
package com.dailyblue.java.controller; ? import org.springframework.util.ResourceUtils; 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.RestController; import org.springframework.web.multipart.MultipartFile; ? import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; ? @RestController @RequestMapping("/upload") public class UploadController { ? ? ? @PostMapping ? ? public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception { ? ? ? ? // file:上傳文件 ? ? ? ? // 獲取到 images 的具體路徑 ? ? ? ? // String realPath = request.getRealPath("images"); ? ? ? ? String realPath = ResourceUtils.getURL("classpath:").getPath() + "/static/images"; ? ? ? ? System.out.println("上傳的文件地址是:" + realPath); ? ? ? ? // 服務(wù)器中對(duì)應(yīng)的位置 ? ? ? ? // 產(chǎn)生唯一的文件名稱(chēng) ? ? ? ? String fileName = UUID.getUUid(); ? ? ? ? // 獲取到文件后綴 ? ? ? ? String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); ? ? ? ? File src = new File(realPath, fileName + fileType); ? ? ? ? // 將file文件傳遞到src去 ? ? ? ? file.transferTo(src); ? ? ? ? return "images/" + fileName + fileType; ? ? } }
這里只做了簡(jiǎn)單的文件上傳,沒(méi)有限制文件類(lèi)型。
前端寫(xiě)法
這里分為兩種寫(xiě)法,一種是常用的表單提交,另一種是當(dāng)下較火的 Vue 上傳方式。
表單寫(xiě)法
<form action="upload" method="post" enctype="multipart/form-data"> ? ? <input type="file" name="file"/> ? ? <button>上傳</button> </form>
Vue 寫(xiě)法
<!DOCTYPE html> <html lang="en"> <head> ? ? <meta charset="UTF-8"> ? ? <title>Title</title> </head> <body> <div id="app"> ? ? <img :src="'http://localhost:8081/'+img" v-show="flag"/> ? ? <input type="file" @change="changeImg"/> ? ? <button @click="upload">Vue 上傳</button> </div> </body> </html> <script src="js/vue.min.js"></script> <script src="js/axios.min.js"></script> <script> ? ? new Vue({ ? ? ? ? el: '#app', ? ? ? ? data: { ? ? ? ? ? ? file: null, ? ? ? ? ? ? img: '', ? ? ? ? ? ? flag: false ? ? ? ? }, ? ? ? ? methods: { ? ? ? ? ? ? changeImg(event) { ? ? ? ? ? ? ? ? this.file = event.target.files[0]; ? ? ? ? ? ? }, ? ? ? ? ? ? upload() { ? ? ? ? ? ? ? ? // 表單數(shù)據(jù) ? ? ? ? ? ? ? ? let data = new FormData(); ? ? ? ? ? ? ? ? data.append('file', this.file); ? ? ? ? ? ? ? ? // 定義發(fā)送格式 ? ? ? ? ? ? ? ? let type = { ? ? ? ? ? ? ? ? ? ? headers: { ? ? ? ? ? ? ? ? ? ? ? ? 'Content-Type': 'multipart/form-data' ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? axios.post('http://localhost:8081/upload', data, type) ? ? ? ? ? ? ? ? ? ? .then((response) => { ? ? ? ? ? ? ? ? ? ? ? ? this.img = response.data; ? ? ? ? ? ? ? ? ? ? ? ? this.flag = true; ? ? ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? } ? ? ? ? } ? ? }); </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)讀取及生成Excel文件的方法
這篇文章主要介紹了Java實(shí)現(xiàn)讀取及生成Excel文件的方法,結(jié)合實(shí)例形式分析了java通過(guò)引入第三方j(luò)ar包poi-3.0.1-FINAL-20070705.jar實(shí)現(xiàn)針對(duì)Excel文件的讀取及生成功能,需要的朋友可以參考下2017-12-12MybatisPlus,無(wú)XML分分鐘實(shí)現(xiàn)CRUD操作
這篇文章主要介紹了MybatisPlus,無(wú)XML分分鐘實(shí)現(xiàn)CRUD操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08基于StringUtils工具類(lèi)的常用方法介紹(必看篇)
下面小編就為大家?guī)?lái)一篇基于StringUtils工具類(lèi)的常用方法介紹(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07Java8 Stream對(duì)兩個(gè) List 遍歷匹配數(shù)據(jù)的優(yōu)化處理操作
這篇文章主要介紹了Java8 Stream對(duì)兩個(gè) List 遍歷匹配數(shù)據(jù)的優(yōu)化處理操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Java實(shí)現(xiàn)Map集合二級(jí)聯(lián)動(dòng)示例
Java實(shí)現(xiàn)Map集合二級(jí)聯(lián)動(dòng)示例,需要的朋友可以參考下2014-03-03