0
我使用Apache和Async在FTP上傳文件。問題在於,在大文件(即200 Mb)上,進度條重新啓動其進度,但上傳仍在繼續(我從計算機上檢查FTP)。進度條重新啓動
這是我看到的logcat:
1->2->3->4->5->6->7->8-> -8 -> -7 .... to 8 and starts again
異步後臺代碼:
con = new FTPClient();
con.setConnectTimeout(300000);
con.setDataTimeout(300000);
// con.setKeepAlive(true);
con.connect("host IP",21);
if (con.login("user", "password"))
{
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
boolean directoryExists = con.changeWorkingDirectory(Email);
File CloudVersionSysForRestore = new File(getFilesDir()+File.separator+"Cloud Version.itb");
try {
CloudVersionSysForRestore.createNewFile();
OutputStream OutstreamCloud = new FileOutputStream(new File(CloudVersionSysForRestore.getPath()));
con.retrieveFile("/"+Email+"/Cloud Version.itb", OutstreamCloud);
OutstreamCloud.close();
if(x!=2){
BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(getFilesDir()+File.separator+"Cloud Version.itb")));
String read;
StringBuilder builder = new StringBuilder("");
while((read = bufferedReader.readLine()) != null) {
Log.w("READ", read);
builder.append(read);
}
bufferedReader.close();
long getintfromstring = Long.parseLong(builder.toString());
if(getintfromstring<Version){
Log.w("Version", String.valueOf(Version));
try{
/************************************ UPLOAD + Progress ****************************/
long fileSize = DBToUploadFromInternalStorage.length();
int sentBytes = 0;
InputStream inputStream = new FileInputStream(DBToUploadFromInternalStorage);
System.out.println("Start uploading second file");
OutputStream outputStream = con.storeFileStream("PilotLogbookDB.itb");
byte[] bytesIn = new byte[4 * 1024];
int read1 = 0;
while ((read1 = inputStream.read(bytesIn)) !=-1) {
outputStream.write(bytesIn, 0, read1);
sentBytes+=read1;
final int progress = (int) ((sentBytes * 100)/fileSize);
runOnUiThread(new Runnable() {
public void run() {
pd.setProgress(progress);
Log.w("Progress", String.valueOf(progress));
}
});
}
outputStream.close();
inputStream.close();
/************************************ UPLOAD + Progress ****************************/
}
catch(Exception e){
e.printStackTrace();
x=2;
}
boolean completed = con.completePendingCommand();
對於龍是這樣嗎?
/************************************ UPLOAD + Progress ****************************/
long fileSize = DBToUploadFromInternalStorage.length();
long sentBytes = 0;
InputStream inputStream = new FileInputStream(DBToUploadFromInternalStorage);
System.out.println("Start uploading second file");
OutputStream outputStream = con.storeFileStream("PilotLogbookDB.itb");
byte[] bytesIn = new byte[4 * 1024];
int read1 = 0;
while ((read1 = inputStream.read(bytesIn)) !=-1) {
outputStream.write(bytesIn, 0, read1);
sentBytes+=read1;
final long progress = (long) ((sentBytes * 100)/fileSize);
runOnUiThread(new Runnable() {
public void run() {
pd.setProgress(Long.bitCount(progress));
Log.w("Progress", String.valueOf(progress));
}
});
}
outputStream.close();
inputStream.close();
/************************************ UPLOAD + Progress ****************************/
我加了問題長版...可以這樣嗎? – Iosif
不,在我寫的(關於Long)是錯誤的,它應該是: pd.setProgress((int)progress); – Iosif
'Long.bitCount(progress)'不是我想要的。由於你的大小現在是'long',所以你解決了這個問題,現在'(int)((sentBytes * 100)/ fileSize)'給你一個介於0到100之間的適當整數。如果你適合int,你可以使用'final int progress =(int)((sentBytes * 100)/ fileSize)'。 – dst