About Java reading remote files when network timeout judgment

Source: Internet
Author: User

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 (); }
		}
	}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.