Java simulated FTP invoke FTP server to upload files

Source: Internet
Author: User
Tags ftp file

Java simulated FTP invoke FTP server to upload files


FTP Server-side download address:https://download.csdn.net/download/heqinghua217/10339631

After the download, install, and then create a new domain, and then set the account, and account directory permissions.


The Java call code is as follows, remember here only supports jdk1.6, if is the Web project, introduced jdk1.6, does not need to add any jar package to be able to run


Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;

Import java.io.IOException;
Import Sun.net.TelnetInputStream;
Import Sun.net.TelnetOutputStream;

Import sun.net.ftp.FtpClient;

	public class Tftpupload {ftpclient ftpclient = new FtpClient ();

	String sserver = "";

	String suser = "";
	String Spassword = "";
	String Sdesfolder;

	String supdate;
	Public String Getsdesfolder () {return sdesfolder;
	} public void Setsdesfolder (String desfolder) {sdesfolder = Desfolder;
	Public String Getsupdate () {return supdate;
	public void Setsupdate (String update) {supdate = update;
	Public String Getspassword () {return spassword;
	} public void Setspassword (String password) {spassword = password;
	Public String Getsserver () {return sserver;
	public void Setsserver (String server) {sserver = server;
	Public String Getsuser () {return suser;
	public void Setsuser (String user) {suser = user; }/** FTP Directory upload */private void Uploadfolder (String strfoldname) throws IOException {file F = new file (strfoldname);
		if (!f.exists ()) {return;
		File subfile[] = F.listfiles ();
		for (int i = 0; i < subfile.length i++) {uploadfile (subfile[i), Subfile[i].getname ()); }/** * Set login FTP information */private void Setlogininfo (String strServerIP, String struser, String strpasswd) {SSer
		ver = strServerIP;
		Suser = struser;
	Spassword = strpasswd;
		/** * Connect FTP/public void Connect () throws IOException {ftpclient.openserver (sserver);
		Ftpclient.login (Suser, Spassword);
	Ftpclient.binary (); /** * Upload file to ftp strsrcfilename upload file path strobjfilename file path after upload/public void UploadFile (String strsrcfilename, S
		Tring Strobjfilename) throws IOException {file file_in = new file (strsrcfilename);
	UploadFile (file_in, strobjfilename); /** * FTP Download srcfname ftp file path to download tarfname download to local file path/public void DownloadFile (string srcfname, String Tarfnam E) throws Exception {Telnetinputstream is = Ftpclient.get (srcfname);
		File File_out = new file (tarfname);
		FileOutputStream OS = new FileOutputStream (file_out);
		byte[] bytes = new byte[1024];
		int C;
		while ((c = is.read (bytes))!=-1) {os.write (bytes, 0, c);
		} is.close ();
	Os.close (); /** Upload file */private void UploadFile (file fscrfile, String strobjfilename) throws IOException {Telnetoutputstre
		Am OS = ftpclient.put (strobjfilename);
		FileInputStream is = new FileInputStream (fscrfile);
		byte[] bytes = new byte[1024];
		int C;
		while ((c = is.read (bytes))!=-1) {os.write (bytes, 0, c);
		} is.close ();
		Os.close (); Log ("" "+ Strobjfilename +" "file has been uploaded.
	\ r \ n ");
	public void Closeconnect () throws IOException {ftpclient.closeserver ();
	private void log (Object obj) {System.out.println (obj);
		/** requires JDK 1.6 */public static void main (string[] args) throws Exception {tftpupload ftp = new Tftpupload (); Ftp.setlogIninfo ("10.250.196.67", "Heqh", "123456");
		Ftp.connect ();
		 Ftp.uploadfile ("d:/basic format. xml", "Basic format. xml");
		Ftp.downloadfile ("Basic format. xml", "c:/basic format. xml");
	Ftp.closeconnect ();
 }

}

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.