Today, remote FTP suddenly hung up, resulting in reading FTP remote files of the program zombie thread. The check found that the code did not judge timeout exceptions. has now been modified as follows.
/** * The remote file is temporarily written into the local * @param ftpurl * @param outputpath/public static Boolean writeftpfile2local (String Ftpurl,
String OutputPath) {System.out.println ("Start writing files to local");
URLConnection Con=null;
FileOutputStream FS =null;
try {///connect the specified network resource, get the network input stream URL u=new url (ftpurl);
int bytesum = 0;
int byteread = 0;
Con=u.openconnection (); System.setproperty ("Sun.net.client.defaultConnectTimeout", "60000")//jdk1.4 replaced with this, connection timeout System.setproperty ("Sun. Net.client.defaultReadTimeout "," 60000 ");
jdk1.4 replaced this, read operation timeout con.connect ();
InputStream instream = Con.getinputstream ();
fs= new FileOutputStream (OutputPath);
byte[] buffer = new byte[1444];
while ((Byteread = instream.read (buffer))!=-1) {bytesum + = Byteread;
Fs.write (buffer, 0, byteread);
return true; catch (Exception e) {System.out.println ("failed to read the remote file to the local.") Please contact your system administrator! "
+ftpurl);
E.printstacktrace ();
return false; }finally{try {if (fs!=null) {FS.CLose ();
Fs=null;
} if (con!=null) {con=null;
} catch (IOException e) {e.printstacktrace (); }
}
}