springboot图片(文件)上传(实测有效)

前端采用element

<el-form-item label="头像">
          <el-upload
              class="avatar-uploader"
              action="http://127.0.0.1:8082/upload"
              :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>
        </el-form-item>

后端

图片文件夹image

在这里插入图片描述

package com.example.dpool.controller;

import com.example.dpool.entity.vo.*;

import org.springframework.util.ResourceUtils;
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.FileNotFoundException;
import java.io.IOException;

@RestController
public class Upload {
    /**
     * 文件上传
     * @param picture
     * @param request
     * @return
     */
    @RequestMapping("/upload")
    public FileResult upload(@RequestParam("file") MultipartFile picture, HttpServletRequest request) throws FileNotFoundException {

        System.out.println("您已进入图片上传服务");
        //获取文件在服务器的储存位置,此种方式直接存入了真实文件夹
        //String path = "D:\\dpool\\src\\main\\resources\\static\\image";
        //文件上传的地址,此种方式直接存入了target
        String realPath = ResourceUtils.getURL("classpath:").getPath()+"static/image";
        String path = realPath.replace('/', '\\').substring(1,realPath.length());


        File filePath = new File(path);
        System.out.println("文件的保存路径:" + path);
        if (!filePath.exists() && !filePath.isDirectory()) {
            System.out.println("目录不存在,创建目录:" + filePath);
            filePath.mkdir();
        }

        //获取原始文件名称(包含格式)
        String originalFileName = picture.getOriginalFilename();
        System.out.println("原始文件名称:" + originalFileName);

        //获取文件类型,以最后一个`.`为标识
        String type = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
        System.out.println("文件类型:" + type);
        //获取文件名称(不包含格式)
        String name = originalFileName.substring(0, originalFileName.lastIndexOf("."));

        String fileName = UUIDUtils.getUuid() + name + "." + type;
        System.out.println("新文件名称:" + fileName);

        //在指定路径下创建一个文件
        File targetFile = new File(path, fileName);
        System.out.println("图片地址:"+path+"/"+fileName);
        //将文件保存到服务器指定位置
        try {
            picture.transferTo(targetFile);
            System.out.println("上传成功");
            //将文件在服务器的存储路径返回
            return new FileResult(true,"http://127.0.0.1:8082/image/"+fileName,path+"/"+fileName);
        } catch (IOException e) {
            System.out.println("上传失败");
            e.printStackTrace();
            return new FileResult(false, "上传失败","");
        }
    }



}

工具类1

package com.example.dpool.entity.vo;

import lombok.Data;

import java.io.Serializable;
@Data
public class FileResult implements Serializable {
    //判断结果
    private boolean success;
    //返回信息
    private String message;
    //文件地址
    private String fileAddress;

    private String start;

    public FileResult(boolean success, String message,String fileAddress) {
        this.success = success;
        this.message = message;
        this.fileAddress = fileAddress;
    }

    public boolean isSuccess() {
        return success;
    }
}



工具类2

package com.example.dpool.entity.vo;
import java.util.UUID;
public class UUIDUtils {
    private UUIDUtils(){}
    public static String getUuid(){
        return UUID.randomUUID().toString().replace("-", "");
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值