Springboot 将前端传递的图片上传至Linux服务器并返回图片的url(附源码)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Wyx_wx/article/details/89117360

问题由来:

用户个人信息需要添加头像功能

当前端程序是微信小程序时,前端将直接将图片 url 传送至服务端

但是当前端是 Web 页面时,前端传递的参数是一张图片,服务端需要将图片保存至 Linux 服务器的某个文件夹下,并将该图片的访问路径保存至数据库中。

pom.xml


    <!-- thymeleaf -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
      <groupId>net.sourceforge.nekohtml</groupId>
      <artifactId>nekohtml</artifactId>
      <version>1.9.22</version>
    </dependency>

application.properties

# 自定义文件上传路径
# Linux
#web.upload-path=/root/photo
# Windows 10
web.upload-path=E:/image

server.port=8989
FileNameUtils: 生成新的文件名(利用 UUID 防止重名)
package com.example.post.util;

/**
 * @Auther: wyx
 * @Date: 2019-04-08 10:08
 * @Description:
 */

public class FileNameUtils {

    /**
     * 获取文件后缀
     * @param fileName
     * @return
     */
    public static String getSuffix(String fileName){
        return fileName.substring(fileName.lastIndexOf("."));
    }

    /**
     * 生成新的文件名
     * @param fileOriginName 源文件名
     * @return
     */
    public static String getFileName(String fileOriginName){
        return UUIDUtils.getUUID() + FileNameUtils.getSuffix(fileOriginName);
    }


}

FileUtils: 上传图片的具体逻辑
package com.example.post.util;

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

import org.springframework.web.multipart.MultipartFile;

/**
 * @Auther: wyx
 * @Date: 2019-04-08 10:10
 * @Description:
 */

public class FileUtils {

    /**
     *
     * @param file 文件
     * @param path   文件存放路径
     * @param fileName 原文件名
     * @return
     */
    public static String upload(MultipartFile file, String path, String fileName){

        String newFileName = FileNameUtils.getFileName(fileName);

        // 生成新的文件名
        String realPath = path + "/" + newFileName;

        File dest = new File(realPath);

        //判断文件父目录是否存在
        if(!dest.getParentFile().exists()){
            dest.getParentFile().mkdir();
        }

        try {
            //保存文件
            file.transferTo(dest);
            return newFileName;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }
}

FileUploadController
package com.example.post.controller;

import com.example.post.util.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Auther: wyx
 * @Date: 2019-04-08 10:13
 * @Description:
 */

@Controller
public class FileUploadController {

    @Value("${web.upload-path}")
    private String path;

    /**
     *
     * @param file 上传的文件
     * @return
     */
    @ResponseBody
    @RequestMapping("/fileUpload")
    public String upload(@RequestParam("file") MultipartFile file){
        //1定义要上传文件 的存放路径
        String localPath="E:/image";
//        String localPath = "/root/photo";
        //2获得文件名字
        String fileName=file.getOriginalFilename();
        //2上传失败提示
        String warning="";
        String newFileName = FileUtils.upload(file, localPath, fileName);
        if(newFileName != null){
            //上传成功
            warning="上传成功";

        }else{
            warning="上传失败";
        }
        System.out.println(warning);
        return warning;
    }

}

运行后利用 Postman 进行本地测试,则设置的路径下出现了该图片,需要上传至 Linux 服务器时,只需要将上传路径修改即可

GitHub地址:https://github.com/Wyxwx/PictureUploadDemo

参考博文:https://blog.csdn.net/qq_39529566/article/details/81872062

猜你喜欢

转载自blog.csdn.net/Wyx_wx/article/details/89117360