java端调用fastdfs存储文件的工具方法之一(针对单台追踪器的)

import java.io.IOException;

import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

public class fastdfsUtil {
	private TrackerClient tClient;
	private TrackerServer tServer;
	private StorageClient1 sClient;
	
	/**
	 * 
	 * @param path 配置文件的名字  (在配置文件中配追踪器的IP和端口号:tracker_server=192.168.101.131:22122)
	 */
	public fastdfsUtil(String path){
		try {
			ClientGlobal.init(fastdfsUtil.class.getResource("/").getPath()+path);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MyException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param file 
	 * @param host
	 * @return
	 */
	public String upload(Multipartfile file,String host){
		try {
			//截取后缀
			int lastindex = file.getOriginalFilename().lastIndexOf(".");
			String houzui = file.getOriginalFilename().substring(lastindex+1);
			
			tClient = new TrackerClient();
			tServer = tClient.getConnection();
			
			sClient = new StorageClient1(tServer,null);
			String upload_file1 = sClient.upload_file1(file.getBytes(), houzui, null);
			return host+"/"+upload_file1;
			} catch (Exception e) {
			e.printStackTrace();
			} 
		return null;
	}
}

猜你喜欢

转载自blog.csdn.net/futao127/article/details/80558192