如何解决如何从android中的ftp服务器下载和上传xml文件?
| 嗨,大家好, 我对Android较新,我有一个代码可用于从ftp服务器下载和上传xml文件,同时与ftp建立连接,它给出了异常:-java.io.IOException:无法连接至服务器:无法检索文件:550 ,但是这段代码在Java中却很好,但是在android上面却抛出了异常,请帮帮我。 **Xml File**
<uses-permission android:name=\"android.permission.INTERNET\" />
**Java Class**
imgBtnSyncRes.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
File objfile=new File(getFilesDir().getAbsolutePath()+ File.separator + \"/index.xml\");
try {
objfile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
download(\"ftp.qualityinaction.net/QIA/Questions/Airlines/\",\"qualityinaction.net\",\"CpQual35\",\"index.xml\",objfile);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
//File Downloading from ftp
public void download( String strftpServer,String struser,String strpassword,String strfileName,File strdestination ) throws MalformedURLException,IOException
{
if (strftpServer != null && strfileName != null && strdestination != null)
{
StringBuffer strb = new StringBuffer( \"ftp://\" );
// check for authentication else assume its anonymous access.
if (struser != null && strpassword != null)
{
strb.append( struser );
strb.append( \':\' );
strb.append( strpassword );
strb.append( \'@\' );
}
strb.append( strftpServer );
strb.append( \'/\' );
strb.append( strfileName );
/*
* type ==> a=ASCII mode,i=image (binary) mode,d= file directory
* listing
*/
strb.append( \";type=i\" );
//OpenHttpConnection(strb.toString());
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
URL url = new URL(strb.toString());
URLConnection urlc =(URLConnection) url.openConnection();
//urlc.setDoOutput(true);
//urlc.connect();
//urlc.connect();
bis = new BufferedInputStream( urlc.getInputStream() );
bos = new BufferedOutputStream( new FileOutputStream(
strdestination.getName() ) );
int i;
while ((i = bis.read()) != -1)
{
bos.write( i );
}
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
if (bos != null)
try
{
bos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
else
{
System.out.println( \"Input not available\" );
}
}
//File uploading to FTP
public void upload( String ftpServer,String user,String password,String fileName,File source ) throws MalformedURLException,IOException
{
if (ftpServer != null && fileName != null && source != null)
{
StringBuffer sb = new StringBuffer( \"http://\" );
// check for authentication else assume its anonymous access.
if (user != null && password != null)
{
sb.append( user );
sb.append( \':\' );
sb.append( password );
sb.append( \'@\' );
}
sb.append( ftpServer );
sb.append( \'/\' );
sb.append( fileName );
/*
* type ==> a=ASCII mode,d= file directory
* listing
*/
sb.append( \";type=i\" );
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
URL url = new URL( sb.toString() );
URLConnection urlc = url.openConnection();
bos = new BufferedOutputStream( urlc.getOutputStream() );
bis = new BufferedInputStream( new FileInputStream( source ) );
int i;
// read byte by byte until end of stream
while ((i = bis.read()) != -1)
{
bos.write( i );
}
}
finally
{
if (bis != null)
try
{
bis.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
if (bos != null)
try
{
bos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
else
{
System.out.println( \"Input not available.\" );
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。