Mar 22, 2007

Code snipet for downloading image file from a ftp server and display that image localy.

/*
* Author Akila Nonis
* /


setImgUrlValues();

//Setting corresponding values to be appended to URL
//URL url = new URL("ftp://"+user+":"+password+"@"+server+file+".jpg;type=i");

//Setting URL
URL url = new URL("ftp://"+imgUserName+":"+imgPassword+"@"+imgUrl+imgName+";type=i");

//Setting URL connection
URLConnection con = url.openConnection();

//Reading image file data from and generating a local copy

BufferedInputStream in = new BufferedInputStream(con.getInputStream());

FileOutputStream out = new FileOutputStream("C:\\Sign\\"+imgName);

int i = 0;
byte[] bytesIn = new byte[1024];

while ((i = in.read(bytesIn)) >= 0)
{
out.write(bytesIn, 0, i);
}

out.close();
in.close();

//-------------------------------------------------------------

//File remoteFile = new File(url.toString());
//if (remoteFile.exists())
// System.out.println("Remote file size = " + remoteFile.length() +" " + url.toURI());

//-------------------------------------------------------------

System.out.println("Local file size = " + localFile.length());

System.out.println("this is Image Name " + imgName);

//Setting the downloaded local image copy as the icon image
lblSignature.setIcon(new ImageIcon("C:\\Sign\\"+imgName));

1 comment:

Anonymous said...

Interesting to know.

Have a nice day!

Thanks for visiting my blog :)