最近用到ftp上传文件到服务器上。文件名称为非中文的都能正常上传,中文文件名就有问题。会抛出以下异常:
org.apache.commons.net.io.CopyStreamException: IOException caught while copying.不知道怎么解决!!!!源代码:
private FTPClient ftpClient;
private String hostname;
private int port;
private String password;
private String username;
/**
* @param hostname 服务器地址
* @param port 服务器端口
* @param username 服务器用户名
* @param password 服务器密码
*/
public FtpClientUtil(String hostname,int port,String username, String password){
this.hostname = hostname;
this.port = port;
this.password = password;
this.username = username;
ftpClient = new FTPClient();
}
/**
* FTPClient连接
* @param hostname 服务器地址
* @param port 服务器端口
* @return
* @throws SocketException
* @throws IOException
*/
private boolean connectServer(String hostname, int port) throws SocketException, IOException{
this.ftpClient.connect(hostname, port);
return this.ftpClient.isConnected();
}
/**
* FTPClient登陆
* @param username 服务器用户名
* @param password 服务器密码
* @return
* @throws IOException
*/
private boolean login(String username, String password) throws IOException{
return this.ftpClient.login(username, password);
}
/**
* 通过输入流上传本地文件
* @param fileName 上传到服务器后文件名称
* @param inputStream 本地文件流
* @param remotePath 服务器路径
* @return 返回上传成功标志
* @throws SocketException
* @throws IOException
*/
public boolean uploadFile(String fileName, InputStream inputStream, String remotePath) throws SocketException, IOException{
boolean flag = false;
if(this.connectServer(this.hostname, this.port) && this.login(this.username, this.password)){
this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE); //使用二进制流上传文件
this.ftpClient.changeWorkingDirectory(remotePath); //跳转到服务器对应目录
ftpClient.setControlEncoding("GBK"); flag = this.ftpClient.storeFile("测试.Nds", inputStream);
}
return flag;
}
/**
* @return
*/
public FTPClient getFtpClientInstance(){
return ftpClient;
}
/**
* FTOClient 连接
* @throws IOException
*/
public void disposeFtpClient() throws IOException{
if(this.ftpClient != null){
if(this.ftpClient.isConnected())
this.ftpClient.disconnect();
this.ftpClient = null;
}
} public static void main(String[] args) {
// TODO Auto-generated method stub
FtpClientUtil a=new FtpClientUtil("192.168.92.36",21,"ccm3","ccm3");
try{
// a.connectServer("192.168.92.36", 21);
//a.login("ccm3", "ccm3");
FileInputStream is = new FileInputStream("c:/你.Nds");
a.uploadFile("vv.Nds", is, "/be");
a.disposeFtpClient();
}catch(Exception e){
e.printStackTrace();
}
}
org.apache.commons.net.io.CopyStreamException: IOException caught while copying.不知道怎么解决!!!!源代码:
private FTPClient ftpClient;
private String hostname;
private int port;
private String password;
private String username;
/**
* @param hostname 服务器地址
* @param port 服务器端口
* @param username 服务器用户名
* @param password 服务器密码
*/
public FtpClientUtil(String hostname,int port,String username, String password){
this.hostname = hostname;
this.port = port;
this.password = password;
this.username = username;
ftpClient = new FTPClient();
}
/**
* FTPClient连接
* @param hostname 服务器地址
* @param port 服务器端口
* @return
* @throws SocketException
* @throws IOException
*/
private boolean connectServer(String hostname, int port) throws SocketException, IOException{
this.ftpClient.connect(hostname, port);
return this.ftpClient.isConnected();
}
/**
* FTPClient登陆
* @param username 服务器用户名
* @param password 服务器密码
* @return
* @throws IOException
*/
private boolean login(String username, String password) throws IOException{
return this.ftpClient.login(username, password);
}
/**
* 通过输入流上传本地文件
* @param fileName 上传到服务器后文件名称
* @param inputStream 本地文件流
* @param remotePath 服务器路径
* @return 返回上传成功标志
* @throws SocketException
* @throws IOException
*/
public boolean uploadFile(String fileName, InputStream inputStream, String remotePath) throws SocketException, IOException{
boolean flag = false;
if(this.connectServer(this.hostname, this.port) && this.login(this.username, this.password)){
this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE); //使用二进制流上传文件
this.ftpClient.changeWorkingDirectory(remotePath); //跳转到服务器对应目录
ftpClient.setControlEncoding("GBK"); flag = this.ftpClient.storeFile("测试.Nds", inputStream);
}
return flag;
}
/**
* @return
*/
public FTPClient getFtpClientInstance(){
return ftpClient;
}
/**
* FTOClient 连接
* @throws IOException
*/
public void disposeFtpClient() throws IOException{
if(this.ftpClient != null){
if(this.ftpClient.isConnected())
this.ftpClient.disconnect();
this.ftpClient = null;
}
} public static void main(String[] args) {
// TODO Auto-generated method stub
FtpClientUtil a=new FtpClientUtil("192.168.92.36",21,"ccm3","ccm3");
try{
// a.connectServer("192.168.92.36", 21);
//a.login("ccm3", "ccm3");
FileInputStream is = new FileInputStream("c:/你.Nds");
a.uploadFile("vv.Nds", is, "/be");
a.disposeFtpClient();
}catch(Exception e){
e.printStackTrace();
}
}
改为
ftpClient.setControlEncoding("utf-8");
http://my.oschina.net/caiyuan/blog/33112 希望能帮到你!