项目需要,网上搜了搜,很多,但问题也不少,估计转来转去,少了不少东西,而且也情况也不太一样。没办法,只能自己去写一个。
-
- 一, 安装sserv-u ftp服务器 版本10.1.0.1
-
- 我所设服务器配置:
-
- 用户名:shiyanming
-
- 密码:123
-
- 端口:21
-
- 跟目录:D:/ftpindex
-
-
-
-
-
- 二、所需jar包:common-net-1.4.1.jar
-
- jakarta-oro-2.0.8.jar
-
- 注意:第二个jar包必须要存在,不然在列举ftp服务器中文件是出错
-
-
-
-
-
- 三、中文传输问题
-
- 四、具体程序 com.ftp. SeforgeFtpUtils.java
-
-
-
- package com.ftp;
-
- import java.io.File;
-
- import java.io.FileInputStream;
-
- import java.io.FileNotFoundException;
-
- import java.io.FileOutputStream;
-
- import java.io.IOException;
-
- import java.io.InputStream;
-
- import java.io.OutputStream;
-
- import java.net.URLEncoder;
-
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.commons.net.ftp.FTP;
-
- import org.apache.commons.net.ftp.FTPClient;
-
- import org.apache.commons.net.ftp.FTPClientConfig;
-
- import org.apache.commons.net.ftp.FTPFile;
-
- import org.apache.commons.net.ftp.FTPReply;
-
-
- public class SeforgeFtpUtils {
-
-
-
- public boolean uploadFile(String url, int port, String username,
-
- String password, String path, String filename, InputStream input) {
-
-
-
-
-
-
-
- boolean success = false;
-
-
-
- FTPClient ftp = new FTPClient();
-
- try {
-
- int reply;
-
-
-
-
-
- ftp.connect(url, port);
-
-
-
- ftp.setControlEncoding("GBK");
-
- FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
-
- conf.setServerLanguageCode("zh");
-
-
-
- ftp.login(username, password);
-
-
-
- reply = ftp.getReplyCode();
-
-
-
- if (!FTPReply.isPositiveCompletion(reply)) {
-
- ftp.disconnect();
-
- System.out.println("连接服务器失败");
-
- return success;
-
- }
-
- System.out.println("登陆服务器成功");
-
- ftp.changeWorkingDirectory(path);
-
- FTPFile[] fs = ftp.listFiles();
-
- System.out.println(fs.length);
-
- System.out.println(filename);
-
- String filename1 = SeforgeFtpUtils.changeName(filename, fs);
-
-
-
-
-
- String filename2 = new String(filename1.getBytes("GBK"),
-
- "ISO-8859-1");
-
- String path1 = new String(path.getBytes("GBK"), "ISO-8859-1");
-
-
-
- ftp.changeWorkingDirectory(path1);
-
-
-
-
-
-
-
- ftp.setFileType(FTP.BINARY_FILE_TYPE);
-
-
- ftp.storeFile(filename2, input);
-
-
-
- input.close();
-
-
-
- ftp.logout();
-
-
-
- success = true;
-
- System.out.println("上传成功。。。。。。");
-
- } catch (IOException e) {
-
- e.printStackTrace();
-
- } finally {
-
- if (ftp.isConnected()) {
-
- try {
-
- ftp.disconnect();
-
- } catch (IOException ioe) {
-
- }
-
- }
-
- }
-
- return success;
-
- }
-
-
-
- public boolean deleteFile(String url, int port, String username,
-
- String password, String path, String filename) {
-
-
-
-
-
-
-
- boolean success = false;
-
-
-
- FTPClient ftp = new FTPClient();
-
- try {
-
- int reply;
-
-
-
-
-
- ftp.connect(url, port);
-
-
-
- ftp.setControlEncoding("GBK");
-
- FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
-
- conf.setServerLanguageCode("zh");
-
-
-
- ftp.login(username, password);
-
-
-
- reply = ftp.getReplyCode();
-
-
-
- if (!FTPReply.isPositiveCompletion(reply)) {
-
- ftp.disconnect();
-
- System.out.println("连接服务器失败");
-
- return success;
-
- }
-
- System.out.println("登陆服务器成功");
-
- String filename2 = new String(filename.getBytes("GBK"),
-
- "ISO-8859-1");
-
- String path1 = new String(path.getBytes("GBK"), "ISO-8859-1");
-
-
-
- ftp.changeWorkingDirectory(path1);
-
- FTPFile[] fs = ftp.listFiles();
-
- ftp.deleteFile(filename2);
-
- ftp.logout();
-
- success=true;
-
- } catch (IOException e) {
-
- System.out.println(e);
-
- } finally {
-
- if (ftp.isConnected()) {
-
- try {
-
- ftp.disconnect();
-
- } catch (IOException ioe) {
-
- }
-
- }
-
- }
-
- return success;
-
- }
-
-
-
-
-
- public static boolean downFile(String ip, int port, String username,
-
- String password, String remotePath, String fileName,
-
- OutputStream outputStream, HttpServletResponse response) {
-
- boolean success = false;
-
- FTPClient ftp = new FTPClient();
-
- try {
-
- int reply;
-
- ftp.connect(ip, port);
-
-
-
- ftp.setControlEncoding("GBK");
-
- FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
-
- conf.setServerLanguageCode("zh");
-
-
-
- ftp.login(username, password);
-
- ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
-
- reply = ftp.getReplyCode();
-
- if (!FTPReply.isPositiveCompletion(reply)) {
-
- ftp.disconnect();
-
- return success;
-
- }
-
- System.out.println("登陆成功。。。。");
-
- ftp.changeWorkingDirectory(remotePath);
-
- FTPFile[] fs = ftp.listFiles();
-
-
-
- for (int i = 0; i < fs.length; i++) {
-
- FTPFile ff = fs[i];
-
- if (ff.getName().equals(fileName)) {
-
- String filename = fileName;
-
-
-
- response.setHeader("Content-disposition",
-
- "attachment;filename="
-
- + URLEncoder.encode(filename, "utf-8"));
-
-
-
- ftp.retrieveFile(new String(ff.getName().getBytes("GBK"),
-
- "ISO-8859-1"), outputStream);
-
- outputStream.flush();
-
- outputStream.close();
-
- }
-
- }
-
- ftp.logout();
-
- success = true;
-
- } catch (IOException e) {
-
- e.printStackTrace();
-
- } finally {
-
- if (ftp.isConnected()) {
-
- try {
-
- ftp.disconnect();
-
- } catch (IOException ioe) {
-
- }
-
- }
-
- }
-
- return success;
-
- }
-
-
-
- public static boolean isDirExist(String fileName, FTPFile[] fs) {
-
- for (int i = 0; i < fs.length; i++) {
-
- FTPFile ff = fs[i];
-
- if (ff.getName().equals(fileName)) {
-
- return true;
-
- }
-
- }
-
- return false;
-
- }
-
-
-
- public static String changeName(String filename, FTPFile[] fs){
-
- int n = 0;
-
-
-
- StringBuffer filename1 = new StringBuffer("");
-
- filename1 = filename1.append(filename);
-
- System.out.println(filename1);
-
- while (isDirExist(filename1.toString(), fs)) {
-
- n++;
-
- String a = "[" + n + "]";
-
- System.out.println("字符串a的值是:" + a);
-
- int b = filename1.lastIndexOf(".");
-
- int c = filename1.lastIndexOf("[");
-
- if (c < 0) {
-
- c = b;
-
- }
-
- StringBuffer name = new StringBuffer(filename1.substring(0, c));
-
- StringBuffer suffix = new StringBuffer(filename1.substring(b + 1));
-
- filename1 = name.append(a).append(".").append(suffix);
-
- }
-
- return filename1.toString();
-
- }
-
-
-
- public static void main(String[] args) throws FileNotFoundException {
-
- String path = "";
-
- File f1 = new File("C:\\新.txt");
-
- String filename = f1.getName();
-
- System.out.println(filename);
-
-
-
-
-
-
-
-
-
-
-
- SeforgeFtpUtils a = new SeforgeFtpUtils();
-
- a.deleteFile("192.168.0.100", 21, "shiyanming", "123", path, filename);
-
- }
-
- }
-
- 上边程序完成了文件的上传和下载
-
-
-
- 重命名判断问题,如果重名,在后边加(n)。
-
-
-
- 存在问题:如果jsp页面中读取from表单的值,只能获取主机的地址,不能上传客户端文件。
-
-
-
-
-
-
-
-
-
-
-
- 3.1、 默认情况下,FtpClient使用的是UTF_8字符集作为服务器通讯的编码集。而FTP服务器SERV-U在windowsXP上,使用GBK字符集作为服务器通讯。
-
-
-
-
-
-
- ftp.setControlEncoding("GBK");
-
- FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
-
- conf.setServerLanguageCode("zh");
-
-
-
- 3.2、同时还要设置服务器
-
- 设置步骤:
-
- 1、打开Serv-U 8.0控制台,点击“限制和设置”--“为域配置高级FTP命令设置和行为”。
-
- 2、在FTP设置中找到OPTS UTF8命令,右击禁用此命令。3、点击下面的“全局属性”。
-
- 4、在出来的FTP命令属性选项卡中,“高级选项”里,把“对所有收发的路径和文件名使用UFT-8编码”前面的钩去掉!
-
- 5、以后再上传中文文件,就不会出现乱码问题啦。
ftp上传下载 java FTPClient (zhuan)
原文:http://www.cnblogs.com/swugogo/p/4614963.html