引用可happyfish100的官方fastdfs-java-client-master的包,用线程去进行压力测试;通过不了,有谁解决过类似的问题?
主类:
public class FastDFSTestPool {
public static void main(String[] args) throws IOException, MyException{
String local_filename = "D:\\smallData.xml";
FastDFSBean fb = new FastDFSBean();
fb.setFileName("TestFile");
fb.setFileExt("xml");
File file = new File(local_filename);
fb.setFile(file);
Resource resource = new FileSystemResource("d:\\fdfs_client.conf");
File file2 = resource.getFile();
String configFile = file2.getAbsolutePath();
ClientGlobal.init(configFile);
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(100);
for (int i = 0; i < 50; i++) {
fixedThreadPool.execute(new FastdfsThread(fb));
}
}
}
线程类:
public class FastdfsThread implements Runnable {
private FastDFSBean bean ;
public FastdfsThread(){
super();
}
public FastdfsThread(FastDFSBean bean1){
super();
this.bean = bean1;
}
@Override
public void run() {
try {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
for (int i = 0; i < 100000; i++) {
TrackerClient tracker = new TrackerClient();
trackerServer = tracker.getConnection();
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
NameValuePair nvp[] = new NameValuePair[] {
new NameValuePair("age", i+""),
new NameValuePair("sex", Thread.currentThread().getName()+i)
};
byte[] filebtyes = fileToBytes(this.bean.getFile());//bean为file的实体类
String fileIds[] = storageClient.upload_file(filebtyes, this.bean.getFileExt(), nvp);
System.out.println("文件数:"+i+" 组名:" + fileIds[0]+" 路径: " + fileIds[1]);
trackerServer.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
public byte[] fileToBytes(File file) {
byte[] buffer = null;
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
buffer = bos.toByteArray();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (null != bos) {
bos.close();
}
} catch (IOException ex) {
ex.printStackTrace();
} finally{
try {
if(null!=fis){
fis.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
return buffer;
}
}