- 论坛徽章:
- 0
|
在下载图片时,报错:java.io.IOException: recv package size -1 != 10。求各位大佬帮忙!!!!!!代码如下:
public class FastDFSUtil {
private static Logger LOGGER = LoggerFactory.getLogger(FastDFSUtil.class);
private static String classPath = FastDFSUtil.class.getProtectionDomain()
.getCodeSource().getLocation().getPath();
private static String FASTDFS_CLIENT_CONF = classPath + "fdfs_client.conf";
private static TrackerClient trackerClient = null;
private static TrackerServer trackerServer = null;
private static StorageClient storageClient = null;
private static StorageServer storageServer = null;
// 默认下载路径
private static String DOWNLOAD_PATH = "D:/test_fastDFS/新建文件夹/download1";
private FastDFSUtil() {
FastDFSUtil.init();
}
private static void init() {
try {
ClientGlobal.init(FASTDFS_CLIENT_CONF);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = trackerClient.getStoreStorage(trackerServer);
storageClient = new StorageClient(trackerServer, storageServer);
} catch (Exception e) {
throw new RuntimeException("init exception");
}
}
public static FastDFSUtil getInstance() {
return FastDFSUtilHolder.INSTANCE;
}
private static class FastDFSUtilHolder {
private static final FastDFSUtil INSTANCE = new FastDFSUtil();
}
public static void download(String group, String file, String storePath) {
boolean istrue=true;
do{
try {
byte[] data = storageClient.download_file(group, file);
if (null != data){
istrue=false;
}
File savefile = new File(storePath);
if (!savefile.exists()) {
savefile.mkdirs();
}
IOUtils.write(data, new FileOutputStream(new File(storePath + "/"
+ file.substring(file.lastIndexOf("/") + 1))));
LOGGER.info("~~download success~~");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("download exception");
}
}while(istrue);
}
}
测试类:
public class TestFastDFS {
public static String MAYDAY_IMG = "D:/Documents/Pictures/test2.jpg";
public static String BASE_DOWNLOAD_PATH = "D:/test_fastDFS/新建文件夹/download2";
public static void main(String[] args) {
NameValuePair[] metaDataList = new NameValuePair[] {
new NameValuePair("width", "100"),
new NameValuePair("height", "100") };
//NameValuePair[] metaDataList = null ;
FastDFSUtil.getInstance();
// download group2/M00/00/00/wKgI21uNe1OAQpaiAAAlV5hH__M065.jpg
FastDFSUtil.download("group1",
"M00/00/00/wKgI2VuNfBmANpG6AAB7lbxNbKo940.jpg",
BASE_DOWNLOAD_PATH);
}
}
下载图片时,总是报错
报错信息如下:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
java.io.IOException: recv package size -1 != 10
at org.csource.fastdfs.ProtoCommon.recvHeader(ProtoCommon.java:169)
at org.csource.fastdfs.ProtoCommon.recvPackage(ProtoCommon.java:201)
at org.csource.fastdfs.StorageClient.download_file(StorageClient.java:1125)
at org.csource.fastdfs.StorageClient.download_file(StorageClient.java:1105)
at com.sml.sz.fastdfs.FastDFSUtil.download(FastDFSUtil.java:153)
at com.sml.sz.fastdfs.TestFastDFS.main(TestFastDFS.java:30)
Exception in thread "main" java.lang.RuntimeException: download exception
at com.sml.sz.fastdfs.FastDFSUtil.download(FastDFSUtil.java:167)
at com.sml.sz.fastdfs.TestFastDFS.main(TestFastDFS.java:30)
|
|