FTPClient.storeFile() returning false and

2019-08-15 06:12发布

My requirement is to write a file to file server and for any connectivity or issues while writing file to file server need to draft a mail.

While testing below code in testing environment by business,client.storeFile returning false for few files .We are placing files manually in target file server for failed ones.It is not expected in PROD environment .Any thoughts on what might be the reason ?

FYI:Business testing this by uploading many files.

My Code:

int TIMEOUT = TimeOut * 1000; //Variable to store Connection TimeOut
AbstractTrace trace = container.getTrace();
FileInputStream fis = null;

FTPClient client = new FTPClient();  //Creating a FTPClient instance

try{

    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX); 
    conf.setDefaultDateFormatStr(Dateformat);
    client.configure(conf);
    try{
        client.setConnectTimeout(TIMEOUT); //Setting connection timeout
        client.connect(Host);// connecting to ftp server
        if(!client.login(Username, Pwd)){   throw new StreamTransformationException("Authorization failed.");} //Giving credentials to login        

        trace.addInfo("Successfully connected to server"+client.getReplyString());

        if(!client.changeWorkingDirectory(Folderpath)){ throw new StreamTransformationException("Exception occurred while changing folder path to Interface specific path.");} //Changing the current directory to required folder path

    }
    catch(Exception c){
        throw new StreamTransformationException("Exception occured while connecting to server  :" + c);

    }//close brace for catch


    // Create an InputStream of the file to be uploaded
    String srcFilename = FileName+"_Temp"+new Date().getTime();
    String targetfilename = FileName+"_"+new Date().getTime();

    File Sourcefile =new File(srcFilename);

    //If file doesn't exists, then create it
    if(!Sourcefile.exists()){ Sourcefile.createNewFile();}
    FileWriter fileWritter = new FileWriter(Sourcefile.getName(),true);
    BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
    bufferWritter.write(Input); //Writing the Input payload to file
    bufferWritter.close();

    fis = new FileInputStream(Sourcefile);
    boolean done = client.storeFile(targetfilename, fis); //Store file to server
    fis.close();

    if (done) {
        trace.addInfo("!!!----File is uploaded successfully----!!!");
    } else {
        trace.addWarning("Upload Failed");
        throw new StreamTransformationException("Failed to upload file  :Please cross check..:");

    } //close brace for else
    client.logout();
}catch(Exception e){

    try{

        Properties properties = System.getProperties(); // Get system properties

        properties.setProperty("mail.smtp.host",Mail_Host);  // Setup mail server

        //Session session = Session.getDefaultInstance(properties);  // Get the default Session object.

        Session session = Session.getInstance(properties);  // if you get Unknown Host exception in JAVA

        //Sending mail to app support folks                 

        MimeMessage message = new MimeMessage(session);     // Create a default MimeMessage object.            

        message.setFrom(new InternetAddress(Mail_From));   // Set From: header field of the header.     

        String recipients[] =Mail_To.split(";");

        InternetAddress[] addressTo = new InternetAddress[recipients.length];

        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        message.setRecipients(Message.RecipientType.TO, addressTo);

        //message.addRecipient(Message.RecipientType.TO,new InternetAddress(Mail_To));     // Set To: header field of the header.            

        message.setSubject(Mail_Subject);    // Set Subject: header field       

        message.setText(e.getMessage()+"."+" Failed message is having field value of "+FieldName+" is "+ReturnFieldName);   // Now set the actual message            

        Transport.send(message);   // Send message 

        trace.addInfo("Sent alert mail to app support folks successfully");

    }catch(Exception mex){

        trace.addWarning("Failed to send alert mail : "+mex);

    }//close brace for catch
} //close brace for catch

finally {
    try {
        if (fis != null) {
            fis.close();
        }
        client.disconnect();
    } catch (IOException k) {trace.addWarning("Exception while closing filestream and diconnecting"+k); }
}  //close brace for finally

return "File placed successfully";

Regards Venkat

1条回答
SAY GOODBYE
2楼-- · 2019-08-15 07:07

Hi First try to find what is reply your receiving from method storeFile()

ftp.getReplyString()

use system.out.println and find out what is problem with FTP.

查看更多
登录 后发表回答