当前位置: 首页 > news >正文

vue+SpringBoot的图片上传

前端VUE的代码实现

直接粘贴过来element-UI的组件实现

        <el-uploadclass="avatar-uploader"action="/uploadAvatar"   //这个action的值是服务端的路径,其他不用改:show-file-list="false":on-success="handleAvatarSuccess":before-upload="beforeAvatarUpload"><img v-if="imageUrl" :src="imageUrl" class="avatar"><i v-else class="el-icon-plus avatar-uploader-icon"></i></el-upload>

后端springboot的代码实现

package com.aqiuo.controller;import com.aqiuo.entity.dto.Result;
import com.aqiuo.utils.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;@RestController
@ResponseBody
@Slf4j
public class UploadAvatar {@RequestMapping(value = "/uploadAvatar",method = {RequestMethod.POST})public Result imgUpDown(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {if(!file.isEmpty()) {String fileName = file.getOriginalFilename();String suffixName = fileName.substring(fileName.indexOf("."));//设置上传文件的保存地址目录String dirpath=request.getServletContext().getRealPath("/upload");System.out.println(dirpath);File parentFilePath=new File(dirpath);//如果保存文件不存在就先创建目录if(!parentFilePath.exists()) {parentFilePath.mkdir();}String fileNewName = UUID.randomUUID() + fileName;File newFile = new File(parentFilePath, fileNewName);file.transferTo(newFile);return Result.ok(newFile);}return null;}
}

易错地点:

文件的存储位置一定要明确

运行效果

http://www.lryc.cn/news/245766.html

相关文章:

  • FFmepg 核心开发库及重要数据结构与API
  • 训练 CNN 对 CIFAR-10 数据中的图像进行分类
  • 香港科技大学广州|智能制造学域博士招生宣讲会—天津大学专场
  • 滑动窗口练习(二)— 子数组中满足max -min <= sum的个数
  • 用xlwings新建一个excel并同时生成多个sheet
  • 诺威信,浪潮云,微众区块链
  • Redux在React中的使用
  • Go 数字类型
  • 时间序列预测 — Informer实现多变量负荷预测(PyTorch)
  • 2023年金融信创行业研究报告
  • 51单片机按键控制LED灯亮灭的N个玩法
  • 推荐6款本周 yyds 的开源项目
  • 【Git】git 更换远程仓库地址三种方法总结分享
  • springboot 返回problem+json
  • AI动画制作 StableDiffusion
  • 【开源】基于Vue和SpringBoot的木马文件检测系统
  • 5 动态规划解分割等和子串
  • file_get_contents() 函数详解与使用
  • 某医生用 ChatGPT 在 4 个月内狂写 16 篇论文,其中 5 篇已发表,揭密ChatGPT进行论文润色与改写的秘籍
  • 进程等待讲解
  • MySQL Binlog深度解析:进阶应用与实战技巧【进阶应用】
  • openpnp - 给底部相机加防尘罩
  • mac mysql连接中断重新启动办法
  • 【Vue3】解决Vue打包后上传服务器 资源路径加载错误
  • u-popup组件在UniApp中的讲解
  • drool 7 multiThread 测试
  • 【网安AIGC专题】46篇前沿代码大模型论文、24篇论文阅读笔记汇总
  • 如何在Simulink中使用syms?换个思路解决报错:Function ‘syms‘ not supported for code generation.
  • 论文导读 | 10月专题内容精选:人的预测
  • 深度学习18