利用Apache 实现FTp的上传和下载
2013-12-06 16:39
471 查看
在ftp上传时我遇到了很奇怪的问题:
上传中文名的文件在服务器上显示正常 ,可到浏览器上就变成了乱码,下载后文件也是正常的。不知道什么原因,修改浏览器编码也不管用,不知道各位朋友遇到这样的问题没,这是我的上传ftp服务器代码,供大家参考下
所需jar包commons-net-3.0.1.jar、ftp4j-1.6.1.jar下载地址http://download.csdn.net/detail/yusewuhen/6669625
上传中文名的文件在服务器上显示正常 ,可到浏览器上就变成了乱码,下载后文件也是正常的。不知道什么原因,修改浏览器编码也不管用,不知道各位朋友遇到这样的问题没,这是我的上传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
相关文章推荐
- 伪静态 url 重写 apache linux
- 如何启动mac的apache服务
- 【Apache Solr系列之五】使用Solr客户端SolrJ实现多层面统计
- 运行apache thrift的错误解决
- APACHE配置文件中文详解
- Debian Rails3 Apache2 Passenger 部署
- 11个经典的Apache开源项目
- apache vhost
- Apache支持.htaccess
- Caused by: java.net.UnknownHostException: struts.apache.org
- java压缩与解压缩文件(利用apache的ant.jar)
- Mac电脑下搭建Apache web服务器
- Apache Hive安装及配置
- apache安装configure: error: APR not found. Please read the documentation
- Apache Commons DbUtils 快速上手
- Apache中如何使用ErrorDocument标签设定统一的错误页面
- apache mina初学,绘制的socket初始化时依赖的类图
- Installing Apache, PHP, and MySQL on Mac OS X
- Apache Commons工具集简介
- php、Apache网站常用安全配置