作者iori9100 (呵呵)
看板AndroidDev
标题[问题] 在FTP下载档案~失败了?找不到PORT号?
时间Wed Aug 2 23:33:01 2017
这个方法也是我参考某些前辈来修改的~
但只要执行下载
java.io.IOException: Unable to connect to server: Unable to configure data
port
at libcore.net.url.FtpURLConnection.connect(FtpURLConnection.java:203)
at
com.example.user.ftpupload.MainActivity$DownloadTask.
doInBackground(MainActivity.java:172)
connection.connect();这行会挂 可是我查了相关的实作~
我在URL那边的网址字串是可以的 但一直连不进去~
请问有大大可以指正我一下吗? 因为查了不少文章 都是跟HTTP的范例有关的
还是我FTP写的网址是错误的呢?? 感恩
private void StartDownload(){
String url="ftp://user:
[email protected]:21/spanish.pdf";
new DownloadTask().execute(url);
}
@Override
@Deprecated
protected Dialog onCreateDialog(int id){
switch (id){
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading...");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
class DownloadTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... params) {
int count;
try {
URL url= new URL(params[0]);
URLConnection connection = url.openConnection();
@@@@@@@@@@@@@@@ connection.connect();挂在这行@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
int filelength=connection.getContentLength();
Log.d("长度", "doInBackground: "+filelength);
InputStream input= new BufferedInputStream(url.openStream());
OutputStream output= new
FileOutputStream(android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/lesson");
byte data[]= new byte[1024];
int total=0;
while((count=input.read(data))!=-1) {
total += count;
publishProgress("" + (int) ((total * 100) / filelength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected void onProgressUpdate(String... values) {
Log.d("HEHEHEHE", values[0]);
mProgressDialog.setProgress(Integer.parseInt(values[0]));
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.163.99.157
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/AndroidDev/M.1501687984.A.F56.html
1F:→ cf1064: 在. connect之前,params[0]有值? 08/03 00:54
2F:→ lovelycateye: 你手机是用4G连还是跟你电脑同个WiFi? 08/03 11:50
3F:→ lovelycateye: 那看起来是内网IP,先确认你是连到跟电脑相同WiFi吧 08/03 11:50
4F:→ iori9100: 给上面前辈,我是用模拟器的,网路应该是在同网域,另外 08/03 17:56
5F:→ iori9100: param[0]得到的值是 执行方法宣告的网址 08/03 17:56
6F:→ ssccg: 用实机试试看 08/03 18:09
7F:→ lovelycateye: 我用 Unable to configure data port 还不少啊 08/04 00:42
9F:→ iori9100: 其实下载档案OK 但我是不知道怎麽把过程丢给进度条..@@ 08/04 13:17
10F:→ iori9100: 类似用上面那个网址 先把PORT号那些参数都设定好就可以 08/04 13:18