Ftp Uploading and Down

Embed Size (px)

Citation preview

  • 8/3/2019 Ftp Uploading and Down

    1/4

    packagecom.resource.util;

    importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;

    importjava.io.IOException;importjava.net.MalformedURLException;importjava.net.URL;importjava.net.URLConnection;

    /***ThisclassisusedtouploadafiletoaFTPserver.**@authorMuthu*/publicclassFileUpload{

    /***UploadafiletoaFTPserver.AFTPURLisgeneratedwiththe*followingsyntax:*ftp://user:password@host:port/filePath;type=i.**@paramftpServer,FTPserveraddress(optionalport':portNumber').*@paramuser,Optionalusernametologin.*@parampassword,Optionalpasswordforuser.*@paramfileName,DestinationfilenameonFTPserver(withoptional*precedingrelativepath,e.g."myDir/myFile.txt").*@paramsource,Sourcefiletoupload.*@throwsMalformedURLException,IOExceptiononerror.

    */publicvoidupload(StringftpServer,Stringuser,Stringpassword,StringfileName,Filesource)throwsMalformedURLException,IOException{if(ftpServer!=null&&fileName!=null&&source!=null){StringBuffersb=newStringBuffer("ftp://");//checkforauthenticationelseassumeitsanonymousaccess.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=ASCIImode,i=image(binary)mode,d=filedirectory*listing*/sb.append(";type=i");

    BufferedInputStreambis=null;BufferedOutputStreambos=null;try

  • 8/3/2019 Ftp Uploading and Down

    2/4

    {URLurl=newURL(sb.toString());URLConnectionurlc=url.openConnection();

    bos=newBufferedOutputStream(urlc.getOutputStream());bis=newBufferedInputStream(newFileInputStream(source));

    inti;//readbytebybyteuntilendofstreamwhile((i=bis.read())!=-1){bos.write(i);}}finally{if(bis!=null)try{

    bis.close();}catch(IOExceptionioe){ioe.printStackTrace();}if(bos!=null)try{bos.close();}catch(IOExceptionioe){

    ioe.printStackTrace();}}}else{System.out.println("Inputnotavailable.");}}

    /***DownloadafilefromaFTPserver.AFTPURLisgeneratedwiththe*followingsyntax:*ftp://user:password@host:port/filePath;type=i.**@paramftpServer,FTPserveraddress(optionalport':portNumber').*@paramuser,Optionalusernametologin.*@parampassword,Optionalpasswordforuser.*@paramfileName,Nameoffiletodownload(withoptionalpreceeding*relativepath,e.g.one/two/three.txt).*@paramdestination,Destinationfiletosave.*@throwsMalformedURLException,IOExceptiononerror.*/publicvoiddownload(StringftpServer,Stringuser,Stringpassword,StringfileName,Filedestination)throwsMalformedURLException,

    IOException{if(ftpServer!=null&&fileName!=null&&destination!=null)

  • 8/3/2019 Ftp Uploading and Down

    3/4

    {StringBuffersb=newStringBuffer("ftp://");//checkforauthenticationelseassumeitsanonymousaccess.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=ASCIImode,i=image(binary)mode,d=filedirectory*listing*/sb.append(";type=i");BufferedInputStreambis=null;

    BufferedOutputStreambos=null;try{URLurl=newURL(sb.toString());URLConnectionurlc=url.openConnection();

    bis=newBufferedInputStream(urlc.getInputStream());bos=newBufferedOutputStream(newFileOutputStream(destination.getName()));

    inti;while((i=bis.read())!=-1){

    bos.write(i);}}finally{if(bis!=null)try{bis.close();}catch(IOExceptionioe){ioe.printStackTrace();}if(bos!=null)try{bos.close();}catch(IOExceptionioe){ioe.printStackTrace();}}}

    else{System.out.println("Inputnotavailable");

  • 8/3/2019 Ftp Uploading and Down

    4/4

    }}}