(整合网络大佬的资料)

在项目开发中经常会碰到做文件上传的功能,一般来说,文件上传的步骤就那么几步,前台通过提交一个选中的文件,后端对文件做处理然后将文件上传至指定的地址,这个地址是一个真实的物理存储路径,可以是本地,也可以是fastdfs等其他的linux文件服务器。我们通过F12可以发现,一些比较大的电商网站里的图片,直接复制图片的url是可以预览并下载下来的,从图片的URL地址规律大概可以推测,这些图片其实是存放在某个或某些文件服务器上的,比较常用的文件服务器像fastdfs,天然支持负载均衡,支持水平扩展,而且很容易做集群。

本节,结合当下比较流行的springboot,利用springboot和fastdfs进行整合,做一个简单的文件上传处理;

https://www.cnblogs.com/yufeng218/p/8111961.html(fastdfs的搭建,挺好的。自己就懒得整理了

从图中可以看出,fastdfs设计的用于管理文件数据的结构非常具有层次性,像一个复杂而有序的文件夹系统,层层管理,每一个有序的节点还有子节点,很方便运维,因此,就算是单台fastdfs服务器,就可以很方便的管理巨大的文件数据,接下来就是代码部分,此处,新建一个springboot的maven 工程,项目结构如图所示,

<?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.0.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</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-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!--日志-->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<!-- thymeleaf模板插件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<!--fastdfs-->
		<dependency>
			<groupId>com.github.tobato</groupId>
			<artifactId>fastdfs-client</artifactId>
			<version>1.26.1-RELEASE</version>
		</dependency>

	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

这里的包就是常规的几个,另外要模拟前台上传文件,项目中主要使用了两个html文件,file.html和success.html

第一个是文件上传的页面,第二个是上传成功后跳转的页面,

file.html代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <meta charset="UTF-8" />
    <title>fileUpload page</title>
</head>
<body>
<h1 th:inlines="text">文件上传</h1>
<form action="uploadFileToFast" method="post" enctype="multipart/form-data">
    <p>
        选择文件: <input type="file" name="fileName" />
    </p>
    <p>
        <input type="submit" value="提交" />
    </p>
</form>
</body>
</html>

success.html代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <meta charset="UTF-8" />
    <title>fileUpload page</title>
</head>
<body>
文件上传成功 !!!
</body>
</html>

下面请看配置文件,连接fastdfs服务器的主要配置项如下:

server.port=8082

#模拟本地上传
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/

#单个文件最大尺寸(设置100)
spring.servlet.multipart.max-file-size=100mb
#一个请求文件的最大尺寸
spring.servlet.multipart.max-request-size=100mb
#设置一个文件上传的临时文件目录(本地需要创建个目录)
spring.servlet.multipart.location=E:/opt/temp/

#FastDfs的配置	====================================
#读取inputsream阻塞时间 
fdfs.connect-timeout=600
fdfs.so-timeout=1500
#tracker地址 
fdfs.trackerList=192.168.6.181:22122 
#缩略图配置
fdfs.thumbImage.height=150
fdfs.thumbImage.width=150
spring.jmx.enabled=false
#通过nginx 访问地址 
fdfs.resHost=192.168.6.181
fdfs.storagePort=8888
#获取连接池最大数量 
fdfs.pool.max-total=200 

 接下来需要初始化fastdfs的相关配置,以便初始化的时候做全局加载,使配置生效,这里创建了两个类,FdfsConfig,FdfsConfiguration,FdfsConfig主要用以连接fastdfs,FdfsConfiguration使配置生效:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * FdfsConfig主要用以连接fastdfs,FdfsConfiguration使配置生效
 */
@Component
public class FdfsConfig {
    @Value("${fdfs.resHost}")
    private String resHost;

    @Value("${fdfs.storagePort}")
    private String storagePort;

    public String getResHost() {
        return resHost;
    }

    public void setResHost(String resHost) {
        this.resHost = resHost;
    }

    public String getStoragePort() {
        return storagePort;
    }

    public void setStoragePort(String storagePort) {
        this.storagePort = storagePort;
    }

}
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.jmx.support.RegistrationPolicy;

@Configuration
@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
public class FdfsConfiguration {

}

 下面是关于使用fastdfs的客户端工具封装的几个操作fastdfs服务器的工具类,这里直接贴上代码,具体的意思可以参考官方文档,工具类在这个文件里,CommonFileUtil,

import com.github.tobato.fastdfs.domain.MateData;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Set;

@Component
public class CommonFileUtil {

    private final Logger logger = LoggerFactory.getLogger(FdfsConfig.class);

    @Autowired
    private FastFileStorageClient storageClient;


    /**
     *	MultipartFile类型的文件上传ַ
     * @param file
     * @return
     * @throws IOException
     */
    public String uploadFile(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()), null);
        return getResAccessUrl(storePath);
    }

    /**
     * 普通的文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    public String uploadFile(File file) throws IOException {
        FileInputStream inputStream = new FileInputStream(file);
        StorePath path = storageClient.uploadFile(inputStream, file.length(),
                FilenameUtils.getExtension(file.getName()), null);
        return getResAccessUrl(path);
    }

    /**
     * 带输入流形式的文件上传
     *
     * @param is
     * @param size
     * @param fileName
     * @return
     */
    public String uploadFileStream(InputStream is, long size, String fileName) {
        StorePath path = storageClient.uploadFile(is, size, fileName, null);
        return getResAccessUrl(path);
    }

    /**
     * 将一段文本文件写到fastdfs的服务器上
     *
     * @param content
     * @param fileExtension
     * @return
     */
    public String uploadFile(String content, String fileExtension) {
        byte[] buff = content.getBytes(Charset.forName("UTF-8"));
        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
        StorePath path = storageClient.uploadFile(stream, buff.length, fileExtension, null);
        return getResAccessUrl(path);
    }

    /**
     * 返回文件上传成功后的地址名称ַ
     * @param storePath
     * @return
     */
    private String getResAccessUrl(StorePath storePath) {
        String fileUrl = storePath.getFullPath();
        return fileUrl;
    }

    /**
     * 删除文件
     * @param fileUrl
     */
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        try {
            StorePath storePath = StorePath.praseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            logger.warn(e.getMessage());
        }
    }

    public String upfileImage(InputStream is, long size, String fileExtName, Set<MateData> metaData) {
        StorePath path = storageClient.uploadImageAndCrtThumbImage(is, size, fileExtName, metaData);
        return getResAccessUrl(path);
    }

}

接下来写一个控制器,FileController,用以操作文件上传,这里模拟单文件上传,如果上传成功,fastdfs会返回给客户端一个地址,这个地址就是存放上传的文件的具体位置,下面看测试代码:

import com.example.demo.comm.CommonFileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class FileController {
    private final static Logger logger = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private CommonFileUtil fileUtil;

    @RequestMapping("/goIndex")
    public String goIndex(){
        logger.info("进入主页面");
        return "/file";
    }

    @RequestMapping("/fileUpload")
    public String fileUpload(@RequestParam("fileName") MultipartFile file){

        String targetFilePath = "E:/opt/uploads/";

        if(file.isEmpty()){
            logger.info("this file is empty");
        }

        String newFileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        //获取原来文件名称
        String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        if(!fileSuffix.equals(".jpg") || !fileSuffix.equals(".png")){
            logger.info("文件格式不正确");
        }
        //拼装新的文件名
        String targetFileName = targetFilePath + newFileName + fileSuffix;
        //上传文件
        try {
            FileCopyUtils.copy(file.getInputStream(),new FileOutputStream(targetFileName));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "/success";
    }

    //使用fastdfs进行文件上传
    @RequestMapping("/uploadFileToFast")
    public String uoloadFileToFast(@RequestParam("fileName")MultipartFile file) throws IOException{

        if(file.isEmpty()){
            logger.info("文件不存在");
        }
        String path = fileUtil.uploadFile(file);
        System.out.println(path);
        return "success";
    }


}

这里使用的是uoloadFileToFast这个控制器,最后启动MainApp主函数

import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;


@SpringBootApplication
@Import(FdfsClientConfig.class)
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

启动成功,在页面的输入框选择一个本地的图片,我这里提前准备好了一张图片:

这样一来,我们就可以直接访问到fastdfs服务器上的这张图片了,当然,需要你把防火墙中的8888端口打开(嫌麻烦就关闭了)

上述就是springboot整合fastdfs做文件上传的流程,实际业务中,我们在和页面进行交互的时候,一旦上传成功,我们需要将返回的文件地址重新包装返回给前台,前台继续做其他的处理,也可以将得到的地址存入mysql,方便其他的业务使用;

另外,温馨提示一下,在整合的过程中,遇到了一个不大不小的坑,耽误了不少的时间,大家在使用springboot整合fastdfs的时候,如果你的springboot版本是2.x的,一定要使用我上面的fastdfs版本,否则请更换springboot的版本至1.5.2,遇到这个问题后,网上的一大堆解决办法就是行不通,最后采用了更换fastdfs版本才得到解决!
 

Logo

更多推荐