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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python爬取指定微博话题下的内容,保存为txt
    • ¥15 vue2登录调用后端接口如何实现
    • ¥65 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?