【springboot】保存前端传来的文件到本地

        前一段时间在看谷粒学院的项目的时候,发现前端上传文件需要保存到云上的服务器,云服务器厂商提供文件保存服务,于是本着省事的原则,自己写了简单的保存文件逻辑

接口: 获取文件,保存到本地,并放回磁盘映射路径(不是绝对路径)

package com.malguy.eduservice.controller;

import com.malguy.commonutils.R;
import com.malguy.commonutils.UploadUtils;
import com.malguy.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @author malguy-wang sir
 * @create ---
 */
@RestController
@RequestMapping("/eduoss/fileoss")
@CrossOrigin
@Api(description="文件上传接口")
public class OssController {
    @Autowired
    private EduTeacherService teacherService;
    //上传头像的方法
    @PostMapping
    @ApiOperation("上传文件,保存到本地,并返回磁盘映射路径")
    public R uploadOssFile(MultipartFile file) {
        //获取上传文件  MultipartFile
        //获取文件的内容
        try {
            InputStream is = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取原始文件名
        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);
        //生成一个uuid名称出来
        String uuidFilename = UploadUtils.getUUIDName(originalFilename); 
        //创建新的文件
        //linux
//        File newFile = new File("var/blob/guli_user",uuidFilename);
        //windows
        File newFile = new File("D:/blob/guli_user",uuidFilename);
        //将文件输出到目标的文件中
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回上传到oss的路径
        return R.ok().data("url",
                "http://localhost:9001/img/eduservice/teacher/"+uuidFilename);
    }
}

配置磁盘映射路径

package com.malguy.eduservice.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author malguy-wang sir
 * @create ---
 */
@Configuration
public class FilePathConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/eduservice/teacher/**")//虚拟地址,如1/1.jpg
                .addResourceLocations("file:D:\\blob\\guli_user\\");//       windows
//        .addResourceLocations("file:var/blob/guli_user"); //       linux
    }
}

工具类,生成随机UUID作为图片名

package com.malguy.commonutils; 
import java.util.UUID; 
/**
 * @author malguy-wang sir
 * @create ---
 */
public class UploadUtils {
    /**
     * 获取随机名称
     *
     * @param realName 真实名称
     * @return uuid 随机名称
     */
    public static String getUUIDName(String realName) { 
        //获取后缀名
        int index = realName.lastIndexOf(".");
        if (index == -1) {//如果没有后缀
            return UUID.randomUUID().toString().replace("-", "").toUpperCase();
        } else { //如果有后缀就接上
            return UUID.randomUUID().toString().replace("-", "")
                    .toUpperCase() + realName.substring(index); 
        }
    }
}

你可能感兴趣的:(笔记,技巧,java,spring,maven)