读书人

android ftp 下载有关问题

发布时间: 2012-05-21 18:04:41 作者: rapoo

android ftp 下载问题
package lemote.test.mid.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import lemote.test.mid.properties.DefaultValue;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;

import android.os.Environment;

public class FtpUnit {
private FTPClient ftpClient = null;
private String SDPATH;
public FtpUnit(){
SDPATH =Environment.getExternalStorageDirectory()+"/";
}

/**
* 连接Ftp服务器
*/
public void connectServer(){
if(ftpClient == null){
int reply;
try{
ftpClient = new FTPClient();
ftpClient.setDefaultPort(21);
ftpClient.configure(getFtpConfig());
ftpClient.connect("172.16.18.175");
ftpClient.login("anonymous","");
ftpClient.setDefaultPort(21);
reply = ftpClient.getReplyCode();
System.out.println(reply+"----");
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.err.println("FTP server refused connection.");
}
ftpClient.enterLocalPassiveMode();
ftpClient.setControlEncoding("gbk");
}catch(Exception e){
e.printStackTrace();
}
}
}

/**
* 上传文件
* @param localFilePath--本地文件路径
* @param newFileName--新的文件名
*/
public void uploadFile(String localFilePath,String newFileName){
connectServer();
//上传文件
BufferedInputStream buffIn=null;
try{
buffIn=new BufferedInputStream(new FileInputStream(SDPATH+"/"+localFilePath));
System.out.println(SDPATH+"/"+localFilePath);
System.out.println("start="+System.currentTimeMillis());
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.storeFile("a1.mp3", buffIn);
System.out.println("end="+System.currentTimeMillis());
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(buffIn!=null)
buffIn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}

/**
* 下载文件
* @param remoteFileName --服务器上的文件名
* @param localFileName--本地文件名
*/
public void loadFile(String remoteFileName,String localFileName){
connectServer();
System.out.println("==============="+localFileName);
//下载文件
BufferedOutputStream buffOut=null;


try{
buffOut=new BufferedOutputStream(new FileOutputStream(SDPATH+localFileName));
long start = System.currentTimeMillis();
ftpClient.retrieveFile(remoteFileName, buffOut);
long end = System.currentTimeMillis();
System.out.println(end-start);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(buffOut!=null)
buffOut.close();
}catch(Exception e){
e.printStackTrace();
}
}
}

/**
* 设置FTP客服端的配置--一般可以不设置
* @return
*/
private static FTPClientConfig getFtpConfig(){
FTPClientConfig ftpConfig=new FTPClientConfig(FTPClientConfig.SYST_UNIX);
ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING);
return ftpConfig;
}

/**
* 关闭连接
*/
public void closeConnect(){
try{
if(ftpClient!=null){
ftpClient.logout();
ftpClient.disconnect();
}
}catch(Exception e){
e.printStackTrace();
}
}

}


这是我的ftp上传下载的代码,在java程序中完全正常使用,但移到模拟器上,只能上传不能下载了,请大侠们给我看看,呵呵
WARN/System.err(317): org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
10-25 06:19:26.485: WARN/System.err(317): at org.apache.commons.net.io.Util.copyStream(Util.java:129)
10-25 06:19:26.485: WARN/System.err(317): at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:1286)
报的错误如下!

请问这是怎么回事啊?


[解决办法]
看错误类型,服务器的问题,不是android的错误…
[解决办法]
网络的问题或FTP服务器的问题,我曾经遇到过这个问题,本来想换个FTP服务器试试,后来直接换了一台服务器,问题就解决了,换的这台机器和那一台机器在同一个网段里,所以我觉得应该就是FTP服务器的问题了。
[解决办法]
代码应该没问题,应该是ftp服务器有问题吧。换台机器试试吧。
[解决办法]
网络或者服务器问题啊 这样的问题我也不到过 找了半天错 结果不是自己的原因 特恼火

读书人网 >Android

热点推荐