package com.basic.analy.utils; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.Date; import java.util.UUID; //import org.csource.common.NameValuePair; //import org.csource.fastdfs.ClientGlobal; //import org.csource.fastdfs.FileInfo; //import org.csource.fastdfs.StorageClient; //import org.csource.fastdfs.StorageClient1; //import org.csource.fastdfs.StorageServer; //import org.csource.fastdfs.TrackerClient; //import org.csource.fastdfs.TrackerServer; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.web.multipart.MultipartFile; // //@Configuration //public class FastDFSUtil { // // //fdfs_client 核心配置文件 // public static String conf_filename="fdfs_client.conf"; // // public static void main(String[] args) { // // String realPath ="C:/Users/Basic/Pictures/Saved Pictures/131431199804020315.jpg"; // // download(); // /M00/00/18/wKgBtlvrelCAQCN2AAAlu9rpxmA717.jpg // } // // // // // 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 FileInfos 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); // //获取文件信息,并保存至数据库 // FileInfos fileInfo = new FileInfos(); // fileInfo.setId(UUID.randomUUID().toString()); // fileInfo.setExtName(extName); // fileInfo.setPath(fileIds[0]+"/"+fileIds[1]); // fileInfo.setCreateTime(new Date()); // // return fileInfo; // } catch (Exception e) { // e.printStackTrace(); // return null; // } // // } //}