public void sendJobs(String hostname, int port, String username, String password, String folderName, Collection<File> files) throws Exception {
FtpClient ftp = new FtpClient(hostname, port);
ftp.login(username, password);
ftp.binary();
//ftp.cd("FTP_Hiring"); // this is for testing in Accel FTP. comment for other FTP
if(folderName != "") {
ftp.cd(folderName);
}
try {
for(File file : files) {
FileInputStream fos = new FileInputStream(file);
TelnetOutputStream tos = (TelnetOutputStream) ftp.put(file.getName());
try {
//ftp.cd(file.getName().substring(0, file.getName().lastIndexOf(".")));
DataOutputStream dos = new DataOutputStream(tos);
byte[] buffer = new byte[1024 * 1024];
for (int length; (length = fos.read(buffer)) > 0;) {
dos.write(buffer, 0, length);
}
System.out.println("success");
tos.flush();
tos.close();
fos.close();
ftp.cdUp();
} catch(Exception e) {
hsLogger.error(e);
e.printStackTrace();
} finally {
tos.close();
fos.close();
}
}
} catch(Exception e) {
hsLogger.error(e);
e.printStackTrace();
}
}
上面给出了我的代码。我在调度程序中运行它。但在一线
TelnetOutputStream tos = (TelnetOutputStream) ftp.put(file.getName());
我得到以下异常
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
at sun.net.TransferProtocolClient.readServerResponse(TransferProtocolClient.java:49)
at sun.net.ftp.FtpClient.readReply(FtpClient.java:217)
at sun.net.ftp.FtpClient.issueCommand(FtpClient.java:193)
at sun.net.ftp.FtpClient.openDataConnection(FtpClient.java:383)
at sun.net.ftp.FtpClient.put(FtpClient.java:594)
at com.hiringsteps.ats.util.net.service.impl.JobXMLManagerService.sendJobs(JobXMLManagerService.java:1883)
at com.hiringsteps.ats.util.net.service.impl.JobXMLManagerService.postJobs(JobXMLManagerService.java:273)
at com.hiringsteps.ats.job.facade.impl.JobFacade$2.jobPosting(JobFacade.java:2031)
at com.hiringsteps.ats.job.facade.impl.JobFacade$2.run(JobFacade.java:1935)
at com.hiringsteps.ats.util.scheduler.service.impl.Scheduler$SchedulerTimerTask.run(Scheduler.java:20)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)