SpringBoot實(shí)現(xiàn)簡(jiǎn)單文件上傳功能
通過 SpringBoot 實(shí)現(xiàn)了表單下的文件上傳,前后端分離情況下的文件上傳。本案例不連接數(shù)據(jù)庫,只做基本的文件上傳操作。
在 SpringBoot 中不需要額外導(dǎo)入其他依賴,正常引入即可。
后端 controller 的寫法
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)生唯一的文件名稱
? ? ? ? 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)單的文件上傳,沒有限制文件類型。
前端寫法
這里分為兩種寫法,一種是常用的表單提交,另一種是當(dāng)下較火的 Vue 上傳方式。
表單寫法
<form action="upload" method="post" enctype="multipart/form-data"> ? ? <input type="file" name="file"/> ? ? <button>上傳</button> </form>
Vue 寫法
<!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)文章
Spring中ResponseBodyAdvice的使用詳解
這篇文章主要介紹了Spring中ResponseBodyAdvice的使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10
SpringMVC多個(gè)文件上傳及上傳后立即顯示圖片功能
這篇文章主要介紹了SpringMVC多個(gè)文件上傳及上傳后立即顯示圖片功能,非常不錯(cuò),具有參考借鑒價(jià)值功能,需要的朋友可以參考下2017-10-10
深入了解JVM(Java虛擬機(jī))內(nèi)存結(jié)構(gòu)
Java虛擬機(jī)(Java Virtual Machine,JVM)是Java程序的運(yùn)行環(huán)境,它是一個(gè)抽象的計(jì)算機(jī)模型,通過解釋和執(zhí)行Java字節(jié)碼來運(yùn)行Java程序,本將大家深入了解JVM(Java虛擬機(jī))內(nèi)存結(jié)構(gòu),需要的朋友可以參考下2023-08-08
SpringBoot整合SpringSecurity認(rèn)證與授權(quán)
在項(xiàng)目開發(fā)中,權(quán)限認(rèn)證是很重要的,尤其是一些管理類的系統(tǒng),對(duì)于權(quán)限要求更為嚴(yán)格,本文主要介紹了SpringBoot整合SpringSecurity認(rèn)證與授權(quán),感興趣的可以了解一下2023-11-11
mybatis?一對(duì)多映射?column屬性的注意事項(xiàng)說明
這篇文章主要介紹了mybatis?一對(duì)多映射?column屬性的注意事項(xiàng)說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01
spring boot優(yōu)雅集成redisson詳解
這篇文章主要為大家介紹了spring boot優(yōu)雅集成redisson詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Java實(shí)現(xiàn)的具有GUI的校園導(dǎo)航系統(tǒng)的完整代碼
這篇文章主要介紹了Java實(shí)現(xiàn)的具有GUI的校園導(dǎo)航系統(tǒng)的完整代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04

