spring boot本地上传图片

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.1.3.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<groupId>cn.com.sctic</groupId>

<artifactId>upload</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>upload</name>

<description>Demo project for Spring Boot</description>

<properties>

<java.version>1.8</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

控制器: UploadController

@Controller

public class UploadController {

private Logger logger = LoggerFactory.getLogger(this.getClass());

@Value("${scitc.upload.src}")

private String rootPath;

@Value("${scitc.upload.host}")

private String uploadhost;

@RequestMapping(value = "/uploadFile",method = { RequestMethod.POST,RequestMethod.GET})

@ResponseBody

public String uploadFile(MultipartFile file) {

//文件的完整名称,如www.198bona.com

String filename = file.getOriginalFilename();

//文件名,如:深圳博纳网络

String name = filename.substring(0,filename.indexOf("."));

//文件后缀,如.jpeg

String suffix = filename.substring(filename.lastIndexOf("."));

//创建年月文件夹

Calendar date = Calendar.getInstance();

File dateDirs = new File(date.get(Calendar.YEAR)

+ File.separator + (date.get(Calendar.MONTH)+1));

//目标文件

File descFile = new File(rootPath+File.separator+dateDirs+File.separator+filename);

int i = 1;

//若文件存在重命名

String newFilename = filename;

while(descFile.exists()) {

newFilename = name+"("+i+")"+suffix;

String parentPath = descFile.getParent();

descFile = new File(parentPath+File.separator+newFilename);

i++;

}

//判断目标文件所在的目录是否存在

if(!descFile.getParentFile().exists()) {

//如果目标文件所在的目录不存在,则创建父目录

descFile.getParentFile().mkdirs();

}

//将内存中的数据写入磁盘

try {

file.transferTo(descFile);

} catch (Exception e) {

e.printStackTrace();

logger.error("上传失败,cause:{}",e);

}

//完整的url

String fileUrl =  uploadhost + rootPath +dateDirs+ "/"+newFilename;

return  "success:" + fileUrl;

}

}

注意:rootPath,uploadhost是可以通过application.properties或者application.yml进行配置的。

由于要对外部资源进行映射需要创建一个类继承WebMvcConfigurationSupport这个适配器,下面是WebMvcConfigurer的这个配置类,代码如下:

@Configuration

public class WebMvcConfigurer extends WebMvcConfigurationSupport {

@Value("${scitc.upload.src}")

private String src;

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler(src + "/**").addResourceLocations("file:" + src);

}

}

注意:这里的src也是从配置文件applicaiton.properties中得到了。

(0)

相关推荐