我在恢復文件傳輸時出現一個奇怪的現象。Android到電腦FTP恢復上傳奇怪現象
看看下面的圖片,你會看到壞的部分。
這種情況顯然是隨機的,也許每隔10:th。
我通過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++;
}
難道這是因爲我設置長resumeLong = 0;也許它應該是-1 – Erik 2012-01-15 19:23:51
你的代碼可以使用一些評論。嘗試讓發件人放棄每個其他文件的連接,然後看看接收者做了什麼。 – 2012-01-19 15:03:30
這就是我所做的測試。以各種方式殺死連接,但是我不能以這種方式重現它(它發生在它發生時)它就像設備一樣,在發送時,設備代碼BufferedInputStream.skip跳過確定,但仍有一些數字是錯誤的。上傳完成時,文件上的大小總是正確的。我在世界各地使用我的應用程序可能有200個設備,其中一些設備有問題。也許這是某種特定設備搞砸的東西。 – Erik 2012-01-19 19:33:12