FTP上传和下载在Android [英] FTP Upload and Download on Android
本文介绍了FTP上传和下载在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到这个错误后,我已经设置INTERNET_ACCESS等..
私有类AsyncUpload扩展的AsyncTask<字符串,太虚,太虚> {
公共无效ftpUpload(){
FtpClient的CON =新FtpClient的();
尝试
{
con.connect(ftp.194.90.81.149); //这里我收到异常
//例外是java.unknownhostexception
//java.net.UnknownHostException:无法解析主机ftp.194.90.81.149:没有与主机名关联的地址
如果(con.login(用户名,密码))
{
con.enterLocalPassiveMode();
字符串数据=测试数据;
ByteArrayInputStream的时间=新ByteArrayInputStream的(data.getBytes());
布尔结果= con.storeFile(/ test.jpg放在中);
附寄();
如果(结果)Log.v(上传结果,成功);
}
}
赶上(例外五)
{
e.printStackTrace();
}
尝试
{
con.logout();
con.disconnect();
}
赶上(IOException异常E)
{
e.printStackTrace();
}
}
@覆盖
保护无效doInBackground(字符串... PARAMS){
ftpUpload();
返回null;
}
}
这里的codeI另一部分已经测试,仍然收到异常
公共类FTPFactory {
私人FtpClient的_ftpClient = NULL;
公共布尔连接(字符串主机,用户名字符串,字符串密码,INT端口)抛出IOException异常
{
尝试 {
_ftpClient =新FtpClient的();
//连接主机
_ftpClient.connect(主机,端口);
//现在检查的答复code,若为阳性平均连接成功
如果(FT preply.isPositiveCompletion(_ftpClient.getReply code())){
//使用用户名和放大器登录;密码
布尔值状态= _ftpClient.login(用户名,密码);
返回状态;
}
}赶上(IOException异常E){
扔ê;
}
返回false;
}
公共布尔断开()
{
尝试 {
_ftpClient.logout();
_ftpClient.disconnect();
返回true;
}赶上(例外五){
}
返回false;
}
公共布尔ChangeDirectory(字符串目录路径)
{
尝试 {
_ftpClient.changeWorkingDirectory(目录);
}赶上(例外五){
}
返回false;
}
公共字符串GetCurrentWorkingDirectory()
{
尝试 {
串workingDir = _ftpClient.printWorkingDirectory();
返回workingDir;
}赶上(例外五){
}
返回null;
}
公共无效PrintFilesList(字符串dirPath)
{
尝试 {
FTPFile [] ftpFiles = _ftpClient.listFiles(dirPath);
INT长度= ftpFiles.length;
的for(int i = 0; I<长度;我++){
字符串名称= ftpFiles [I] .getName();
布尔ISFILE = ftpFiles [I] .isFile();
如果(ISFILE){
}
其他 {
}
}
}赶上(例外五){
e.printStackTrace();
}
}
公共布尔MakeDirectory(字符串newDirPath)
{
尝试 {
布尔值状态= _ftpClient.makeDirectory(newDirPath);
返回状态;
}赶上(例外五){
}
返回false;
}
公共布尔RemoveDirectory(字符串dirPath)
{
尝试 {
布尔值状态= _ftpClient.removeDirectory(dirPath);
返回状态;
}赶上(例外五){
}
返回false;
}
公共布尔RemoveFile(字符串文件路径)
{
尝试 {
布尔值状态= _ftpClient.deleteFile(文件路径);
返回状态;
}赶上(例外五){
e.printStackTrace();
}
返回false;
}
公共布尔RenameFile(从字符串,字符串)
{
尝试 {
布尔值状态= _ftpClient.rename(从,到);
返回状态;
}赶上(例外五){
}
返回false;
}
/ **
* mFTPClient:FTP客户端连接对象(见FTP连接例)
* srcFilePath:路径在FTP服务器上的源文件
* desFilePath:路径目标文件保存在SD卡
* /
公共布尔下载(字符串srcFilePath,字符串desFilePath)
{
布尔值状态= FALSE;
尝试 {
FileOutputStream中desFileStream =新的FileOutputStream(desFilePath);;
状态= _ftpClient.retrieveFile(srcFilePath,desFileStream);
desFileStream.close();
返回状态;
}赶上(例外五){
}
返回状态;
}
/ **
* mFTPClient:FTP客户端连接对象(见FTP连接例)
* srcFilePath:源文件中的SD卡路径
要被存储在FTP服务器文件名:* desFileName
* desDirectory:该文件应该被上传到目录路径
* /
公共布尔上传(字符串srcFilePath,字符串desFileName,字符串desDirectory)
{
布尔值状态= FALSE;
尝试 {
的FileInputStream srcFileStream =新的FileInputStream(srcFilePath);
//改变工作目录到目标目录
如果(ChangeDirectory(desDirectory)){
状态= _ftpClient.storeFile(desFileName,srcFileStream);
}
srcFileStream.close();
返回状态;
}赶上(例外五){
}
返回状态;
}
}
解决方案
con.connect(ftp.194.90.81.149); //这里我收到异常 //例外是java.unknownhostexception //java.net.UnknownHostException:无法解析主机ftp.194.90.81.149:没有与主机名关联的地址
这是您收到一个UnknownHostException的事实意味着ftp.194.90.81.149不在DNS中真正的主机名。我怀疑的是,数字部分是你真正想要的。即,尝试改变该行
con.connect(194.90.81.149);
I get this error after i have set INTERNET_ACCESS and etc...
private class AsyncUpload extends AsyncTask<String, Void, Void>{
public void ftpUpload(){
FTPClient con = new FTPClient();
try
{
con.connect("ftp.194.90.81.149"); //here i recieve exception
//the exception is java.unknownhostexception
//java.net.UnknownHostException: Unable to resolve host "ftp.194.90.81.149": No address associated with hostname
if (con.login("username", "password"))
{
con.enterLocalPassiveMode();
String data = "test data";
ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());
boolean result = con.storeFile("/test.jpg", in);
in.close();
if (result) Log.v("upload result", "succeeded");
}
}
catch (Exception e)
{
e.printStackTrace();
}
try
{
con.logout();
con.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
protected Void doInBackground(String... params) {
ftpUpload();
return null;
}
}
here is another part of code i have tested and still receive that exception
public class FTPFactory {
private FTPClient _ftpClient = null;
public boolean Connect(String host, String userName, String password, int port) throws IOException
{
try {
_ftpClient = new FTPClient();
// connecting to the host
_ftpClient.connect(host, port);
// now check the reply code, if positive mean connection success
if (FTPReply.isPositiveCompletion(_ftpClient.getReplyCode())) {
// login using username & password
boolean status = _ftpClient.login(userName, password);
return status;
}
} catch(IOException e) {
throw e;
}
return false;
}
public boolean Disconnect()
{
try {
_ftpClient.logout();
_ftpClient.disconnect();
return true;
} catch (Exception e) {
}
return false;
}
public boolean ChangeDirectory(String directoryPath)
{
try {
_ftpClient.changeWorkingDirectory(directoryPath);
} catch(Exception e) {
}
return false;
}
public String GetCurrentWorkingDirectory()
{
try {
String workingDir = _ftpClient.printWorkingDirectory();
return workingDir;
} catch(Exception e) {
}
return null;
}
public void PrintFilesList(String dirPath)
{
try {
FTPFile[] ftpFiles = _ftpClient.listFiles(dirPath);
int length = ftpFiles.length;
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
boolean isFile = ftpFiles[i].isFile();
if (isFile) {
}
else {
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
public boolean MakeDirectory(String newDirPath)
{
try {
boolean status = _ftpClient.makeDirectory(newDirPath);
return status;
} catch(Exception e) {
}
return false;
}
public boolean RemoveDirectory(String dirPath)
{
try {
boolean status = _ftpClient.removeDirectory(dirPath);
return status;
} catch(Exception e) {
}
return false;
}
public boolean RemoveFile(String filePath)
{
try {
boolean status = _ftpClient.deleteFile(filePath);
return status;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean RenameFile(String from, String to)
{
try {
boolean status = _ftpClient.rename(from, to);
return status;
} catch (Exception e) {
}
return false;
}
/**
* mFTPClient: FTP client connection object (see FTP connection example)
* srcFilePath: path to the source file in FTP server
* desFilePath: path to the destination file to be saved in sdcard
*/
public boolean Download(String srcFilePath, String desFilePath)
{
boolean status = false;
try {
FileOutputStream desFileStream = new FileOutputStream(desFilePath);;
status = _ftpClient.retrieveFile(srcFilePath, desFileStream);
desFileStream.close();
return status;
} catch (Exception e) {
}
return status;
}
/**
* mFTPClient: FTP client connection object (see FTP connection example)
* srcFilePath: source file path in sdcard
* desFileName: file name to be stored in FTP server
* desDirectory: directory path where the file should be upload to
*/
public boolean Upload(String srcFilePath, String desFileName, String desDirectory)
{
boolean status = false;
try {
FileInputStream srcFileStream = new FileInputStream(srcFilePath);
// change working directory to the destination directory
if (ChangeDirectory(desDirectory)) {
status = _ftpClient.storeFile(desFileName, srcFileStream);
}
srcFileStream.close();
return status;
} catch (Exception e) {
}
return status;
}
}
解决方案
con.connect("ftp.194.90.81.149"); //here i recieve exception //the exception is java.unknownhostexception //java.net.UnknownHostException: Unable to resolve host "ftp.194.90.81.149": No address associated with hostname
The fact that you're receiving an UnknownHostException means that ftp.194.90.81.149 isn't a real hostname in DNS. I'd suspect that the numeric part of that is what you really want. I.e, try changing that line to
con.connect("194.90.81.149");
这篇关于FTP上传和下载在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文