1:添加spring-data-redis1.7.1相关jar及测试代码
2:添加fastdfs的相关jar及测试代码 3:修改某些系统文件中项目名称为maat_service(有些系统配置文件里面的项目名称没有改过来)
This commit is contained in:
80
src/main/java/com/nis/web/service/fdfs/FastDFSFile.java
Normal file
80
src/main/java/com/nis/web/service/fdfs/FastDFSFile.java
Normal file
@@ -0,0 +1,80 @@
|
||||
package com.nis.web.service.fdfs;
|
||||
|
||||
/**
|
||||
* <p>Title: FastDFSFile.java</p>
|
||||
* <p>Description: </p>
|
||||
* <p>Company: IIE</p>
|
||||
* @author rkg
|
||||
* @date 2018年5月16日
|
||||
*
|
||||
*/
|
||||
|
||||
public class FastDFSFile extends FileManagerConfig {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private byte[] content;
|
||||
private String name;
|
||||
private String ext;
|
||||
private String length;
|
||||
private String author = FILE_DEFAULT_AUTHOR;
|
||||
|
||||
public FastDFSFile(byte[] content, String ext) {
|
||||
this.content = content;
|
||||
this.ext = ext;
|
||||
}
|
||||
|
||||
public FastDFSFile(byte[] content, String name, String ext) {
|
||||
this.content = content;
|
||||
this.name = name;
|
||||
this.ext = ext;
|
||||
}
|
||||
|
||||
public FastDFSFile(byte[] content, String name, String ext, String length, String author) {
|
||||
this.content = content;
|
||||
this.name = name;
|
||||
this.ext = ext;
|
||||
this.length = length;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public byte[] getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(byte[] content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getExt() {
|
||||
return ext;
|
||||
}
|
||||
|
||||
public void setExt(String ext) {
|
||||
this.ext = ext;
|
||||
}
|
||||
|
||||
public String getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public void setLength(String length) {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
}
|
||||
108
src/main/java/com/nis/web/service/fdfs/FileManager.java
Normal file
108
src/main/java/com/nis/web/service/fdfs/FileManager.java
Normal file
@@ -0,0 +1,108 @@
|
||||
package com.nis.web.service.fdfs;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.csource.common.NameValuePair;
|
||||
import org.csource.fastdfs.ClientGlobal;
|
||||
import org.csource.fastdfs.StorageClient;
|
||||
import org.csource.fastdfs.StorageServer;
|
||||
import org.csource.fastdfs.TrackerClient;
|
||||
import org.csource.fastdfs.TrackerServer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
/**
|
||||
* <p>Title: FileManager.java</p>
|
||||
* <p>Description: </p>
|
||||
* <p>Company: IIE</p>
|
||||
* @author rkg
|
||||
* @date 2018年5月16日
|
||||
*
|
||||
*/
|
||||
|
||||
public class FileManager extends FileManagerConfig {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static TrackerClient trackerClient;
|
||||
private static TrackerServer trackerServer;
|
||||
private static StorageServer storageServer;
|
||||
private static StorageClient storageClient;
|
||||
|
||||
static {
|
||||
try {
|
||||
String classPath = new File(FileManager.class.getResource("/").getFile()).getCanonicalPath();
|
||||
String fdfsClientConfigFilePath = classPath + File.separator + CLIENT_CONFIG_FILE;
|
||||
ClientGlobal.init(fdfsClientConfigFilePath);
|
||||
trackerClient = new TrackerClient();
|
||||
trackerServer = trackerClient.getConnection();
|
||||
storageClient = new StorageClient(trackerServer, storageServer);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向fastdfs上传文件
|
||||
* @param file
|
||||
* @param valuePairs
|
||||
* @return
|
||||
*/
|
||||
public static String upload(FastDFSFile file, NameValuePair[] valuePairs) {
|
||||
String[] uploadResults = null;
|
||||
try {
|
||||
// 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 fileAbsolutePath = PROTOCOL + TRACKER_NGNIX_ADDR
|
||||
// + trackerServer.getInetSocketAddress().getHostName()
|
||||
// + SEPARATOR + TRACKER_NGNIX_PORT
|
||||
+ SEPARATOR + groupName + SEPARATOR + remoteFileName;
|
||||
return fileAbsolutePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从fastdfs下载文件
|
||||
* @param groupName
|
||||
* @param remoteFileName
|
||||
* @param specFileName
|
||||
* @return
|
||||
*/
|
||||
public static ResponseEntity<byte[]> download(String groupName, String remoteFileName, String specFileName) {
|
||||
byte[] content = null;
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
try {
|
||||
content = storageClient.download_file(groupName, remoteFileName);
|
||||
headers.setContentDispositionFormData("attachment",
|
||||
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();
|
||||
}
|
||||
return new ResponseEntity<byte[]>(content, headers, HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据组和文件名删除文件
|
||||
* @param group 文件所属组
|
||||
* @param filePath 文件存放路径,不包含group,例如文件url是http://10.0.6.192/group1/M00/00/01/CgAGwFr786aAegOQAADz3NN2rlY283.jpg,那么filePath=/M00/00/01/CgAGwFr786aAegOQAADz3NN2rlY283.jpg
|
||||
* @return 返回0代表成功,其他则失败
|
||||
*/
|
||||
public static Integer delete_file(String group, String filePath) {
|
||||
Integer result = -1;
|
||||
try {
|
||||
result = storageClient.delete_file(group, filePath);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.nis.web.service.fdfs;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* <p>Title: FileManagerConfig.java</p>
|
||||
* <p>Description: </p>
|
||||
* <p>Company: IIE</p>
|
||||
* @author rkg
|
||||
* @date 2018年5月16日
|
||||
*
|
||||
*/
|
||||
|
||||
public class FileManagerConfig implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final String FILE_DEFAULT_AUTHOR = "renkaige";
|
||||
|
||||
public static final String PROTOCOL = "http://";
|
||||
|
||||
public static final String SEPARATOR = "/";
|
||||
|
||||
public static final String TRACKER_NGNIX_ADDR = "10.0.6.192";
|
||||
|
||||
public static final String TRACKER_NGNIX_PORT = "";
|
||||
|
||||
public static final String CLIENT_CONFIG_FILE = "fdfs_client.conf";
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.nis.web.service.restful;
|
||||
|
||||
/**
|
||||
*
|
||||
* <p>Title: TestRedisService</p>
|
||||
* <p>Description: 测试redis事务</p>
|
||||
* <p>Company: IIE</p>
|
||||
* @author rkg
|
||||
* @date 2018年5月17日
|
||||
*
|
||||
*/
|
||||
public interface TestRedisService {
|
||||
public void saveConfigCompile();
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.nis.web.service.restful;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.nis.datasource.DynamicJedisDataBase;
|
||||
import com.nis.web.dao.impl.BaseRedisDao;
|
||||
|
||||
@Service("testRedisServiceimpl")
|
||||
public class TestRedisServiceimpl extends BaseRedisDao<String, String> implements TestRedisService {
|
||||
@Transactional
|
||||
public void saveConfigCompile() {
|
||||
DynamicJedisDataBase.setRedisDataBase(2, redisTemplate);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
redisTemplate.boundZSetOps("1").add("a" + i, i);
|
||||
if (i == 5) {
|
||||
// int a = 1 / 0;
|
||||
}
|
||||
}
|
||||
// 切换redis数据库
|
||||
DynamicJedisDataBase.setRedisDataBase(3, redisTemplate);
|
||||
for (int i = 10; i < 20; i++) {
|
||||
redisTemplate.boundZSetOps("1").add("a" + i, i);
|
||||
if (i == 5) {
|
||||
// int a = 1 / 0;
|
||||
}
|
||||
}
|
||||
DynamicJedisDataBase.setRedisDataBase(4, redisTemplate);
|
||||
for (int i = 20; i < 30; i++) {
|
||||
redisTemplate.boundZSetOps("1").add("a" + i, i);
|
||||
if (i == 25) {
|
||||
int a = 1 / 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user