FastDFS client併發上傳異常 recv cmd: 0 is not correct, expect cmd: 100
阿新 • • 發佈:2018-12-20
描述:在呼叫同一個介面,併發上傳檔案的時候出現異常。
java.io.IOException: recv cmd: 0 is not correct, expect cmd: 100 at org.csource.fastdfs.ProtoCommon.recvHeader(ProtoCommon.java:173) at org.csource.fastdfs.ProtoCommon.recvPackage(ProtoCommon.java:201) at org.csource.fastdfs.StorageClient.do_upload_file(StorageClient.java:714) at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:162) at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:180) at org.csource.fastdfs.StorageClient1.upload_file1(StorageClient1.java:103) at com.xxx.school.util.FastDFSUtils.uploadFile(FastDFSUtils.java:113) at com.xxx.school.file.service.impl.FileServiceImpl.createOneFile(FileServiceImpl.java:100) at com.xxx.school.resource.controller.ResourceFileBockUpload.fileBockUpload(ResourceFileBockUpload.java:116) at sun.reflect.GeneratedMethodAccessor232.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
原因: 有一個FastDFSUtil類,裡面的trackerServer,storageServer,StorageClient1成員是共享的。
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
解決方案:每次呼叫上傳介面的時候重新獲取下新連線
TrackerServer trackerServer = trackerClient.getConnection(); if (null == trackerServer) { return null; } StorageServer storageServer = trackerClient.getStoreStorage(trackerServer); if (null == storageServer) { return null; } StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer); if (storageClient1 == null){ return null; } String upload_file1 = storageClient1.upload_file1(buff, fileName.substring(fileName.lastIndexOf(".") + 1), nameValuePairs);