开发者

How to download and upload xml files from ftp server in android?

开发者 https://www.devze.com 2023-03-10 14:21 出处:网络
Hi Everyone, I\'m fresher to android, i have an code for downloading and uploading xml file from ftp server while establishing connection with ftp it is giving exception:- java.io.IOException: Unable

Hi Everyone, I'm fresher to android, i have an code for downloading and uploading xml file from ftp server while establishing connection with ftp it is giving exception:- java.io.IOException: Unable to connect to server: Unable to retrieve file: 550, but this code well working in java but android it is throwing above exception please help me.

  **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." );
          }
       }
0

精彩评论

暂无评论...
验证码 换一张
取 消