package com.basic.security.utils;
|
|
import android.content.ContextWrapper;
|
|
import com.basic.security.base.BaseApplication;
|
|
import org.csource.common.NameValuePair;
|
import org.csource.fastdfs.ClientGlobal;
|
import org.csource.fastdfs.StorageClient1;
|
import org.csource.fastdfs.StorageServer;
|
import org.csource.fastdfs.TrackerClient;
|
import org.csource.fastdfs.TrackerServer;
|
|
import java.io.BufferedInputStream;
|
import java.io.BufferedOutputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileNotFoundException;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.net.URL;
|
import java.util.Properties;
|
|
/**
|
* FastDFS文件上传,下载处理类
|
*
|
* @author sjw
|
*/
|
public class FastDFSUtil {
|
private static final String TAG = "FastDFSUtil";
|
|
public static void testUpload(String filePath, String type) {
|
try {
|
/*
|
* 上传测试
|
*/
|
String fdfs_client_path = new ContextWrapper(BaseApplication.getApplication().activity).getFilesDir().getAbsolutePath() + "/fdfs_client.properties";
|
ClientGlobal.init(fdfs_client_path);
|
fdfs_client_path = filePath;
|
byte[] by = getBytes(fdfs_client_path);
|
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length());
|
String resultPath = uploadFile(by, fileName, type);
|
} catch (Exception e) {
|
ExceptionUtil.printException(e);
|
}
|
}
|
|
public static String upLoadVisitorPic(byte[] data, long time) {
|
try {
|
String fast_dfs_client_path = new ContextWrapper(BaseApplication.getApplication().activity).getFilesDir().getAbsolutePath() + "/fdfs_client.properties";
|
ClientGlobal.init(fast_dfs_client_path);
|
String fileName = DateUtil.formatTime(time, "yyyy-MM-dd HH:mm:ss");
|
return uploadFile(data, fileName, "jpg");
|
} catch (Exception e) {
|
System1.out.println("FastDFSUtil.upLoadVisitorPic " + e.getMessage());
|
return null;
|
}
|
}
|
|
public static byte[] getBytes(String filePath) {
|
byte[] buffer = null;
|
try {
|
File file = new File(filePath);
|
FileInputStream fis = new FileInputStream(file);
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
|
byte[] b = new byte[1000];
|
int n;
|
while ((n = fis.read(b)) != -1) {
|
bos.write(b, 0, n);
|
}
|
fis.close();
|
bos.close();
|
buffer = bos.toByteArray();
|
} catch (FileNotFoundException e) {
|
ExceptionUtil.printException(e);
|
} catch (IOException e) {
|
ExceptionUtil.printException(e);
|
}
|
return buffer;
|
}
|
|
/**
|
* 文件上传
|
*
|
* @param pic 上传文件字节数组
|
* @param name 文件名
|
* @param ext 文件后缀名
|
* @return FastDFS存储文件路径
|
*/
|
public static String uploadFile(byte[] pic, String name, String ext) {
|
String path;
|
TrackerServer trackerServer = null;
|
try {
|
if (pic.length / 1024 / 1024 > 10) {
|
throw new RuntimeException(TAG + "仅支持10MB以下文件上传!");
|
}
|
String fdfs_client_path = new ContextWrapper(BaseApplication.getApplication().activity).getFilesDir().getAbsolutePath();
|
ClientGlobal.init(fdfs_client_path + "/fdfs_client.properties");
|
TrackerClient trackerClient = new TrackerClient();
|
trackerServer = trackerClient.getConnection();
|
StorageServer storageServer = null;
|
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
|
NameValuePair[] meta_list = new NameValuePair[3];
|
meta_list[0] = new NameValuePair("filename", name);
|
meta_list[1] = new NameValuePair("fileext", ext);
|
meta_list[2] = new NameValuePair("filesize", String.valueOf(pic.length));
|
path = storageClient1.upload_file1(pic, ext, meta_list);
|
if (null != path && !"".equals(path)) {
|
return path;
|
} else {
|
throw new RuntimeException(TAG + "文件上传失败!");
|
}
|
} catch (Exception e) {
|
System1.out.println("FastDFSUtil.uploadFile " + e.getMessage());
|
} finally {
|
if (trackerServer != null) {
|
try {
|
trackerServer.close();
|
} catch (IOException e) {
|
ExceptionUtil.printException(e);
|
}
|
}
|
}
|
return null;
|
}
|
|
public static String downFile(String fileUrl) {
|
if (null == fileUrl || "".equals(fileUrl)) {
|
return null;
|
}
|
BufferedInputStream dis = null;
|
BufferedOutputStream fos = null;
|
String filePath;
|
try {
|
filePath = System.getProperty("user.home") + File.separator
|
+ fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
|
File destFile = new File(filePath);
|
if (!destFile.exists()) {
|
destFile.createNewFile();
|
}
|
URL url = new URL(getInfo("http_head") + getInfo("fastdfs_vip")
|
+ fileUrl);
|
dis = new BufferedInputStream(url.openStream());
|
fos = new BufferedOutputStream(new FileOutputStream(destFile));
|
byte[] buff = new byte[2048];
|
int bytesRead;
|
while (-1 != (bytesRead = dis.read(buff, 0, buff.length))) {
|
fos.write(buff, 0, bytesRead);
|
}
|
} catch (Exception e) {
|
return null;
|
} finally {
|
if (dis != null) {
|
try {
|
dis.close();
|
} catch (IOException e) {
|
ExceptionUtil.printException(e);
|
}
|
}
|
if (fos != null) {
|
try {
|
fos.close();
|
} catch (IOException e) {
|
ExceptionUtil.printException(e);
|
}
|
}
|
}
|
return filePath;
|
}
|
|
public static String getInfo(String code) throws Exception {
|
ClassLoader classLoader = FastDFSUtil.class.getClassLoader();
|
InputStream is = classLoader
|
.getResourceAsStream("config/fdfs_client.conf");
|
if (is == null) {
|
is = classLoader.getResourceAsStream("fdfs_client.properties");
|
}
|
Properties prop = new Properties();
|
if (is != null) {
|
prop.load(is);
|
return prop.getProperty(code);
|
}
|
return code;
|
}
|
}
|