public interface ImageServer { }
上传文件实现:ImageServerImpl
import java.io.File;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;
import java.net.InetSocketAddress;
import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.StorageClient1;
/** * 上传文件
* @param file 文件 * @return 文件存储路径 * @throws IOException * @throws Exception */
public String uploadFile(File file) throws IOException, Exception ; /** * 上传文件
* @param file 文件 * @param name 文件名称 * @return 文件存储路径 * @throws IOException * @throws Exception */
public String uploadFile(File file, String name) throws IOException, /** * 上传文件
* @param fileBuff 二进制数组 * @param name * @return */
public String uploadFile(byte[] fileBuff,String name) throws
Exception ;
IOException, Exception;
import org.csource.fastdfs.TrackerGroup;
import com.imageserver.pool.ConnectionPool;
public class ImageServerImpl implements ImageServer {
private String send(byte[] fileBuff, String fileExtName)
throws IOException, Exception { String upPath = null;
StorageClient1 client1 = null; try {
client1 = ConnectionPool.getPoolInstance().checkout(10); upPath = client1.upload_file1(fileBuff, fileExtName, null); ConnectionPool.getPoolInstance().checkin(client1); //确实没有空闲连接,并不需要删除与fastdfs连接 throw e;
//发生io异常等其它异常,默认删除这次连接重新申请
public String uploadFile(byte[] fileBuff, String name) throws }
Exception {
IOException,
String fileExtName = getFileExtName(name); return send(fileBuff, fileExtName);
public String uploadFile(File file, String name) throws IOException, }
Exception {
byte[] fileBuff = getFileBuffer(file); String fileExtName = getFileExtName(name); return send(fileBuff, fileExtName);
public String uploadFile(File file) throws IOException, Exception { }
return uploadFile(file, file.getName()); public ImageServerImpl() { }
init();
private final static String tgStr = \; private final static int port = 22122;
} catch (InterruptedException e) {
} catch (Exception e) {
}
}
}
ConnectionPool.getPoolInstance().drop(client1); e.printStackTrace(); throw e;
return upPath;
private String getFileExtName(String name) { }
private byte[] getFileBuffer(File file) { }
private void init() { }
InetSocketAddress[] trackerServers = new InetSocketAddress[1]; trackerServers[0] = new InetSocketAddress(tgStr, port); ClientGlobal.setG_tracker_group(new // 连接超时的时限,单位为毫秒
ClientGlobal.setG_connect_timeout(2000); // 网络超时的时限,单位为毫秒
ClientGlobal.setG_network_timeout(30000); ClientGlobal.setG_anti_steal_token(false); // 字符集
ClientGlobal.setG_charset(\); ClientGlobal.setG_secret_key(null); byte[] fileByte = null; try { }
return fileByte;
FileInputStream fis = new FileInputStream(file); fileByte = new byte[fis.available()]; fis.read(fileByte); e.printStackTrace(); e.printStackTrace(); String extName = null;
if (name != null && name.contains(\)) { }
return extName;
extName = name.substring(name.lastIndexOf(\) + 1);
} catch (FileNotFoundException e) { } catch (IOException e) {
TrackerGroup(trackerServers));
上传文件测试类:ImageServerTest
import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
public class ImageServerTest {
public void statTime() {
if (count.getAndIncrement() == 9) { public static void main(String[] args) { }
stime = System.currentTimeMillis(); new ImageServerTest().mutiUP(); static long stime = 0;
ImageServer s = new ImageServerImpl(); AtomicInteger count = new AtomicInteger();
System.out.println(\用时:\ + (System.currentTimeMillis() - stime));
public void mutiUP() {
for (int i = 0; i < 1; i++) {
new Thread() {
@Override
public void run() {
try {
for (int j = 0; j < 10; j++) { }
statTime();
e.printStackTrace();
String p = s.uploadFile(new System.out.println(p);
}
}
File(\));
} catch (IOException e) {
}
}
}
}
} catch (Exception e) { }
e.printStackTrace();
}.start();