Java implementation reads the CSV file on the FTP server

Source: Internet
Author: User
Tags ftp file get ip ftp client ftp protocol

Defining the FTP Operation interface

Import Java.io.InputStream; Import java.util.List; Import org.apache.commons.net.ftp.FTPClient; /** * FTP Server Operation */  Public Interface IFTPSERVU {    public  ftpclient FTP (string IP, string user, string password);      Public List<string[]> csv (InputStream in);}

Interface Implementation Class

Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.SocketException;ImportJava.nio.charset.Charset;Importjava.util.ArrayList;Importjava.util.List;Importorg.apache.commons.net.ftp.FTPClient;Importorg.apache.commons.net.ftp.FTPReply;ImportCom.csvreader.CsvReader;/*** FTP Server operation specific implementation*/ Public classFtpservuimplImplementsIftpservu {/**Local character encoding*/    Private StaticString local_charset = "GBK"; //FTP protocol, the specified file name is encoded as Iso-8859-1    Private StaticString server_charset = "Iso-8859-1"; /*** * <b> Login FTP return ftpclient event <b> * *@paramIP * FTP location IP *@paramUser * Login name *@paramPassword * Password*/     Publicftpclient FTP (string IP, string user, string password) {ftpclient ftpclient=Newftpclient (); Try{ftpclient.connect (IP); if(Ftpreply.ispositivecompletion (Ftpclient.getreplycode ())) {if(ftpclient.login (user, password)) {if(Ftpreply.ispositivecompletion (Ftpclient.sendcommand ("OPTS UTF8", "on")) {//turn on server support for UTF-8, and use local encoding (GBK) if the server supports it with UTF-8 encoding.Local_charset = "UTF-8";                    } ftpclient.setcontrolencoding (Local_charset); Ftpclient.enterlocalpassivemode ();//Set Passive mode//Ftpclient.setfiletype (Gettransformodule ());//set the mode of the transfer                }            }            //ftpclient.login (user, password);}Catch(SocketException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }        if(!ftpclient.isconnected ()) {ftpclient=NULL; }        returnftpclient; }    /*** <b> parses an IO stream, transforms the collection in array form <b> *@paramIn * file InputStream stream*/     PublicList<string[]>csv (InputStream in) {List<String[]> csvlist =NewArraylist<string[]>(); if(NULL!=In ) {Csvreader Reader=NewCsvreader (In, ', ', Charset.forname ("GBK"))); Try {                //Traverse each line, if there is a # comment section, then do not process, if not, then add Csvlist                 while(Reader.readrecord ()) {if(!reader.getvalues () [0].contains ("#"))//Clear the comments section{Csvlist.add (reader.getvalues ()); }                }            } Catch(IOException e) {e.printstacktrace ();        } reader.close (); }        returncsvlist; }}

In the business scenario, the call to FTP occurs in the reconciliation operation, the user by entering the number of trades, the CSV file name, to determine whether the file exists on the FTP server, if the file exists, first get the number of content in the file, compared with the number of input pen value, Consistent, continue to obtain the CSV file content and the system database for comparison, reconciliation, so as to complete the reconciliation operation.

ImportJava.io.InputStream;Importjava.util.List;Importorg.apache.commons.collections.CollectionUtils;Importorg.apache.commons.net.ftp.FTPClient;ImportOrg.apache.commons.net.ftp.FTPFile;//Profile Tool ClassImportCom.online.charge.customer.deployconfig.util.DeployConfigUtil;/*** @ClassName: Queryftpfilesutils * @Description: Get a list of files on FTP **/ Public classQueryftpfilesutils {/*** Get FTP client * *@return     */     Public Staticftpclient getftpclient () {String IP=deployconfigutil.getinterfacedeployconfig (). Getftpurl (); String UserName=deployconfigutil.getinterfacedeployconfig (). Getftpusername (); String UserPassword=deployconfigutil.getinterfacedeployconfig (). Getftppassword (); //get ip,username,password by configuration fileFtpClient ftpclient =getiftpservu (). FTP (IP, userName, userpassword); returnftpclient; }    /*** Get FTP interface Service * *@return     */    Private StaticIftpservu Getiftpservu () {return NewFtpservuimpl (); }    /*** Get the number of lines * *@paramfileName * CSV file name such as: 0320180908110523.csv file name is 0320180908110523 *@returnNumber of detail bars*/     Public Staticinteger getftpfilecount (String fileName) {integer Count= 0; BooleanIscontain =Boolean.false; FtpClient ftpclient=getftpclient (); List<String[]> fileList =NULL; if(NULL!=ftpclient) {            Try{ftpfile[] file=Ftpclient.listfiles (); if(File = =NULL|| File.length <= 0) {                    return-1; }                //traverse all files to match the files you need to find                 for(inti = 0; i < file.length; i++) {                    //match to then enter                    if(File[i].getname (). Equals (Filename.concat (". csv")) {Iscontain=boolean.true; //Stream the matched file into the interface and convert the collection to an array ofInputStream in =Ftpclient.retrievefilestream (File[i].getname ()); FileList=Getiftpservu (). csv (in);                    In.close (); }                }                if(!Iscontain) {                    return-1; }                if(Collectionutils.isnotempty (fileList) && filelist.size () > 0) {Count=filelist.size (); }            } Catch(Exception e) {e.printstacktrace (); }        }        returncount; }    /*** Get FTP file contents * *@paramFileName *@return     */     Public Static voidgetftpfilecontent (String fileName, List<String[]>filecontent) {ftpclient ftpclient=getftpclient (); if(NULL!=ftpclient) {            Try{ftpfile[] file=Ftpclient.listfiles (); //traverse all files to match the files you need to find                 for(inti = 0; i < file.length; i++) {                    //match to then enter                    if(File[i].getname (). Contains (FileName)) {InputStream in=Ftpclient.retrievefilestream (File[i].getname ());                        Filecontent.addall (GETIFTPSERVU (). csv (in));                    In.close (); }                }            } Catch(Exception e) {e.printstacktrace (); }        }    }}

In the test, encountered can be obtained through the Listfiles method to the FTP server file list, but call Retrievefilestream method to get the contents of the file failed (return is not null), online find a lot of methods, try to fail, now summarized as follows:

//match to then enterif(File[i].getname (). Equals (Filename.concat (constant.account_file_extends_name))) {Iscontain=boolean.true;//1. Set the active mode//Ftpclient.enterlocalpassivemode (); //2. File type issues, setting file types//Ftpclient.setfiletype (ftpclient.binary_file_type);//3. Encoding problem, setting the encoding//InputStream in = Ftpclient.retrievefilestream (New String (File[i].getname (). GetBytes ("UTF-8"), "iso-8859-1"));InputStream in =Ftpclient.retrievefilestream (File[i].getname ());//Stream the matched file into the interface and convert the collection to an array ofFileList =Getiftpservu (). csv (in); In.close ();//4. Get the return status after the stream is closed//Ftpclient.completependingcommand ();}

The suspicion is that the Retrievefilestream method calls the socket to get the stream in the middle of the problem, but the problem is currently unable to locate (readers if you know why bother to enlighten!) )

To solve this problem, a change of mind: Because the ftp file has data, and the number of input is greater than 0, so when it is determined that FTP does have the file, the first or the call Retrievefilestream method to stream the way to obtain the file, converted to the array after the decision, if the length of 0, In the case of failure to get the contents of the file, this is a different approach: Use the Retrievefile method to download the files on the FTP server locally and get their contents through IO streams. The implementation method is as follows:

    //match to then enter    if(File[i].getname (). Equals (Filename.concat (". csv")) {Iscontain=boolean.true; //Stream the matched file into the interface and convert the collection to an array ofInputStream in =Ftpclient.retrievefilestream (File[i].getname ()); FileList=Getiftpservu (). csv (in);        In.close (); if(filelist.size () = = 0){            //download ftp file to localString osfilename = "g:/ftpfiles/" +File[i].getname (); File LocalFile=NewFile (osfilename); OutputStream OS=NewFileOutputStream (LocalFile);            Ftpclient.retrievefile (File[i].getname (), OS);                        Os.close (); InputStream ins=NewFileInputStream (NewFile (osfilename)); FileList=Getiftpservu (). csv (INS);        Ins.close (); }    }

Java implementation reads the CSV file on the FTP server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.