File Upload and Download is
always a handy utility to know. There will be some need to upload a file to an
FTP server, like if you generate a report or store some data in .xls file, then
it needs to be uploaded to a FTP server for further use. Likewise we need to
download some data (data stored in.xls files) for manipulation from the server
in our projects. Here we have the code to do this for us. The File Upload/Download
utility.
This file has the two methods
one for upload a file to the FTP server and the other one for downloading the
file from the FTP server. This program is written in very simple and easy strategy
to upload or download the files using Java.
It is using BufferedOutputStream and BufferedInputStream IO classes. This program can be
effectively reuse for your purpose in case if you want to upload or download
the files from your website or your project work. If you like the program and
feels it is very useful for you, please write comment and let us know if there
is any improvement needed in the code.
If
you have any doubts on the code and looking for the help on this code, please
post your queries in the comments section.
package
com.mohan.javaexamples;
import
java.io.BufferedInputStream;
import
java.io.BufferedOutputStream;
import java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import java.net.URL;
import
java.net.URLConnection;
public class FIleLoadFtp {
public FIleLoadFtp() {
}
public void upload() throws IOException {
String
ftpServer = "ftpservername";
String
userName = "username";
String
password = "password";
File
source = new File(
"location of file with Filename(which file, you want
upload to ftp)");
String
fileName = source.getName();
if (ftpServer != null &&
fileName != null && source != null) {
StringBuffer
buffer = new StringBuffer("ftp://");
if (userName != null &&
password != null) {
buffer.append(userName);
buffer.append(':');
buffer.append(password);
buffer.append('@');
}
buffer.append(ftpServer);
buffer.append("//");
buffer.append(fileName);
buffer.append(";type=i");
BufferedInputStream
bis = null;
BufferedOutputStream
bos = null;
try {
URL
url = new URL(buffer.toString());
System.out.println("Url :
" + url);
URLConnection
urlCon = url.openConnection();
System.out.println("UrlCon : " + urlCon);
bos
= new
BufferedOutputStream(urlCon.getOutputStream());
bis
= new
BufferedInputStream(new FileInputStream(source));
int i;
while (((i =
bis.read()) != -1)) {
bos.write(i);
}
}
finally {
if (bis != null) {
try {
bis.close();
}
catch (IOException e)
{
e.getMessage();
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
}
catch (IOException
ioe) {
ioe.getMessage();
ioe.printStackTrace();
}
}
}
}
else {
System.out.println("Input is
not available");
}
}
public void download(String
ftpServer, String userName, String password,
File
destination, String fileName) throws IOException {
if (ftpServer != null &&
fileName != null && destination != null) {
StringBuffer
buffer = new StringBuffer("ftp://");
if (userName != null &&
password != null) {
buffer.append(userName);
buffer.append(':');
buffer.append(password);
buffer.append('@');
}
buffer.append(ftpServer);
buffer.append("//");
buffer.append(fileName);
buffer.append(";type=i");
BufferedInputStream
bis = null;
BufferedOutputStream
bos = null;
try {
URL
url = new URL(buffer.toString());
System.out.println("url is ;
" + url);
URLConnection
urlCon = url.openConnection();
bis
= new
BufferedInputStream(urlCon.getInputStream());
bos
= new
BufferedOutputStream(new FileOutputStream(destination.getName()));
int i;
while (((i =
bis.read()) != -1)) {
bos.write(i);
}
}
finally {
if (bos != null) {
try {
bos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
}
catch (IOException
ie) {
System.out.println("message
: " + ie.getMessage());
ie.printStackTrace();
}
}
}
}
}
}
No comments:
Post a Comment