f Upload files to FTP server on Linux

Source: Internet
Author: User
Tags ftp client

Linux upload is not the same as the upload on Windows, testing on Windows is not a problem, but put on the Linux server running, uploaded files in Chinese garbled. Solution:

Ftputil.java Red Marking Place

Package cn.zto.util;

Import Java.io.ByteArrayInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.net.SocketException;

Import Org.apache.log4j.Logger;

Import Cn.zto.log4j.Log4jConfigurator;
Import Cn.zto.pusher.HeNanPusher;
Import Sun.net.TelnetInputStream;
Import Sun.net.TelnetOutputStream;

Import sun.net.ftp.FtpClient;
Import org.apache.commons.net.ftp.*;

@SuppressWarnings ({"Restriction", "unused"})
public class Ftputil {
Private Logger log = Log4jconfigurator.getlogger (Ftputil.class);
private static String encoding = System.getproperty ("file.encoding");
/**
* Local file name
*/
Private String localfilename;
/**
* Remote file name
*/
Private String remotefilename;
/**
* FTP client
*/
Private ftpclient ftpclient;

/**
* Server Connection
*
* @param IP
* Server IP
* @param Port
* Server Port
* @param user
* User Name
* @param password
* Password
* @param path
* Server Path
*/
public boolean connect (string hostname, int port, string username,
String password) {
FtpClient = new FtpClient ();
try {
Ftpclient.connect (hostname, port);
Ftpclient.setcontrolencoding ("UTF-8");
if (Ftpreply.ispositivecompletion (Ftpclient.getreplycode ())) {
if (Ftpclient.login (username, password)) {
return true;
}
}
} catch (Exception e) {
E.printstacktrace ();
This.log.debug ("Connect error");
return false;
}
return true;
}

/**
* Upload Files
*/
public boolean upload (string remotefilename, string xml) {
This.remotefilename = Remotefilename;
try {
InputStream input = new Bytearrayinputstream (xml.getbytes ("Utf-8"));
Ftpclient.changeworkingdirectory (New String (remotefilename
. GetBytes (encoding), "iso-8859-1"));
Ftpclient.storefile (remotefilename, input);
return true;
} catch (IOException ex) {
This.log.debug ("not upload" + ex.getmessage ());
return false;
}

}

}

File Content Build Office:

private static String encoding = System.getproperty ("file.encoding");//Gets the current system encoding, Windows is different from Linux, so that processing encoding can be obtained automatically

.....

Upload to Ecsscus
String remotefilename = "\\EcssCus\\" + OrderNo + ". xml";
Upload to Ciq
String remotefilename1 = "\\Ciq\\" + OrderNo + ". xml";
Upload to Ecssorders
String remotefilename2 = "\\Ciq\\Orders\\" + OrderNo + ". xml";
try {
XML = new String (Xml.getbytes (), encoding);
XML1 = new String (Xml1.getbytes (), encoding);
XML2 = new String (Xml2.getbytes (), encoding);
This.log.debug (XML);
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}

f Upload files to FTP server on Linux

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.