这个方法也是我参考某些前辈来修改的~
但只要执行下载
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]));
}
}