在微架构的系统之中,如果要保存上传图片内容,肯定要保存在图片服务器之中,因为如果你直接保存在web项目里面,那么该图片只有一个web容器可以看见,并且在集群之中容器之间的数据也没法进行共享,那么久需要有一个图片服务器,本次使用fastdfs的图片服务器完成整体的图片保存。
1.如果要想使用图片服务器,则首先一定要进行一些依赖包的配置,修改pom.xml配置文件,追加fastdfs依赖支持包:
com.github.kischang
fastdfs-client
0.1
完整pom
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4.0.0
cn.mldn
microboot
0.0.1-SNAPSHOT
microboot-upload
microboot-upload
http://maven.apache.org
UTF-8
com.github.kischang
fastdfs-client
0.1
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-jetty
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
junit
junit
test
org.springframework
springloaded
org.springframework.boot
spring-boot-devtools
2.随后由于该图片服务器有token认证处理过程,所以一定要保证有认证的配置文件,在"src/main/resources"目录之中创建"fastdfs_client.conf"文件
# 配置上你tracker的连接地址
tracker_server=fastdfs-tracker:22122
# 表示现在要进行token检测
http.anti_steal_token=true
# 进行认证的密码处理
http.secret_key=mldnhelloworldhahheihanhheiehinajueduicaibudao
3.修改控制器中的上传处理操作
microboot/pom.xml
**/*.properties
**/*.yml
**/*.xml
**/*.tld
**/*.p12
**/*.conf
完整pom
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
cn.mldn
microboot
0.0.1-SNAPSHOT
pom
microboot
http://maven.apache.org
1.8
UTF-8
org.springframework.boot
spring-boot-dependencies
1.5.4.RELEASE
pom
import
microboot
src/main/resources
**/*.properties
**/*.yml
**/*.xml
**/*.tld
**/*.p12
**/*.conf
false
src/main/java
**/*.properties
**/*.xml
**/*.tld
false
src/main/view
**/*.*
false
org.apache.maven.plugins
maven-compiler-plugin
${jdk.version}
${jdk.version}
${project.build.sourceEncoding}
org.springframework.boot
spring-boot-maven-plugin
cn.mldn.microboot.StartSpringBootMain
repackage
microboot-base
microboot-advance
microboot-error
microboot-tomcat
microboot-thymeleaf
microboot-upload
package cn.mldn.microboot.controller;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ProtoCommon;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import cn.mldn.microboot.util.controller.AbstractBaseController;
@Controller
public class UploadController extends AbstractBaseController {
@RequestMapping(value = "/uploadPre", method = RequestMethod.GET)
public String uploadPre() { // 通过model可以实现内容的传递
return "upload_page";
}
@RequestMapping(value = "/show", method = RequestMethod.GET)
public String show(String groupId, String fileId,Model model) throws Exception {
// 通过ClassPath路径获取要使用的配置文件
ClassPathResource classPathResource = new ClassPathResource(
"fastdfs_client.conf");
// 进行客户端访问的整体配置,需要知道配置文件的完整路径
ClientGlobal.init(classPathResource.getClassLoader()
.getResource("fastdfs_client.conf").getPath());
// FastDFS的核心操作在于tracker处理上,所以此时需要定义Tracker客户端
TrackerClient trackerClient = new TrackerClient();
// 定义TrackerServer的配置信息
TrackerServer trackerServer = trackerClient.getConnection();
int ts = (int) (System.currentTimeMillis() / 1000);// 时间参考
StringBuffer fileUrl = new StringBuffer();
fileUrl.append("http://");
fileUrl.append("fastdfs-tracker");
fileUrl.append("/" + groupId + "/").append(fileId);
fileUrl.append("?token=").append(
ProtoCommon.getToken(fileId, ts, ClientGlobal.g_secret_key));
fileUrl.append("&ts=").append(ts);
System.out.println(fileUrl);
trackerServer.close();
model.addAttribute("image", fileUrl) ;
return "upload_show" ;
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String upload(String name, HttpServletRequest request)
throws Exception {
if (request instanceof MultipartHttpServletRequest) { // 如果你现在是MultipartHttpServletRequest对象
MultipartHttpServletRequest mrequest = (MultipartHttpServletRequest) request;
List files = mrequest.getFiles("photo");
Iterator iter = files.iterator();
while (iter.hasNext()) {
MultipartFile photo = iter.next();
if (photo != null) { // 现在有文件上传
// 如果要想进行上传则一定需要获取到文件的扩展名称
String fileExtName = photo.getContentType().substring(
photo.getContentType().lastIndexOf("/") + 1);
// 通过ClassPath路径获取要使用的配置文件
ClassPathResource classPathResource = new ClassPathResource(
"fastdfs_client.conf");
// 进行客户端访问的整体配置,需要知道配置文件的完整路径
ClientGlobal.init(classPathResource.getClassLoader()
.getResource("fastdfs_client.conf").getPath());
// FastDFS的核心操作在于tracker处理上,所以此时需要定义Tracker客户端
TrackerClient trackerClient = new TrackerClient();
// 定义TrackerServer的配置信息
TrackerServer trackerServer = trackerClient.getConnection();
// 在整个FastDFS之中真正负责干活的就是Storage
StorageServer storageServer = null;
StorageClient1 storageClient = new StorageClient1(
trackerServer, storageServer);
// 定义上传文件的元数据
NameValuePair[] metaList = new NameValuePair[3];
metaList[0] = new NameValuePair("fileName",
photo.getOriginalFilename());
metaList[1] = new NameValuePair("fileExtName", fileExtName);
metaList[2] = new NameValuePair("fileLength",
String.valueOf(photo.getSize()));
// 如果要上传则使用trackerClient对象完成
String upload_file = storageClient.upload_file1(
photo.getBytes(), fileExtName, metaList);
System.out.println(upload_file);
trackerServer.close();
}
}
}
return "upload-file";
}
}
4.设置图片显示页面:
upload_show.html
SpringBoot模版渲染5.每次显示图片的时候设置好groupId和fileId就可以实现图片的查看处理:
upload_page.html
SpringBoot模版渲染姓名:
照片:
照片:
照片:
查看照片
查看照片
查看照片
如果传递的内容是字符串一定要使用"'"声明,如果是数字则可以直接编写。