我的要求是写一个到文件服务器的文件和任何连接或在写入文件到文件服务器问题需要起草一份邮件。
同时通过业务测试下面的代码在测试环境中,client.storeFile返回false的几个文件。我们是手动将文件放置在目标文件服务器失败ones.It预计不会在PROD环境。任何思考可能是什么原因呢?
FYI:企业通过上传多个文件测试此。
我的代码:
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