为fastdfs工具类添加注释

This commit is contained in:
RenKaiGe-Office
2018-05-17 18:01:03 +08:00
parent 5ecb09dd0f
commit d825c96145
5 changed files with 45 additions and 32 deletions

View File

@@ -43,7 +43,7 @@ public class FastDFSController {
String groupName=filePath.substring(0,filePath.indexOf("/"));
filePath=filePath.substring(filePath.indexOf("/")+1);
Integer a = FileManager.delete_file(groupName, filePath);
System.out.println(a);
//System.out.println(a);
}
public String testAdd() {
@@ -56,7 +56,7 @@ public class FastDFSController {
meta_list[1] = new NameValuePair("fileLength", String.valueOf(file.length()));
meta_list[2] = new NameValuePair("fileExt", ext);
meta_list[3] = new NameValuePair("fileAuthor", "rkg");
String successFilePath = FileManager.upload(new FastDFSFile(getByte(file), ext), null);
String successFilePath = FileManager.upload(new FastDFSFile(getByte(file),fileName, ext), meta_list);
System.out.println("上传成功返回的路径是" + successFilePath);
return successFilePath;
}
@@ -80,7 +80,7 @@ public class FastDFSController {
public String add(MultipartFile attach, HttpServletRequest request) throws IOException, MyException {
// 获取文件后缀名
String ext = attach.getOriginalFilename().substring(attach.getOriginalFilename().lastIndexOf(".") + 1);
FastDFSFile file = new FastDFSFile(attach.getBytes(), ext);
FastDFSFile file = new FastDFSFile(attach.getBytes(),attach.getOriginalFilename(), ext);
NameValuePair[] meta_list = new NameValuePair[4];
meta_list[0] = new NameValuePair("fileName", attach.getOriginalFilename());
meta_list[1] = new NameValuePair("fileLength", String.valueOf(attach.getSize()));

View File

@@ -2,7 +2,7 @@ package com.nis.web.service.fdfs;
/**
* <p>Title: FastDFSFile.java</p>
* <p>Description: </p>
* <p>Description: file对象</p>
* <p>Company: IIE</p>
* @author rkg
* @date 2018年5月16日
@@ -18,11 +18,22 @@ public class FastDFSFile extends FileManagerConfig {
private String length;
private String author = FILE_DEFAULT_AUTHOR;
public FastDFSFile(byte[] content, String ext) {
this.content = content;
this.ext = ext;
}
/**
* 根据byte数组和文件后缀构建FastDFSFile对象
* @param content 文件转化为byte
* @param ext 文件后缀名
*/
// public FastDFSFile(byte[] content, String ext) {
// this.content = content;
// this.ext = ext;
// }
/**
* 根据byte数组和文件后缀构建FastDFSFile对象
* @param content 文件转化为byte
* @param name 文件名称
* @param ext 文件后缀名
*/
public FastDFSFile(byte[] content, String name, String ext) {
this.content = content;
this.name = name;

View File

@@ -8,6 +8,8 @@ import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -15,7 +17,7 @@ import org.springframework.http.ResponseEntity;
/**
* <p>Title: FileManager.java</p>
* <p>Description: </p>
// * <p>Description: 上传文件到fdfs的工具类</p>
* <p>Company: IIE</p>
* @author rkg
* @date 2018年5月16日
@@ -25,6 +27,7 @@ import org.springframework.http.ResponseEntity;
public class FileManager extends FileManagerConfig {
private static final long serialVersionUID = 1L;
private static Logger logger = LoggerFactory.getLogger(FileManager.class);
private static TrackerClient trackerClient;
private static TrackerServer trackerServer;
private static StorageServer storageServer;
@@ -39,15 +42,15 @@ public class FileManager extends FileManagerConfig {
trackerServer = trackerClient.getConnection();
storageClient = new StorageClient(trackerServer, storageServer);
} catch (Exception e) {
e.printStackTrace();
logger.error("创建tracker|storage失败,请检查配置文件或fdfs服务,nginx服务是否正常", e);
}
}
/**
* 向fastdfs上传文件
* @param file
* @param valuePairs
* @return
* @param file 文件对象
* @param valuePairs 设置meta信息,这个可以为空,设置了好像也没有什么用
* @return 返回文件在fdfs上的http访问地址(可直接在浏览器下载),null代表上传失败
*/
public static String upload(FastDFSFile file, NameValuePair[] valuePairs) {
String[] uploadResults = null;
@@ -55,24 +58,24 @@ public class FileManager extends FileManagerConfig {
// uploadResults = storageClient.upload_file(file.getContent(), file.getExt(),
// valuePairs);
uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), valuePairs);
} catch (Exception e) {
e.printStackTrace();
}
String groupName = uploadResults[0];
String remoteFileName = uploadResults[1];
String groupName = uploadResults[0];// group名称:group1|group2...
String remoteFileName = uploadResults[1];// 文件路径:M00/00/01/CgAGwFr9LTiAEoSaAADz3NN2rlY365.jpg
String fileAbsolutePath = PROTOCOL + TRACKER_NGNIX_ADDR
// + trackerServer.getInetSocketAddress().getHostName()
// + SEPARATOR + TRACKER_NGNIX_PORT
+ SEPARATOR + groupName + SEPARATOR + remoteFileName;
return fileAbsolutePath;
} catch (Exception e) {
logger.error("上传文件{}到fastfds服务器失败,请检查配置文件或fdfs服务,nginx服务是否正常", file.getName(), e);
}
return null;
}
/**
* 从fastdfs下载文件
* @param groupName
* @param remoteFileName
* @param specFileName
* @param groupName 文件所属组:group1
* @param remoteFileName 文件路径:M00/00/01/CgAGwFr9LriANfj6AADz3NN2rlY448.jpg
* @param specFileName 重命名文件:对CgAGwFr9LriANfj6AADz3NN2rlY448.jpg重命名,例如重命名为:1.jpg
* @return
*/
public static ResponseEntity<byte[]> download(String groupName, String remoteFileName, String specFileName) {
@@ -84,8 +87,7 @@ public class FileManager extends FileManagerConfig {
new String(specFileName.getBytes("UTF-8"), "iso-8859-1"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("从fastfds服务器下载文件{}失败,请检查配置文件或fdfs服务,nginx服务是否正常", remoteFileName, e);
}
return new ResponseEntity<byte[]>(content, headers, HttpStatus.CREATED);
}
@@ -101,7 +103,7 @@ public class FileManager extends FileManagerConfig {
try {
result = storageClient.delete_file(group, filePath);
} catch (Exception e) {
e.printStackTrace();
logger.error("删除文件:{}失败,所属组:{},请检查配置文件或fdfs服务,nginx服务是否正常", filePath, group, e);
}
return result;
}

View File

@@ -15,12 +15,12 @@ public class FileManagerConfig implements Serializable {
private static final long serialVersionUID = 1L;
public static final String FILE_DEFAULT_AUTHOR = "renkaige";
public static final String FILE_DEFAULT_AUTHOR = "iie-rkg";
public static final String PROTOCOL = "http://";
public static final String SEPARATOR = "/";
// fdfs-tracker-nginx服务器
public static final String TRACKER_NGNIX_ADDR = "10.0.6.192";
public static final String TRACKER_NGNIX_PORT = "";

View File

@@ -15,7 +15,7 @@
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"
default-lazy-init="true">
<!-- 加载配置文件 -->
<!-- 加载配置文件,不知道为啥不能加载redis.properties的内容,先把redis.properties里面的内容放到jdbc.properties里面吧 -->
<context:property-placeholder
ignore-unresolvable="true" location="classpath:jdbc.properties" />