我为我的英语道歉。
我使用以下代码将文件从 ftp 主机下载到平板电脑。当我使用 Wi-Fi 时,一切正常。但是当我尝试使用移动互联网下载文件时,下载会停止,但并非总是如此,有时会正常完成。我发现停止总是在“retrieveFile”或“注销”,程序到了这些命令就没有继续下去了,只是站着,数据传输的图标不闪烁,停止随机发生。我尝试使用不同的移动运营商,但没有区别。可能是什么原因?
还有一个问题,但不是那么重要,我还没有找到如何获取文件大小,并使用了我的决定,也许还有另一种获取文件大小的方法?
private void downloadFile(final String url, final String Message, final String Message2, final Uri uri) { final ProgressDialog progressDialog = new ProgressDialog(this); new AsyncTask() { 私有异常 m_error = null;
@Override protected void onPreExecute() { progressDialog.setMessage(Message); progressDialog.setCancelable(true); progressDialog.setMax(100); progressDialog .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.show(); } @Override protected File doInBackground(String... params) { FileOutputStream fos = null; File file = null; FTPClient client = null; try{ client = new FTPClient(); client.connect(ftp_host,Integer.parseInt(ftp_port)); client.login(ftp_user, ftp_password); client.enterLocalPassiveMode(); client.setFileType(FTP.BINARY_FILE_TYPE); String stat = ""; if (url.equals("MobiTrade.apk")){ client.changeWorkingDirectory("/var/srv/home/user/mobitrade/update/");} file = new File(Environment.getExternalStorageDirectory(), "/MobiTrade/update/MobiTrade.apk");} stat = client.getStatus("/var/srv/home/user/mobitrade/update/MobiTrade.apk"); else { client.changeWorkingDirectory("/var/srv/home/user/mobitrade/"+number+"/out/"); file = new File(Environment.getExternalStorageDirectory(), "/MobiTrade/in/"+url); if (url.equals("message.csv")) file.delete(); stat = client.getStatus("/var/srv/home/user/mobitrade/"+number+"/out/"+url); } final Integer FileSize; if (stat.length() >= 64) { stat = stat.substring(49,64); stat = stat.trim(); FileSize = Integer.parseInt(stat); } else { FileSize = 0; } fos = new FileOutputStream(file); CountingOutputStream cos = new CountingOutputStream(fos){ protected void beforeWrite(int n){ super.beforeWrite(n); publishProgress(getCount(), FileSize); } }; if (url.equals("MobiTrade.apk")){ client.retrieveFile("/var/srv/home/user/mobitrade/update/MobiTrade.apk", cos); } else { client.retrieveFile("/var/srv/home/user/mobitrade/"+number+"/out/"+url, cos); } if (url.equals("message.csv")){ client.deleteFile("/var/srv/home/user/mobitrade/"+number+"/out/"+url); } client.logout(); } catch (Exception e){ e.printStackTrace(); } finally{ try{ if (fos != null) fos.close(); if (client.isConnected()) { client.disconnect(); } } catch (IOException e){ e.printStackTrace(); } } return file; } protected void onProgressUpdate(Integer... values) { progressDialog .setProgress((int) ((values[0] / (float) values[1]) * 100)); }; @Override protected void onPostExecute(File result) { if (m_error != null) { m_error.printStackTrace(); return; } progressDialog.hide(); if (url.equals("settings.csv")) ProcessSettings(url); else if (url.equals("MobiTrade.apk")) ProcessUpdate(); else ProcessData(url, Message2, uri); } }.execute(url); }
任何帮助都是不言而喻的。