欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一步步完成FastDFS + Spring MVC上传下载整合示例

程序员文章站 2022-05-06 21:21:55
...

准备FastDFS连接jar包

下载代码

本文示例中,使用了两个代码库

将上述代码下载到本地,如

一步步完成FastDFS + Spring MVC上传下载整合示例

打包和Maven安装

将上述代码打包成一个jar包,如fastdfs-client-pool-java-0.0.1.jar

然后使用Maven完成从jar包安装~

mvn install:install-file -DgroupId=org.xxx -DartifactId=fastdfs-client-pool -Dversion=0.0.1 -Dpackaging=jar -Dfile=fastdfs-client-pool-java-0.0.1.jar

C:\Users\wangmengjun>mvn install:install-file -DgroupId=org.xxx -DartifactId=fastdfs-client-pool -Dversion=0.0.1 -Dpackaging=jar -Dfile=fastdfs-client-pool-java-0.0.1.jar
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ standalone-pom ---
[INFO] Installing C:\Users\wangmengjun\fastdfs-client-pool-java-0.0.1.jar to D:\java_tools\Reponsitories\Maven\org\xxx\fastdfs-client-pool\0.0.1\fastdfs-client-pool-0.0.1.jar
[INFO] Installing C:\Users\WANGME~1\AppData\Local\Temp\mvninstall6453901574382164817.pom to D:\java_tools\Reponsitories\Maven\org\xxx\fastdfs-client-pool\0.0.1\fastdfs-client-pool-0.0.1.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.443 s
[INFO] Finished at: 2017-07-05T20:02:57+08:00
[INFO] Final Memory: 7M/244M
[INFO] ------------------------------------------------------------------------

成功之后,就可以直接在pom.xml中引用上述打包好的文件~ 如:

		<dependency>
			<groupId>org.xxx</groupId>
			<artifactId>fastdfs-client-pool</artifactId>
			<version>0.0.1</version>
		</dependency>

搭建Spring MVC工程

一个简单的Spring MVC工程结构如下:

一步步完成FastDFS + Spring MVC上传下载整合示例

pom.xml添加依赖j包

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.xxx.tutorial.demo</groupId>
	<artifactId>fastdfs-springmvc-demo</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>fastdfs-springmvc-demo Maven Webapp</name>
	<url>http://maven.apache.org</url>


	<!-- 配置属性选项,如encoding以及jar包的版本信息 -->
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<!-- Spring版本 -->
		<spring.framework.version>4.3.6.RELEASE</spring.framework.version>
	</properties>

	<dependencies>
		<!-- Servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>
		<!-- Spring 框架 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>

		<!-- logback日志相关 -->
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>1.1.3</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.1.3</version>
		</dependency>
		<!-- JSON 处理包 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.28</version>
		</dependency>


		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>commons-pool</groupId>
			<artifactId>commons-pool</artifactId>
			<version>1.5.5</version>
		</dependency>

		<dependency>
			<groupId>org.xxx</groupId>
			<artifactId>fastdfs-client-pool</artifactId>
			<version>0.0.1</version>
		</dependency>


		<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.3</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>fastdfs-springmvc-demo</finalName>
	</build>
</project>

创建xml配置文件

一步步完成FastDFS + Spring MVC上传下载整合示例

  • applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:websocket="http://www.springframework.org/schema/websocket"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task-4.0.xsd">

	<!-- enable autowire 向容器自动注册 -->
	<context:annotation-config />

	<!-- 设置使用注解的类所在的jar包 -->
	<context:component-scan base-package="com.xxx.tutorial.demo" />

	<!-- 完成请求和注解POJO的映射 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
		
	<!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
	<bean id="configProperties"
		class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="locations">
			<list>
				<value>classpath:fdfs_client.properties</value>
			</list>
		</property>
	</bean>
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="properties" ref="configProperties" />
	</bean>

</beans>
  • spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<!-- 默认的注解映射的支持 ,它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter -->
	<mvc:annotation-driven />

	<!-- enable autowire 向容器自动注册 -->
	<context:annotation-config />

	<!-- 设置使用注解的类所在的jar包 -->
	<context:component-scan base-package="com.xxx.tutorial.demo" />

	<!-- 完成请求和注解POJO的映射 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

	<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/jsp/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>

	<!-- 对静态资源文件的访问 -->
	<mvc:resources mapping="/js/**" location="/js/"
		cache-period="31556926" />

	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8" />
		<property name="maxUploadSize" value="10485760000" />
		<property name="maxInMemorySize" value="40960" />
	</bean>

	<!-- 属性文件读入 -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:fdfs_client.properties</value>
			</list>
		</property>
	</bean>

</beans>
  • fdfs_client.properties
connect_timeout = 2000
network_timeout = 30000

charset = UTF-8

http.tracker_http_port = 80

http.anti_steal_token = false

http.secret_key = FastDFS1234567890

tracker_server = <YOUR_DEMO_IP>:22122

#fasfdfs nginx port
tracker_nginx_port=19080


tracker_nginx_host=<YOUR_DEMO_IP>
#pool config
pool.maxActive = 20
pool.maxIdle = 5
pool.minIdle = 2
pool.maxWait = 100
pool.testOnBorrow = false
pool.testOnReturn = false
pool.testWhileIdle = true

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" metadata-complete="true" version="3.0">
  <display-name>couchbaseRPC</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>Spring-MVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Spring-MVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>
      	org.springframework.web.util.IntrospectorCleanupListener
      </listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <session-config>
    <session-timeout>60</session-timeout>
  </session-config>
</web-app>

Spring MVC工程基本的完成了,接下来,我们就要为操作FastDFS上的文件做准备工作了~

FastDFS操作类

FastDFSFile

FastDFS文件属性~

package com.xxx.tutorial.demo.fdfs;

import java.io.Serializable;
import java.util.Arrays;

public class FastDFSFile implements Serializable {

	private static final long serialVersionUID = 1072167272807139878L;

	private byte[] content;
	private String name;
	private String suffix;
	private String length;
	private String author;

	/**
	 * @return the content
	 */
	public byte[] getContent() {
		return content;
	}

	/**
	 * @param content
	 *            the content to set
	 */
	public void setContent(byte[] content) {
		this.content = content;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the suffix
	 */
	public String getSuffix() {
		return suffix;
	}

	/**
	 * @param suffix
	 *            the suffix to set
	 */
	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	/**
	 * @return the length
	 */
	public String getLength() {
		return length;
	}

	/**
	 * @param length
	 *            the length to set
	 */
	public void setLength(String length) {
		this.length = length;
	}

	/**
	 * @return the author
	 */
	public String getAuthor() {
		return author;
	}

	/**
	 * @param author
	 *            the author to set
	 */
	public void setAuthor(String author) {
		this.author = author;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "FastDFSFile [content=" + Arrays.toString(content) + ", name=" + name + ", suffix=" + suffix
				+ ", length=" + length + ", author=" + author + "]";
	}

}

FastDFSConfig

FastDFS配置类~

package com.xxx.tutorial.demo.fdfs;

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

@Component
public class FastDFSConfig {

	@Value("#{configProperties['pool.maxActive']}")
	private int maxActive;

	@Value("#{configProperties['pool.maxIdle']}")
	private int maxIdle;

	@Value("#{configProperties['pool.minIdle']}")
	private int minIdle;

	@Value("#{configProperties['pool.maxWait']}")
	private int maxWait;

	@Value("#{configProperties['pool.testOnBorrow']}")
	private boolean testOnBorrow;

	@Value("#{configProperties['pool.testOnReturn']}")
	private boolean testOnReturn;

	@Value("#{configProperties['pool.testWhileIdle']}")
	private boolean testWhileIdle;

	@Value("#{configProperties['tracker_nginx_host']}")
	private String trackerNginxHost = "localhost";

	@Value("#{configProperties['tracker_nginx_port']}")
	private String trackerNginxPort = "80";

	/**
	 * @return the maxActive
	 */
	public int getMaxActive() {
		return maxActive;
	}

	/**
	 * @param maxActive
	 *            the maxActive to set
	 */
	public void setMaxActive(int maxActive) {
		this.maxActive = maxActive;
	}

	/**
	 * @return the maxIdle
	 */
	public int getMaxIdle() {
		return maxIdle;
	}

	/**
	 * @param maxIdle
	 *            the maxIdle to set
	 */
	public void setMaxIdle(int maxIdle) {
		this.maxIdle = maxIdle;
	}

	/**
	 * @return the minIdle
	 */
	public int getMinIdle() {
		return minIdle;
	}

	/**
	 * @param minIdle
	 *            the minIdle to set
	 */
	public void setMinIdle(int minIdle) {
		this.minIdle = minIdle;
	}

	/**
	 * @return the maxWait
	 */
	public int getMaxWait() {
		return maxWait;
	}

	/**
	 * @param maxWait
	 *            the maxWait to set
	 */
	public void setMaxWait(int maxWait) {
		this.maxWait = maxWait;
	}

	/**
	 * @return the testOnBorrow
	 */
	public boolean isTestOnBorrow() {
		return testOnBorrow;
	}

	/**
	 * @param testOnBorrow
	 *            the testOnBorrow to set
	 */
	public void setTestOnBorrow(boolean testOnBorrow) {
		this.testOnBorrow = testOnBorrow;
	}

	/**
	 * @return the testOnReturn
	 */
	public boolean isTestOnReturn() {
		return testOnReturn;
	}

	/**
	 * @param testOnReturn
	 *            the testOnReturn to set
	 */
	public void setTestOnReturn(boolean testOnReturn) {
		this.testOnReturn = testOnReturn;
	}

	/**
	 * @return the testWhileIdle
	 */
	public boolean isTestWhileIdle() {
		return testWhileIdle;
	}

	/**
	 * @param testWhileIdle
	 *            the testWhileIdle to set
	 */
	public void setTestWhileIdle(boolean testWhileIdle) {
		this.testWhileIdle = testWhileIdle;
	}

	/**
	 * @return the trackerNginxHost
	 */
	public String getTrackerNginxHost() {
		return trackerNginxHost;
	}

	/**
	 * @param trackerNginxHost
	 *            the trackerNginxHost to set
	 */
	public void setTrackerNginxHost(String trackerNginxHost) {
		this.trackerNginxHost = trackerNginxHost;
	}

	/**
	 * @return the trackerNginxPort
	 */
	public String getTrackerNginxPort() {
		return trackerNginxPort;
	}

	/**
	 * @param trackerNginxPort
	 *            the trackerNginxPort to set
	 */
	public void setTrackerNginxPort(String trackerNginxPort) {
		this.trackerNginxPort = trackerNginxPort;
	}

}

FastDFSFileManager

文件管理,包含FastDFS文件的上传和下载等~

package com.xxx.tutorial.demo.service;

import org.csource.common.NameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fast.pool.FastdfsPool;
import com.fast.pool.FastdfsPoolConfig;
import com.fast.pool.StorageClient;
import com.xxx.tutorial.demo.fdfs.FastDFSConfig;
import com.xxx.tutorial.demo.fdfs.FastDFSFile;

@Service
public class FastDFSFileManager {

	/** 协议 */
	private static final String PROTOCOL = "http://";

	/** 路径分隔符 */
	private static final String SEPARATOR = "/";

	@Autowired
	private FastDFSConfig config;

	/**
	 * 上传,附加属性
	 * 
	 * @param file
	 * @param valuePairs
	 * @return
	 * @throws Exception
	 */
	public String[] upload(FastDFSFile file, NameValuePair[] valuePairs) throws Exception {
		FastdfsPool pool = new FastdfsPool(initPoolConfig(), "fdfs_client.properties");
		StorageClient storageClient = null;
		String[] uploadResults = null;
		try {
			storageClient = pool.getResource();
			uploadResults = storageClient.upload_file(file.getContent(), file.getSuffix(), valuePairs);

		} catch (Exception e1) {
			pool.returnResource(storageClient);
		} finally {
			pool.returnResource(storageClient);
		}
		return uploadResults;// getFileUrl(uploadResults);
	}

	/**
	 * 初始化配置
	 * 
	 * @return
	 */
	private FastdfsPoolConfig initPoolConfig() {
		FastdfsPoolConfig poolCfg = new FastdfsPoolConfig();
		poolCfg.setMaxActive(config.getMaxActive());
		poolCfg.setMaxIdle(config.getMaxIdle());
		poolCfg.setMaxWait(config.getMaxWait());
		poolCfg.setMinIdle(config.getMinIdle());
		poolCfg.setTestOnBorrow(config.isTestOnBorrow());
		poolCfg.setTestOnReturn(config.isTestOnReturn());
		poolCfg.setTestWhileIdle(config.isTestWhileIdle());
		return poolCfg;
	}

	/**
	 * 上传
	 * 
	 * @param file
	 * @return
	 * @throws Exception
	 */
	public String[] upload(FastDFSFile file) throws Exception {
		return upload(file, null);
	}

	/**
	 * 组装文件路径
	 * 
	 * @param uploadResults
	 * @return
	 */
	private String getFileUrl(String[] uploadResults) {
		if (uploadResults != null) {
			String groupName = uploadResults[0];
			String remoteFileName = uploadResults[1];
			StringBuilder sb = new StringBuilder();
			sb.append(PROTOCOL);
			sb.append(config.getTrackerNginxPort());
			sb.append(":");
			sb.append(SEPARATOR);
			sb.append(groupName);
			sb.append(SEPARATOR);
			sb.append(remoteFileName);
			return sb.toString();
		} else {
			return null;
		}
	}

	public byte[] downloadFile(String groupName, String remoteFileName) throws Exception {
		FastdfsPool pool = new FastdfsPool(initPoolConfig(), "fdfs_client.properties");
		StorageClient storageClient = null;
		byte[] downloadResults = null;
		try {
			storageClient = pool.getResource();
			downloadResults = storageClient.download_file(groupName, remoteFileName);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pool.returnResource(storageClient);
		}
		return downloadResults;
	}
}

文件上传

通过

		<dependency>
			<groupId>org.xxx</groupId>
			<artifactId>fastdfs-client-pool</artifactId>
			<version>0.0.1</version>
		</dependency>

使用fastdfs连接工具包,其中,com.fast.pool.StorageClient提供了文件上传的多种方法,如:

一步步完成FastDFS + Spring MVC上传下载整合示例

上传文件的方法可以在FastDFSFileManager类中找到,如:

	public String[] upload(FastDFSFile file, NameValuePair[] valuePairs) throws Exception {
		FastdfsPool pool = new FastdfsPool(initPoolConfig(), "fdfs_client.properties");
		StorageClient storageClient = null;
		String[] uploadResults = null;
		try {
			storageClient = pool.getResource();
			uploadResults = storageClient.upload_file(file.getContent(), file.getSuffix(), valuePairs);

		} catch (Exception e1) {
			pool.returnResource(storageClient);
		} finally {
			pool.returnResource(storageClient);
		}
		return uploadResults;// getFileUrl(uploadResults);
	}

文件上传后会返回一个String数组,其中,

第一个元素为GroupName,如group1

第二个元素为RemoteFileName, 如M00/00/00/rBcfylkawqaAbttWAAKucQftPc0621.jpg

文件控制器类


package com.xxx.tutorial.demo.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.xxx.tutorial.demo.fdfs.FastDFSFile;
import com.xxx.tutorial.demo.service.FastDFSFileManager;

@Controller
@RequestMapping("/file")
public class FileController {

	@Autowired
	private FastDFSFileManager fdfsClient;

	/**
	 * 转到文件上传页面
	 */
	@RequestMapping("/add")
	public String login() {
		return "file_add";
	}

	@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String uploadFile(@RequestParam("file") CommonsMultipartFile uploadFile, HttpServletRequest request,
			HttpServletResponse response) {
		String originalFilename = uploadFile.getOriginalFilename();
		String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
		JSONObject jsonObj = new JSONObject();
		try {
			FastDFSFile fdfsFile = new FastDFSFile();
			fdfsFile.setContent(uploadFile.getBytes());
			fdfsFile.setSuffix(fileSuffix);
			String[] uploadResult = fdfsClient.upload(fdfsFile);
			jsonObj.put("success", "Y");
			jsonObj.put("groupName", uploadResult[0]);
			jsonObj.put("remoteFileName", uploadResult[1]);
			return jsonObj.toJSONString();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonObj.toJSONString();
	}

	
}

编写文件上传jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>文件上传页面</title>
</head>
<body>
	<form enctype="multipart/form-data" action="upload" method="POST"
		id="J_upload_form">
		<input type="file" id="J_file" name="file" /> <input type="button"
			id="J_file_ok" value="上传" />
	</form>

	<br />
	<!-- <input type="button" value="下载" id="J_download" /> -->
	<a href="" id="J_download">下载</a>
	<input id="groupName" type="hidden" value="" />
	<input id="remoteFileName" type="hidden" value="" />

</body>
</html>

<script type="text/javascript" src="../js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="../js/jquery.form.js"></script>
<script type="text/javascript" src="../js/file_upload.js"></script>

 

一步步完成FastDFS + Spring MVC上传下载整合示例

页面内容很简单,

上传”按钮将提交选中的文件,然后,返回给页面FastDFS文件的groupName和remoteFileName~。

然后点击“下载”按钮就可以根据groupName和remoteFileName下载刚刚上传的文件~

编写上传文件的js

$(function() {

	/**
	 * 上传按钮点击
	 */
	$('#J_file_ok').click(function() {
		/**
		 * 文件上传表单处理
		 */
		$('#J_upload_form').ajaxForm({
			beforeSend : function() {
				// 表单提交前做表单验证
			},
			success : function(result) {
				if (result.success == 'Y') {
					var downloadURL = "download?groupName="+result.groupName+"&remoteFileName="+result.remoteFileName;
					$('#J_download').attr('href',downloadURL);
				}
			},
			dataType : 'json'
		}).submit();
	});

});

jsp和js的目录结构如下~

一步步完成FastDFS + Spring MVC上传下载整合示例

其中,文件上传使用了jquey.form.js来做~

注意,

文件上传需要配置multipartResolver,如:

	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8" />
		<property name="maxUploadSize" value="10485760000" />
		<property name="maxInMemorySize" value="40960" />
	</bean>

 

上传一个文件,然后debug一下试试~ 

一步步完成FastDFS + Spring MVC上传下载整合示例

可以看出上传后已经成功返回groupName和remoteFileName~

至此,上传部分已经完成了~

文件下载

FastDFS的文件下载还是采用com.fast.pool.StorageClient类来完成~,其包含的download方法如下:

一步步完成FastDFS + Spring MVC上传下载整合示例

下载文件的方法可以在FastDFSFileManager类中找到,如:

	public byte[] downloadFile(String groupName, String remoteFileName) throws Exception {
		FastdfsPool pool = new FastdfsPool(initPoolConfig(), "fdfs_client.properties");
		StorageClient storageClient = null;
		byte[] downloadResults = null;
		try {
			storageClient = pool.getResource();
			downloadResults = storageClient.download_file(groupName, remoteFileName);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pool.returnResource(storageClient);
		}
		return downloadResults;
	}

页面添加FastDFS文件下载链接

<a href="" id="J_download">下载</a>

其中,下载的路径是通过上传文件表单提交成功后,返回拼凑的,如~

		$('#J_upload_form').ajaxForm({
			beforeSend : function() {
				// 表单提交前做表单验证
			},
			success : function(result) {
				if (result.success == 'Y') {
					var downloadURL = "download?groupName="+result.groupName+"&remoteFileName="+result.remoteFileName;
					$('#J_download').attr('href',downloadURL);
				}
			},
			dataType : 'json'
		}).submit();

控制器编写下载方法

​
@RequestMapping(value = "/download", method = RequestMethod.GET)
	public ResponseEntity<byte[]> downloadFile(String groupName, String remoteFileName, HttpServletResponse response)
			throws Exception {

		String fileName = remoteFileName.substring(remoteFileName.lastIndexOf("/") + 1);
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
		try {

			OutputStream os = response.getOutputStream();
			byte[] b = fdfsClient.downloadFile(groupName, remoteFileName);
			os.write(b, 0, b.length);
			os.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

​

至此,使用FastDFS存储文件,使用Spring MVC完成文件上传和下载的整合示例就全部完成了,具体的Maven工程结构如下图所示:

一步步完成FastDFS + Spring MVC上传下载整合示例

上传下载功能验证

运行工程,来测试一下上传下载的功能~

  • 访问文件上传页面

一步步完成FastDFS + Spring MVC上传下载整合示例

  • 选择一个图片文件,点击“上传”按钮上传文件

点击“选择文件”按钮,选择要上传的文件

一步步完成FastDFS + Spring MVC上传下载整合示例

然后,

一步步完成FastDFS + Spring MVC上传下载整合示例

并点击“上传”按钮~

  • 点击“下载”链接下载刚刚上传的图片

一步步完成FastDFS + Spring MVC上传下载整合示例

可以从上图左下角看出,文件已经下载~ 点击打开,看到和我们上传的图片是一样的~

一步步完成FastDFS + Spring MVC上传下载整合示例

至此,一个简单的FastDFS存储文件,并使用Spring MVC完成上传下载的整合示例就完成了~

小结

本文我们一步一步完成以FastDFS为文件存储,结合Spring MVC完成上传下载的整合示例。

代码下载https://pan.baidu.com/s/1hsP45NI

本文给出的示例比较简单,采用com.fast.pool.StorageClient类还可以完成其它操作,

如“删除文件” --【删除文件和下载文件一样,也使用了groupName和remoteFileName】

一步步完成FastDFS + Spring MVC上传下载整合示例

文件追加等功能~

一步步完成FastDFS + Spring MVC上传下载整合示例

分片上传、分片下载等功能将不再本文给出~ 

有兴趣的读者,可以自己玩玩并做些更加深入的研究~ 一步步完成FastDFS + Spring MVC上传下载整合示例

 

 

原文地址:https://my.oschina.net/wangmengjun/blog/1163030