Rajkumarreddy
Lurker
Hi Everybody,
I'm rajkumar this is my new thread in this forum, i'm new to android i'm facing problem connecting to ftp server for downloading and uploading xml files i used following code(see attachment) it will work fine in java but in android it is giving exception(505 error) unable to connected to ftp server i included all permissions in manifest files please help me.
Thank You,
**Java class**
//on button click
File objfile=new File(getFilesDir().getAbsolutePath()+ File.separator + "/index.xml");
download("ftp.qualityinaction.net/QIA/Questions/Airlines/","qualityinaction.net", "password","index.xml",objfile);
I'm rajkumar this is my new thread in this forum, i'm new to android i'm facing problem connecting to ftp server for downloading and uploading xml files i used following code(see attachment) it will work fine in java but in android it is giving exception(505 error) unable to connected to ftp server i included all permissions in manifest files please help me.
Thank You,
**Java class**
//on button click
File objfile=new File(getFilesDir().getAbsolutePath()+ File.separator + "/index.xml");
download("ftp.qualityinaction.net/QIA/Questions/Airlines/","qualityinaction.net", "password","index.xml",objfile);
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" );
//test(strb.toString());
//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." );
}
}
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" );
//test(strb.toString());
//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." );
}
}