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

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

问候 文卡特

最佳答案

您好,首先尝试查找您从方法 storeFile() 收到的回复

ftp.getReplyString()

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

关于java - FTPClient.storeFile() 返回 false,FTP 返回代码为 450,表示文件正忙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24361030/

相关文章:

php - 对 PHP move_uploaded_file() 修改的文件的奇怪权限

php - 使用ajax上传文件和插入数据

go - 在fasthttp中上传尺寸大于DefaultMaxRequestBodySize的文件

java.lang.String 无法转换为 java.util.List

java - Android Studio 更新到 3.4 后,工具菜单中不显示 SDK Manager 和 AVDManager

java - 字符串文字、实习和反射

java - Selenium Serenity 屏幕截图和电影延迟并挂起执行

java - Lucene 通配符查询

用于 CPU 消耗的 Java 分析工具

javascript - 使用Javascript上传 "file"