利用Apache 实现FTp的上传和下载

在ftp上传时我遇到了很奇怪的问题:

上传中文名的文件在服务器上显示正常 ,可到浏览器上就变成了乱码,下载后文件也是正常的。不知道什么原因,修改浏览器编码也不管用,不知道各位朋友遇到这样的问题没,这是我的上传ftp服务器代码,供大家参考下

package com.wu;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

/**
 * @author wu
 *
 */
public class Ftps {

	public static void main(String[] args) {
		ftpUpload("10.254.201.50", "21", "xxxx", "123456", "DIMS/UploadData", "C:\\Users\\Desktop\\", "12345新建文本文.txt");
		downLoadFile("10.254.201.50", "21", "xxxx", "123456","DIMS/UploadData/12345新建文本文.txt","C:\\Users\\Desktop\\12345新建文本文.txt");

}
        /** 
	 * 通过ftp上传文件 
	 * @param url ftp服务器地址 如: 192.168.1.110 
	 * @param port 端口如 : 21 
	 * @param username  登录名 
	 * @param password   密码 
	 * @param remotePath  上到ftp服务器的磁盘路径 
	 * @param fileNamePath  要上传的文件路径 
	 * @param fileName      要上传的文件名 
	 * @return 
	 */  
	public static String ftpUpload(String url, String port, String username,String password, String remotePath, String fileNamePath,String fileName) {  
		 FTPClient ftpClient = new FTPClient();  
		 FileInputStream fis = null;  
		 String returnMessage = "0";  
		 try {  
		     ftpClient.connect(url, Integer.parseInt(port));  
		     boolean loginResult = ftpClient.login(username, password);  
		     int returnCode = ftpClient.getReplyCode();  
		     if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功  
		    	 ftpClient.setControlEncoding("GBK"); 
		    	 ftpClient.makeDirectory(remotePath);  
		         // 设置上传目录  
		         ftpClient.changeWorkingDirectory(new String(remotePath.getBytes("GBK"),"iso-8859-1"));  
		         ftpClient.setBufferSize(1024);  
		         //ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
		         ftpClient.enterLocalPassiveMode();  
		         fis = new FileInputStream(fileNamePath + fileName);  
		         //注意文件名转码否则可能不能上传haiy中文文件名
		         ftpClient.storeFile(new String(fileName.getBytes("GBK"), "ISO-8859-1") , fis);  
		            
		         returnMessage = "1";   //上传成功        
		     } else {// 如果登录失败  
		         returnMessage = "0";  
		         }  
		                
		   
		 } catch (IOException e) {  
		     e.printStackTrace();  
		     throw new RuntimeException("FTP客户端出错!", e);  
		 } finally {  
		     //IOUtils.closeQuietly(fis);  
		 try {  
		     ftpClient.disconnect();  
		 } catch (IOException e) {  
		        e.printStackTrace();  
		        throw new RuntimeException("关闭FTP连接发生异常!", e);  
		    }  
		 }  
		 return returnMessage;  
		} 

         /**
	 * ftp下载文件
         * @param remoteFileName --服务器上的文件名
         * @param localFileName--本地文件名
	 */
	public static boolean downLoadFile(String url, String port, String username,String password,String remoteFileName,String localFileName){
		 
		FTPClient ftpClient = new FTPClient();
	        BufferedOutputStream buffOut=null;
	        boolean flag = true;//下载成功返回true
	        try {
			ftpClient.connect(url, Integer.parseInt(port));
			boolean loginResult = ftpClient.login(username, password);  
			int returnCode = ftpClient.getReplyCode(); 
			if(loginResult && FTPReply.isPositiveCompletion(returnCode)){
			    buffOut=new BufferedOutputStream(new FileOutputStream(localFileName));
			    //注意编码 我刚开始没有编码 下载的文件会是空的
			    flag =  ftpClient.retrieveFile(new String(remoteFileName.getBytes("GBK"), "ISO-8859-1"), buffOut);
			}else{
			    	ftpClient.disconnect();
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			throw new RuntimeException("FTP客户端出错!", e1); 
		}finally{
		        try{
	                   if(buffOut!=null){
	                	buffOut.flush();
	                	buffOut.close();
	                   }
	                   ftpClient.logout();
	                   ftpClient.disconnect();
	                }catch(Exception e){
	                   e.printStackTrace();
	                   throw new RuntimeException("关闭FTP连接发生异常!", e);
	                }
		}
	        return flag;
	}
}

所需jar包 commons-net-3.0.1.jar、ftp4j-1.6.1.jar下载地址http://download.csdn.net/detail/yusewuhen/6669625



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值