SpringBoot实现简单文件上传功能
  9m65el8SCpbP 2023年11月30日 33 0

通过 SpringBoot 实现了表单下的文件上传,前后端分离情况下的文件上传。本案例不连接数据库,只做基本的文件上传操作。

在 SpringBoot 中不需要额外导入其他依赖,正常引入即可。

后端 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);


// 服务器中对应的位置


// 产生唯一的文件名称


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;


}

}

这里只做了简单的文件上传,没有限制文件类型。 

前端写法

这里分为两种写法,一种是常用的表单提交,另一种是当下较火的 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() {


// 表单数据


let data = new FormData();


data.append('file', this.file);


// 定义发送格式


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>

以上就是本文的全部内容。

 

参考文章:http://blog.ncmem.com/wordpress/2023/11/21/springboot实现简单文件上传功能/


 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月30日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   55   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   110   0   0 Java
  8s1LUHPryisj   2024年05月17日   46   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
9m65el8SCpbP