分布式商城项目-后台开发-文件上传和下载
一 fastdfs-client-java 依赖包
在 编 写 文 件 上 传 下 载 客 户 端 之 前 , 需 要 处 理 fastdfs-client-java 依 赖 包 。 获 取
fastdfs-client-java 依赖包有两种方式:
1.1 直接从 maven 获取 fastdfs-client-java 依赖包
<!-- https://mvnrepository.com/artifact/org.csource/fastdfs-client-java -->
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-RELEASE</version>
</dependency>
注意: 这种方式获取的依赖包, 是基于 JDK1.8 编译的。 如果直接放在我们的项目中,
会有错误。 因为项目是基于 JDK1.7 的, 而且使用 maven 的 tomcat7 插件进行运行, 这个
tomcat7 插件默认是基于 jdk1.7 运行的。 如果直接将项目的编译环境修改为 jdk1.8, 那么
tomcat7 插件将不能启动。
解决这个问题可以采用下面第二中方式:
1.2 下载 fastdfs-client-java 源代码, 自行编译成 jar 包
1、 下载地址: https://github.com/happyfish100/fastdfs-client-java
2、 解压文件, 并导入到 eclipse
3、 修改编译环境 JDK1.7
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncodi
ng>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<maven.test.skip>true</maven.test.skip>
<jdk.version>1.7</jdk.version>
</properties>
4、 编译发布
二 封装 FastDFS-client 客户端
2.1 创键 maven 项目 fastdfs-client
<groupId>cn.yuechenc</groupId>
<artifactId>fastdfs-client</artifactId>
打包方式: jar
2.2 pom.xml
<dependencies>
<!--上面编译好的 fastdfs-client-java -->
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java 编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
2.3 封装 FastDFSClient 类
package cn.yuechenc.fastdfs.client;
import java.io.IOException;
import java.net.URLDecoder;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null;
/**
* 调用本类时, 需要在 resources 中增加 fdfs_client.properties 配置文件
*
* @throws Exception
*/
public FastDFSClient() throws Exception {
String conf = this.getClass().getResource("/fdfs_client.properties").getPath();
if (conf.contains("classpath:")) {
String path = URLDecoder.decode(getClass().getProtectionDomain().getCodeSource().getLocation().toString(),
"UTF-8");
path = path.substring(6);
conf = conf.replace("classpath:", URLDecoder.decode(path, "UTF-8"));
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}
/***
* 上传文件方法*
* <p>
* Title:uploadFile
* </p>
* *
* <p>
* Description:
* </p>
* *
*
* @param fileName
* 文件全路径*
* @param extName
* 文件扩展名,不包含(.)*
* @param metas
* 文件扩展信息*@return*@throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) {
String result = null;
try {
result = storageClient.upload_file1(fileName, extName, metas);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
}
/***
* 上传文件,
*
* 传 fileName*
*
* @param fileName
* 文件的磁盘路径名称 如:D:/image/aaa.jpg*@return null 为失败
*/
public String uploadFile(String fileName) {
return uploadFile(fileName, null, null);
}
/****
*
* @param fileName
* 文件的磁盘路径名称 如:D:/image/aaa.jpg*
* @param extName
* 文件的扩展名 如 txt jpg 等*@return null 为失败
*/
public String uploadFile(String fileName, String extName) {
return uploadFile(fileName, extName, null);
}
/***
* 上传文件方法*
* <p>
* Title:uploadFile
* </p>
* *
* <p>
* Description:
* </p>
* *
*
* @param fileContent
* 文件的内容,字节数组*
* @param extName
* 文件扩展名*
* @param metas
* 文件扩展信息*@return*@throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) {
String result = null;
try {
result = storageClient.upload_file1(fileContent, extName, metas);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
return result;
}
/***
* 上传文件*
*
* @param fileContent
* 文件的字节数组*@return null 为失败*@throws Exception
*/
public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}
/***
* 上传文件*
*
* @param fileContent
* 文件的字节数组*
* @param extName
* 文件的扩展名 如 txt jpg png 等*@return null 为失败
*/
public String uploadFile(byte[] fileContent, String extName) {
return uploadFile(fileContent, extName, null);
}
}
2.4 fdfs_client.properties 示例
connect_timeout = 2
network_timeout = 30
charset = UTF-8
# tracker Http port
http.tracker_http_port = 8088
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
# tracker Server address
tracker_server = 47.100.224.4:22122
三 文件上传示例
3.1 tyh-manager-web 项目中创建 controller
3.2 引用 fastdfs-client 项目 pom.xml
<dependency>
<groupId>cn.yuechenc</groupId>
<artifactId>fastdfs-client</artifactId><version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 文件上传组件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
3.3 增加 fdfs_client.properties 配置文件
参加第二小结的配置文件
3.4 修改 spring-mvc.xml 文件
在 spring-mvc.xml 文件中增加以下内容:
<!-- 配置多媒体文件解析器 -->
<!-- 文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartRes
olver">
<!-- 设置上传文件的最大尺寸为 10MB 1024*1024*10 -->
<property name="maxUploadSize">
<value>10485760</value>
</property>
</bean>
3.5 增加 upload.jsp 文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<jsp:include page="/static/common.jsp"></jsp:include>
<title>MWS Admin - Form Elements</title>
</head>
<body>
<%@ include file="head.jsp"%>
<div id="mws-wrapper">
<div id="mws-sidebar-stitch"></div>
<div id="mws-sidebar-bg"></div>
<%@ include file="nav.jsp"%>
<div id="mws-container" class="clearfix">
<div class="container">
<div class="mws-report-container clearfix">
</div>
<div class="mws-panel grid_4">
<div class="mws-panel-header">
<span class="mws-i-24 i-eyedropper">Custom Inputs</span>
</div>
<div class="mws-panel-body">
<form class="mws-form" action="${pageContext.request.contextPath}/upload/test" method="post" enctype="multipart/form-data">
<div class="mws-form-inline">
<div class="mws-form-row">
<label>文件上传</label>
<div class="mws-form-item large">
<input type="file" name="file" class="mws-textinput" />
</div>
</div>
<div class="mws-form-row">
<div class="mws-form-item large">
<input type="submit" value="上传" class="mws-button black">
</div>
</div>
<div class="mws-form-row">
<div class="mws-form-item large">
${url}
</div>
</div>
</div>
</form>
</div>
</div>
<jsp:include page="foot.jsp"></jsp:include>
</div>
</div>
</div>
</body>
</html>
3.6 编写 controller
package cn.yuechenc.ycshop.manager.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import cn.yuechenc.fastdfs.client.FastDFSClient;
@Controller
@RequestMapping("/test/")
public class UploadFileController {
@RequestMapping("toupload")
public String toupload() {
return "upload";
}
@RequestMapping("upload")
public String upload(Model model, @RequestParam("file") MultipartFile file) throws Exception {
FastDFSClient dfs = new FastDFSClient();
String filename = file.getOriginalFilename();
String extName = filename.substring(filename.lastIndexOf(".") + 1);
String url = dfs.uploadFile(file.getBytes(), extName);
System.out.println(url);
model.addAttribute("url", url);
return "upload";
}
}
3.7 测试
1、 启动 tyh-manager-web 项目
2、 访问 http://127.0.0.1:8080/test/toupload 进入到上传页面
4、 测试访问文件
访问路径: http://47.100.224.4/group1/M00/00/00/rBOr_1zrfK6AVMITAA6mV1UTzD0929.png
如果能够浏览到图片, 则说明我们的文件上传, 读取都成功了!!!!