其他分享
首页 > 其他分享> > springboot图片(文件)上传(实测有效)

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("-", "");
    }
}

标签:springboot,System,实测,println,import,out,上传,public,String
来源: https://blog.csdn.net/qq_41820986/article/details/120602996