Linux上F上傳檔案到FTP伺服器

來源:互聯網
上載者:User

標籤:style   color   io   os   ar   java   檔案   sp   on   

Linux上上傳跟Windows上上傳不一樣,在Windows上測試沒問題,但是放到Linux伺服器上跑,上傳的檔案中文顯示亂碼。解決方案:

FtpUtil.java紅色標記處

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");
/**
* 本地檔案名稱
*/
private String localfilename;
/**
* 遠程檔案名稱
*/
private String remotefilename;
/**
* FTP用戶端
*/
private FTPClient ftpClient;

/**
* 伺服器串連
*
* @param ip
* 伺服器IP
* @param port
* 伺服器連接埠
* @param user
* 使用者名稱
* @param password
* 密碼
* @param 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;
}

 

/**
* 上傳檔案
*/
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;
}

}

}

 

檔案內容構建處:

private static String encoding = System.getProperty("file.encoding");//擷取當前系統編碼,Windows跟Linux不同,這樣可以自動擷取處理編碼

.....

// 上傳到EcssCus
String remotefilename = "\\EcssCus\\" + orderNo + ".xml";
// 上傳到Ciq
String remotefilename1 = "\\Ciq\\" + orderNo + ".xml";
// 上傳到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();
}

Linux上F上傳檔案到FTP伺服器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.