spring boot 图片上传
html页面代码:
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>文件上传</title> <!-- 导入Vue --> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <!-- 导入axios --> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <div id="bigdiv"> <input type="file" ref="fileInt" name="file" @change="changeHandle"><br> <img :src="imgurl"> </div> <script> var vueUpload=new Vue({ el:'#bigdiv', data:{ imgurl:"" }, mounted(){ }, methods:{ changeHandle() { const file = this.$refs.fileInt.files[0]; const data = new FormData(); data.append('file', file); axios({ method: 'post', url: 'http://localhost:8080/upload', data, headers: { 'Content-Type': 'multipart/form-data', }, }).then(response => { this.imgurl = response.data.data[0]; }).catch(err => { console.log(err); }); } } }) </script> </body></html>
Controller层:
@PostMapping("/upload") @ResponseBody public imgUploadResult upload(@RequestParam("file") MultipartFile file) { //获取文件路径 String filePath = "F:\\Program Files\\JetBrains\\hadoopWork\\gameStra.........