vue+springboot 上传单张图片(jpg/png)

java后端

配置文件

#上传文件到vue路径中
file:
  uploadFolder: C:\Users\iMac\Desktop\front\src\image\

配置类uuid更改文件名字

复制代码
package com.example.back.utils;

import java.util.UUID;

/**
 *
 * @项目: mail--cc.ccoder.mail.utils
 * @TODO: 生成随机字符串的工具类 uuid
 */
public class UuidUtil {

    public static String getUUID(){
         return UUID.randomUUID().toString().replace("-", "");
    }

    public static void main(String[] args) {
        System.out.println("格式前的UUID : " + UUID.randomUUID().toString());
        System.out.println("格式化后的UUID :" + getUUID());
    }
}
复制代码

 

复制代码
package com.example.back.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import com.example.back.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@CrossOrigin//解决跨域问题
@RestController
@RequestMapping("/file")
public class FileController {
    @Value("${file.uploadFolder}")
    private String uploadFolder;

    @RequestMapping("/upload")
    public String FileUpdate (HttpServletRequest request,MultipartFile file) {
        //配置文件设置想保存的路径
        String str=uploadFolder;
        //得到上传时的文件名字
         String originalname=file.getOriginalFilename();
        System.out.println("上传时的文件名字:"+originalname);
        
         //substring(originalname.lastIndexOf(".")截取图片名后缀
         String newName= originalname.substring(originalname.lastIndexOf("."));
        System.out.println("图片名字:"+originalname);
        System.out.println("截取图片名后缀:"+newName);
        
        String frontNewName= originalname.substring(0,originalname.lastIndexOf("."));
        System.out.println("图片名字:"+originalname);
        System.out.println("截取图片名前缀:"+frontNewName);
        
         //利用UUidUtil工具类生成新的文件名字
         newName = UuidUtil.getUUID()+newName;
         
           System.out.println(newName);
           System.out.println(str);
           File newFile=new File(str,newName);
           
           //获得文件目录,判断是否存在
           if(!newFile.getParentFile().exists()) {
               //如果path路径不存在,创建一个文件夹,存在则使用当前文件夹
               newFile.getParentFile().mkdirs();
           }
           try {
               //保存文件到指定路径之中
            file.transferTo(newFile);
        } catch (IllegalStateException | IOException e) {
        
            e.printStackTrace();
        }
           System.out.println("上传的文件路径:"+str+newName);
           return newName;


    }
}
复制代码

 

vue前端

复制代码
<el-upload
            class="upload-demo"
            action="http://localhost:8091/file/upload"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            multiple="false"
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList"
            :on-success="handleAvatarSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件,且不超过500kb
            </div>
          </el-upload>
复制代码
复制代码
 //上传图片
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}?`);
    },
    // 文件上传成功时的钩子
    handleAvatarSuccess(res, file) {
      this.form.leaveUrl = res;
      console.log(res);
      console.log(file);
    },
复制代码

显示上传的图片(另一页面)

 <el-image
            style="width: 100px; height: 100px"
            :src="require('/src/image/' + leaveUrl)"
          >
          </el-image>

 

 

posted @   快了星球  阅读(576)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示