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服务器的更多相关文章

  1. .Net 上传文件到ftp服务器和下载文件

    突然发现又很久没有写博客了,想起哎呦,还是写一篇博客记录一下吧,虽然自己还是那个渣渣猿. 最近在做上传文件的功能,上传到ftp文件服务器有利于管理上传文件. 前面的博客有写到layui如何上传文件,然 ...

  2. java 上传文件到 ftp 服务器

    1.  java 上传文件到 ftp 服务器 package com.taotao.common.utils; import java.io.File; import java.io.FileInpu ...

  3. asp.net 服务器 上传文件到 FTP服务器

    private string ftpServerIP = "服务器ip";//服务器ip private string ftpUserID = "ftp的用户名" ...

  4. ftp配置 Laravel上传文件到ftp服务器

    listen=YES anonymous_enable=NO local_enable=YES write_enable=YES local_umask= dirmessage_enable=YES ...

  5. Linux系统下定时上传文件至FTP服务器脚本

    环境:Red Hat Enterprise Linux Server release 6.4 需求:需要将Oracle数据库的定时备份上传至FTP服务器 1.干货,用户名:oracle,数据库名称:X ...

  6. Android 上传文件到 FTP 服务器

    实现背景 近期接触到一个需求,就是将文件从Android系统上传到FTP服务器,虽然之前接触过FTP服务器,了解基本的使用流程,但是将此流程从使用习惯转化为代码实现还是有一定难度的.但是基本的流程还是 ...

  7. Linux: 通过命令行上传文件到ftp服务器

    url -T fie-name ftp://server-address --user user:password

  8. WIN7系统 如何上传文件到FTP服务器中

    https://zhidao.baidu.com/question/214644671.html

  9. Linux下自动备份MySQL数据库并上传到远程FTP服务器

    Linux下自动备份MySQL数据库并上传到远程FTP服务器且删除指定日期前的备份Shell脚本 说明:  1.备份MySQL数据库存放目录/var/lib/mysql下面的xshelldata数据库 ...

随机推荐

  1. NOIp 2013 #3 转圈游戏 Label:模拟

    题目描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……,依此 ...

  2. 【BZOJ2186】【SDoi2008】沙拉公主的困惑 数论

    Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现 ...

  3. 简单 常用的git命令

    常用的git命令 git pull  获取最新 git add . 提交所有 git commit  -m “我的注释” git status 查看状态 git push origin master ...

  4. Linux查看操作系统时间

    date命令的功能是显示和设置系统日期和时间. 该命令的一般格式为: date [选项] 显示时间格式(以+开头,后面接格式) date 设置时间格式 命令中各选项的含义分别为: -d datestr ...

  5. Update UI from an asynchronous thread

    One of the most common tasks you need to perform in a Windows Phone application is updating the UI f ...

  6. 抄书 Copying Books UVa 714

    Copying  Books 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B 题目: Descri ...

  7. spring mvc 拦截器 拦截子目录

    项目中碰到这一个问题: 对于/user/loginpage,/user/login这一类的url,放行: 对于/user/{userId}/xxx(xxx不为空)的操作,需要拦截,url-patter ...

  8. GitLab安装手记

    阿里云1G内存20G硬盘 1.首先下载GitLab Deb包(官网附有apt-get安装方式,但国内环境貌似不成功): https://about.gitlab.com/downloads/ 2. d ...

  9. javaweb实验五

    product类: package com.lab;public class Product { private int id;                // 商品编号    private S ...

  10. redis集群搭建

    1.  Redis Cluster的架构图.            (1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽. (2)节点的fail是通过集群 ...