恢复文件传输时出现奇怪的现象。
看下面的图片你会看到坏的部分。
这显然是随机发生的,可能每 10 次发生一次。
我通过 ftp 将图片从我的 Android 手机发送到 java 服务器。
我忘记了什么。
我看到连接由于 java.net.SocketTimeoutException:
而被终止
传输正在恢复,如下所示
Resume at : 287609 Sending 976 bytes more
当文件完全接收时,字节始终是正确的。 即使是下面的图片。
不知道从哪里开始调试它,因为它大多数时候都在工作。
任何建议或想法都会很高兴,我想我完全错过了这里的一些东西。
设备发送者代码(仅发送循环):
int count = 1;
//Sending N files, looping N times
while(count <= max) {
String sPath = batchFiles.get(count-1);
fis = new FileInputStream(new File(sPath));
int fileSize = bis.available();
out.writeInt(fileSize); // size
String nextReply = in.readUTF();
// if the file exist,
if(nextReply.equals(Consts.SERVER_give_me_next)){
count++;
continue;
}
long resumeLong = 0; // skip this many bytes
int val = 0;
buffer = new byte[1024];
if(nextReply.equals(Consts.SERVER_file_exist)){
resumeLong = in.readLong();
}
//UPDATE FOR @Justin Breitfeller, Thanks
long skiip = bis.skip(resumeLong);
if(resumeLong != -1){
if(!(resumeLong == skiip)){
Log.d(TAG, "ERROR skip is not the same as resumeLong ");
skiip = bis.skip(resumeLong);
if(!(resumeLong == skiip)){
Log.d(TAG, "ERROR ABORTING skip is not the same as resumeLong);
return;
}
}
}
while ((val = bis.read(buffer, 0, 1024)) > 0) {
out.write(buffer, 0, val);
fileSize -= val;
if (fileSize < 1024) {
val = (int) fileSize;
}
}
reply = in.readUTF();
if (reply.equals(Consts.SERVER_file_receieved_ok)) {
// check if all files are sent
if(count == max){
break;
}
}
count++;
}
接收器代码(非常截断):
//receiving N files, looping N times
while(count < totalNrOfFiles){
int ii = in.readInt(); // File size
fileSize = (long)ii;
String filePath = Consts.SERVER_DRIVE + Consts.PTPP_FILETRANSFER;
filePath = filePath.concat(theBatch.getFileName(count));
File path = new File(filePath);
boolean resume = false;
//if the file exist. Skip if done or resume if not
if(path.exists()){
if(path.length() == fileSize){ // Does the file has same size
logger.info("File size same skipping file:" + theBatch.getFileName(count) );
count++;
out.writeUTF(Consts.SERVER_give_me_next);
continue; // file is OK don't upload it again
}else {
// Resume the upload
out.writeUTF(Consts.SERVER_file_exist);
out.writeLong(path.length());
resume = true;
fileSize = fileSize-path.length();
logger.info("Resume at : " + path.length() +
" Sending "+ fileSize +" bytes more");
}
}else
out.writeUTF("lets go");
byte[] buffer = new byte[1024];
// ***********************************
// RECEIVE FROM PHONE
// ***********************************
int size = 1024;
int val = 0;
bos = new BufferedOutputStream(new FileOutputStream(path,resume));
if(fileSize < size){
size = (int) fileSize;
}
while (fileSize >0) {
val = in.read(buffer, 0, size);
bos.write(buffer, 0, val);
fileSize -= val;
if (fileSize < size)
size = (int) fileSize;
}
bos.flush();
bos.close();
out.writeUTF("file received ok");
count++;
}
最佳答案
发现了错误,问题是我的逻辑不好。 不多说了。
我发送的图片在发送之前正在调整大小。
问题出在传输失败后简历启动时
未使用调整大小的图片,而是使用原始代码
图中的尺寸更大。
我现在已经设置了一个短暂的缓存来保存调整大小的临时图片。
鉴于我制作的应用程序的复杂性,我只是忘记了恢复期间的文件与原始文件不同。
关于java - Android到电脑FTP断点上传奇怪现象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8871725/