FTPClient.storeFile()返回false并且FTP返回代码是450并且表示文件正忙 [英] FTPClient.storeFile() returning false and FTP return code is 450 and says file is busy

查看:378
本文介绍了FTPClient.storeFile()返回false并且FTP返回代码是450并且表示文件正忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是写一个文件到文件服务器和任何连接或问题,同时写文件到文件服务器需要起草一个邮件。



当测试下面的代码在商业测试环境中, client.storeFile 为少数文件返回false。我们将文件手动放置在目标文件服务器中,以防出现故障。在PROD环境中,这是不期望的。原因是什么?b / b>
$ b

FYI:业务部门通过上传很多文件来测试。 b
$ b

  int TIMEOUT = TimeOut * 1000; //变量来存储连接TimeOut 
AbstractTrace trace = container.getTrace();
FileInputStream fis = null;

FTPClient client = new FTPClient(); //创建一个FTPClient实例

try {

FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
conf.setDefaultDateFormatStr(Dateformat);
client.configure(conf);
尝试{
client.setConnectTimeout(TIMEOUT); //设置连接超时
client.connect(Host); //连接到ftp服务器
if(!client.login(Username,Pwd)){抛出新的StreamTransformationException(Authorization failed。); } //授予凭证登录

trace.addInfo(成功连接到服务器+ client.getReplyString());

if(!client.changeWorkingDirectory(Folderpath)){抛出新的StreamTransformationException(将文件夹路径更改为接口特定路径时发生异常);} //将当前目录更改为所需的文件夹路径

$ b catch(Exception c){
throw new StreamTransformationException(连接服务器时发生异常:+ c);

//关闭大括号捕获


//创建要上传的文件的InputStream
字符串srcFilename = FileName +_ Temp+ new Date()。getTime();
String targetfilename = FileName +_+ new Date()。getTime();

文件Sourcefile =新文件(srcFilename);
$ b $ //如果文件不存在,则创建它
if(!Sourcefile.exists()){Sourcefile.createNewFile();}
FileWriter fileWritter = new FileWriter (Sourcefile.getName(),TRUE);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(Input); //将输入有效载荷写入文件
bufferWritter.close();

fis = new FileInputStream(Sourcefile);
boolean done = client.storeFile(targetfilename,fis); //将文件存储到服务器
fis.close();

if(done){
trace.addInfo(!!! ----文件上传成功---- !!!);
} else {
trace.addWarning(上传失败);
抛出新的StreamTransformationException(无法上传文件:请交叉检查..);

} //关闭大括号
client.logout();
catch(Exception e){

try {

属性properties = System.getProperties(); //获取系统属性

properties.setProperty(mail.smtp.host,Mail_Host); //安装邮件服务器

// Session session = Session.getDefaultInstance(properties); //获取默认的Session对象

Session session = Session.getInstance(properties); //如果在JAVA

//发送邮件给应用程序支持人员

MimeMessage message = new MimeMessage(session); //创建一个默认的MimeMessage对象。

message.setFrom(new InternetAddress(Mail_From)); // Set From:标题的标题字段。

字符串收件人[] = Mail_To.split(;);

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

(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)); //设置为:标题的标题字段。

message.setSubject(Mail_Subject); //设置主题:标题字段

message.setText(e.getMessage()+。+失败的消息的字段值是+ FieldName +是+ ReturnFieldName; //现在设置实际的消息

Transport.send(message); //发送消息

trace.addInfo(发送提醒邮件到应用程序支持成功者);

} catch(Exception mex){

trace.addWarning(发送警报邮件失败:+ mex);

} //大括号为catch
} //大括号为catch

finally {
try {
if(fis! = null){
fis.close();
}
client.disconnect();
} catch(IOException k){trace.addWarning(关闭filestream和diconnecting时出现异常+ k); }
} //最后关闭大括号

返回文件放置成功;

问候
Venkat

解决方案

嗨首先尝试找到什么是答复你从方法storeFile()收到$ / b>

  ftp .getReplyString()

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

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

解决方案

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.

这篇关于FTPClient.storeFile()返回false并且FTP返回代码是450并且表示文件正忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆