700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 上传图片到linux返回url Springboot 将前端传递的图片上传至Linux服务器并返回图片

上传图片到linux返回url Springboot 将前端传递的图片上传至Linux服务器并返回图片

时间:2018-09-27 06:18:04

相关推荐

上传图片到linux返回url Springboot 将前端传递的图片上传至Linux服务器并返回图片

问题由来:

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

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

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

pom.xml

org.springframework.boot

spring-boot-starter-thymeleaf

net.sourceforge.nekohtml

nekohtml

1.9.22

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: -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: -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: -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地址:/Wyxwx/PictureUploadDemo

参考博文:/qq_39529566/article/details/81872062

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。