FTPClient.storeFile()返回false,FTP返回代码为450,表示文件正忙

时间:2014-06-23 08:01:15

标签: java file-upload

我的要求是将文件写入文件服务器,并且在将文件写入文件服务器时需要起草任何连接或问题。

在按业务测试环境中测试下面的代码时, client.storeFile 为少量文件返回false。我们将文件手动放在目标文件服务器中以用于失败的文件。在PROD环境中不会出现这种情况。关于可能是什么原因的任何想法?

仅供参考:通过上传许多文件来对此进行测试。

我的代码:

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";

此致 Venkat

1 个答案:

答案 0 :(得分:2)

嗨首先尝试从方法storeFile()

中找到您收到的回复内容
ftp.getReplyString()

使用system.out.println并找出FTP的问题。