Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中从ftp服务器下载和上传xml文件?_Android - Fatal编程技术网

如何在android中从ftp服务器下载和上传xml文件?

如何在android中从ftp服务器下载和上传xml文件?,android,Android,大家好,, 我对android比较熟悉,我有一个代码,用于在建立ftp连接时从ftp服务器下载和上载xml文件。它出现异常:-java.io.IOException:无法连接到服务器:无法检索文件:550,但该代码在java中运行良好,但android它抛出了上述异常。请帮助我 **Xml File** <uses-permission android:name="android.permission.INTERNET" /> **Java Class** i

大家好,, 我对android比较熟悉,我有一个代码,用于在建立ftp连接时从ftp服务器下载和上载xml文件。它出现异常:-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, i=image (binary) 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." );
          }
       }
**Xml文件**
**Java类**
imgBtnSyncRes.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
File objfile=新文件(getFilesDir().getAbsolutePath()+File.separator+“/index.xml”);
试一试{
objfile.createNewFile();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
试一试{
下载(“ftp.qualityinaction.net/QIA/Questions/Airlines/”、“qualityinaction.net”、“CpQual35”、“index.xml”、objfile);
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
});
}
//从ftp下载文件
公共无效下载(字符串strftpServer、字符串struser、字符串strpassword、,
字符串strfileName,文件strdestination)引发格式错误的异常,
IOException
{
if(strftpServer!=null&&strfileName!=null&&strdestination!=null)
{
StringBuffer strb=新的StringBuffer(“ftp://”);
//检查身份验证,否则假定其匿名访问。
if(struser!=null&&strpassword!=null)
{
strb.append(struser);
strb.append(“:”);
strb.append(strpassword);
strb.append('@');
}
追加(strftpServer);
strb.append('/');
strb.append(strfileName);
/*
*类型==>a=ASCII模式,i=图像(二进制)模式,d=文件目录
*上市
*/
strb.append(“;type=i”);
//OpenHttpConnection(strb.toString());
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
尝试
{
URL=新URL(strb.toString());
URLConnection urlc=(URLConnection)url.openConnection();
//urlc.setDoOutput(真);
//connect();
//connect();
bis=新的BufferedInputStream(urlc.getInputStream());
bos=新BufferedOutputStream(新文件输出流(
strdestination.getName());
int i;
而((i=bis.read())!=-1)
{
bos.write(i);
}
}
最后
{
如果(bis!=null)
尝试
{
二、关闭();
}
捕获(ioe异常ioe)
{
ioe.printStackTrace();
}
如果(bos!=null)
尝试
{
bos.close();
}
捕获(ioe异常ioe)
{
ioe.printStackTrace();
}
}
}
其他的
{
System.out.println(“输入不可用”);
}
}
//文件上传到FTP
公共无效上载(字符串ftpServer、字符串用户、字符串密码、,
字符串文件名、文件源)引发错误的DurLexException,
IOException
{
if(ftpServer!=null&&fileName!=null&&source!=null)
{
StringBuffer sb=新的StringBuffer(“http://”);
//检查身份验证,否则假定其匿名访问。
if(user!=null&&password!=null)
{
某人附加(用户);
某人附加(“:”);
某人附加(密码);
某人附加('@');
}
sb.append(ftpServer);
某人附加(“/”);
sb.append(文件名);
/*
*类型==>a=ASCII模式,i=图像(二进制)模式,d=文件目录
*上市
*/
sb.追加(“;type=i”);
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
尝试
{
URL=新URL(sb.toString());
URLConnection urlc=url.openConnection();
bos=新的BufferedOutputStream(urlc.getOutputStream());
bis=新的BufferedInputStream(新的FileInputStream(源));
int i;
//逐字节读取,直到流结束
而((i=bis.read())!=-1)
{
bos.write(i);
}
}
最后
{
如果(bis!=null)
尝试
{
二、关闭();
}
捕获(ioe异常ioe)
{
ioe.printStackTrace();
}
如果(bos!=null)
尝试
{
bos.close();
}
捕获(ioe异常ioe)
{
ioe.printStackTrace();
}