package cn.kgc.core.util; import org.apache.commons.io.FilenameUtils; 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; public class FastDFSClient { private TrackerClient trackerClient = null; private TrackerServer trackerServer = null; private StorageServer storageServer = null; private StorageClient1 storageClient = null; public FastDFSClient(String conf) throws Exception { if (conf.contains("classpath:")) { = conf.replace the conf ( "CLASSPATH:."., this.getClass () the getResource ( "/") getPath ()); } ClientGlobal.init (the conf); trackerClient new new TrackerClient = (); trackerServer trackerClient.getConnection = ( ); storageserver = null; StorageClient = new new StorageClient1 (trackerServer, storageserver); } / ** * upload method * <P> the Title: the uploadFile </ P> * <P> the Description: </ P> * @param fileName file The full path * @param extName file extension does not include (.) * @param Metas information file extension * @return * @throws Exception * / public String the uploadFile (fileName String, String extname, of NameValuePairs [] Metas) throws Exception { String = storageClient.upload_file1 the Result (fileName, extname, Metas); return the Result; } // 这个比较好用 public String uploadFile(byte[] file, String fileName, long fileSize) throws Exception { NameValuePair[] metas = new NameValuePair[3]; metas[0] = new NameValuePair("fileName", fileName); metas[1] = new NameValuePair("fileSize", String.valueOf(fileSize)); metas[2] = new NameValuePair("fileExt", FilenameUtils.getExtension(fileName)); String result = storageClient.upload_file1(file, FilenameUtils.getExtension(fileName), metas); return result; } public String uploadFile(String fileName) throws Exception { return uploadFile(fileName, null, null); } String the uploadFile public (fileName String, String extname) throws Exception { return the uploadFile (fileName, extname, null); } / ** * upload method * <P> the Title: the uploadFile </ P> * <P> the Description: </ P> * @param fileContent content file, a byte array * @param extName file extension * @param metas file extent information * @return * @throws Exception * / public String the uploadFile (byte [] fileContent, String extname, of NameValuePairs [ ] Metas) throws Exception { String Result = storageClient.upload_file1 (fileContent, extname, Metas); return Result; } public String the uploadFile (byte [] fileContent) throws Exception { return the uploadFile ( fileContent, null, null); } public String the uploadFile (byte [] fileContent, String extname) throws Exception { return the uploadFile (fileContent, extname, null); } }
Tools on the Common (common module) below