package com.basic.analy.utils;
|
|
|
//import org.csource.common.NameValuePair;
|
//import org.csource.fastdfs.*;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
//
|
//@Configuration
|
//public class FastDFSUtil2 {
|
//
|
// //fdfs_client 核心配置文件
|
// public static String conf_filename="fdfs_client.conf";
|
//
|
//
|
// public String upload(MultipartFile file) { //上传文件
|
// TrackerServer trackerServer =null;
|
// StorageServer storageServer = null;
|
//
|
// try {
|
// ClientGlobal.init(conf_filename);
|
// TrackerClient tracker = new TrackerClient();
|
// trackerServer = tracker.getConnection();
|
//
|
// String ext_Name = file.getOriginalFilename().split("\\.")[1];
|
//
|
// byte[] bytes = null;
|
// try {
|
// bytes = file.getBytes();
|
// } catch (IOException e) {
|
// e.printStackTrace();
|
// }
|
//
|
// StorageClient storageClient = new StorageClient(trackerServer, storageServer);
|
// String fileIds[] = storageClient.upload_file(bytes, ext_Name, null);
|
// return fileIds[0]+"/"+fileIds[1];
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// return "error";
|
// }
|
//
|
// }
|
//
|
// public String uploadByByte(byte[] bytes,String extName) { //上传文件
|
// TrackerServer trackerServer =null;
|
// StorageServer storageServer = null;
|
//
|
// try {
|
// ClientGlobal.init(conf_filename);
|
// TrackerClient tracker = new TrackerClient();
|
// trackerServer = tracker.getConnection();
|
// StorageClient1 client = new StorageClient1(trackerServer, storageServer);
|
//
|
// StorageClient storageClient = new StorageClient(trackerServer, storageServer);
|
// String fileIds[] = storageClient.upload_file(bytes, extName, null);
|
// return fileIds[0]+"/"+fileIds[1];
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// return "error";
|
// }
|
//
|
// }
|
//
|
//
|
// public static void download() { //下载文件
|
// TrackerServer trackerServer =null;
|
// StorageServer storageServer = null;
|
//
|
// try {
|
// String groupName = "group1";
|
// String filePath = "/M00/00/18/wKgBtlvrelCAQCN2AAAlu9rpxmA717.jpg";
|
//// String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
|
// ClientGlobal.init(conf_filename);
|
//
|
// TrackerClient tracker = new TrackerClient();
|
// trackerServer = tracker.getConnection();
|
//
|
// StorageClient storageClient = new StorageClient(trackerServer, storageServer);
|
// byte[] bytes = storageClient.download_file(groupName, filePath);
|
//
|
// String storePath = "/Users/shenwei/Desktop/download.png";
|
// OutputStream out = new FileOutputStream(storePath);
|
// out.write(bytes);
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// } finally{
|
// try {
|
// if(null!=storageServer) storageServer.close();
|
// if(null!=trackerServer) trackerServer.close();
|
// } catch (IOException e) {
|
// // TODO Auto-generated catch block
|
// e.printStackTrace();
|
// }
|
// }
|
// }
|
//
|
//
|
// public void getFileInfo(){ //获取文件信息
|
// TrackerServer trackerServer =null;
|
// StorageServer storageServer = null;
|
//
|
// try {
|
// String groupName = "group1";
|
// String filePath = "M00/B6/ED/wKgBy1vYJrSAMlSiAACrc4GTmVg117.png";
|
// ClientGlobal.init(conf_filename);
|
//
|
// TrackerClient tracker = new TrackerClient();
|
// trackerServer = tracker.getConnection();
|
//
|
// StorageClient storageClient = new StorageClient(trackerServer, storageServer);
|
// FileInfo file = storageClient.get_file_info(groupName, filePath);
|
// System.out.println("ip--->"+file.getSourceIpAddr());
|
// System.out.println("文件大小--->"+file.getFileSize());
|
// System.out.println("文件上传时间--->"+file.getCreateTimestamp());
|
// System.out.println(file.getCrc32());
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// }
|
// }
|
//
|
//
|
// public void getFileMate(){ //获取文件的原数据类型
|
// TrackerServer trackerServer =null;
|
// StorageServer storageServer = null;
|
//
|
// try {
|
// String groupName = "group1";
|
// String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
|
// ClientGlobal.init(conf_filename);
|
//
|
// TrackerClient tracker = new TrackerClient();
|
// trackerServer = tracker.getConnection();
|
//
|
// StorageClient storageClient = new StorageClient(trackerServer,
|
// storageServer);
|
//
|
// //这个值是上传的时候指定的NameValuePair
|
// NameValuePair nvps [] = storageClient.get_metadata(groupName, filePath);
|
// if(null!=nvps && nvps.length>0){
|
// for(NameValuePair nvp : nvps){
|
// System.out.println(nvp.getName() + ":" + nvp.getValue());
|
// }
|
// }
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// }
|
// }
|
//
|
//
|
// public void delete(){ //删除文件
|
// TrackerServer trackerServer =null;
|
// StorageServer storageServer = null;
|
//
|
// try {
|
// String groupName = "group1";
|
// String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
|
// ClientGlobal.init(conf_filename);
|
//
|
// TrackerClient tracker = new TrackerClient();
|
// trackerServer = tracker.getConnection();
|
//
|
// StorageClient storageClient = new StorageClient(trackerServer,
|
// storageServer);
|
// int i = storageClient.delete_file(groupName, filePath);
|
// System.out.println( i==0 ? "删除成功" : "删除失败:"+i);
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// }
|
// }
|
//
|
// /**
|
// * 根据图片的FastDFS地址获得数据的字节流
|
// * @param strUrl 网络连接地址
|
// * @return
|
// */
|
// public byte[] getImageFromFDFS(String strUrl){
|
// try {
|
// URL url = new URL(strUrl);
|
// System.out.println("url是:"+strUrl);
|
// HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
// conn.setRequestMethod("GET");
|
// conn.setConnectTimeout(5 * 1000);
|
// InputStream inStream = conn.getInputStream();//获取从网络的输入流
|
// byte[] btImg = new byte[inStream.available()];
|
// inStream.read(btImg); //从输入流中得到图片的二进制数据
|
// return btImg;
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// }
|
// return null;
|
// }
|
//
|
// public static byte[] downloadFDFS(String group_name,String remote_filename) throws Exception {
|
// // 向项目中添加jar包
|
// // 创建一个配置文件配置tracker服务器地址
|
// //加载配置文件
|
// ClientGlobal.init("fdfs_client.conf");
|
//
|
// //创建一个tracker客户端对象
|
// TrackerClient trackerClient = new TrackerClient();
|
//
|
// //使用trackerclient连接trackersever对象
|
// TrackerServer trackerServer = trackerClient.getConnection();
|
//
|
// //创建一个straged server对象
|
// StorageServer storageServer = null;
|
//
|
// //创建一个storage client 对象
|
// StorageClient storageClient = new StorageClient(trackerServer, storageServer);
|
//
|
//
|
// byte[] download_file = storageClient.download_file(group_name,remote_filename);
|
// //upload_file 存储着访问路径
|
// return download_file;
|
// }
|
//}
|