SpringBoot實(shí)現(xià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ù)器中對應(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;
? ? }
}這里只做了簡單的文件上傳,沒有限制文件類型。
前端寫法
這里分為兩種寫法,一種是常用的表單提交,另一種是當(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>以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)讀取及生成Excel文件的方法
這篇文章主要介紹了Java實(shí)現(xiàn)讀取及生成Excel文件的方法,結(jié)合實(shí)例形式分析了java通過引入第三方j(luò)ar包poi-3.0.1-FINAL-20070705.jar實(shí)現(xiàn)針對Excel文件的讀取及生成功能,需要的朋友可以參考下2017-12-12
MybatisPlus,無XML分分鐘實(shí)現(xiàn)CRUD操作
這篇文章主要介紹了MybatisPlus,無XML分分鐘實(shí)現(xiàn)CRUD操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Java8 Stream對兩個(gè) List 遍歷匹配數(shù)據(jù)的優(yōu)化處理操作
這篇文章主要介紹了Java8 Stream對兩個(gè) List 遍歷匹配數(shù)據(jù)的優(yōu)化處理操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Java實(shí)現(xiàn)Map集合二級(jí)聯(lián)動(dòng)示例
Java實(shí)現(xiàn)Map集合二級(jí)聯(lián)動(dòng)示例,需要的朋友可以參考下2014-03-03

