qq584852076 2018-05-29 02:18
浏览 708
已结题

FastDFS JAVA socket Address already in use:connect

引用可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;
    }
}
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大