请问如何用java调用FTP程序,并截获标准输入输出流处理 请问如何用java调用FTP程序,并截获标准输入输出流处理 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看看有没有帮助private void ftpUpload(String localfile,String remotefile,String path) throws Exception { //连接ftp服务器 sun.net.ftp.FtpClient ftpClient = new sun.net.ftp.FtpClient(); ftpClient.openServer("192.168.2.10"); ftpClient.login("root", "root"); //path是ftp服务下主目录的子目录 // if (path.length() != 0) ftpClient.cd(path); //用2进制上传、下载 ftpClient.cd(path); ftpClient.binary(); //上传文件 TelnetOutputStream os = null; FileInputStream is = null; try { java.io.File file_in = new java.io.File(localfile); // if (!file_in.exists()) return -1; // if (file_in.length()==0) return -2; os = ftpClient.put(remotefile); // result = file_in.length(); is = new FileInputStream(file_in); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); } } finally { if (is != null) { is.close(); } if (os != null) { os.close(); } } //传送完毕 关闭ftp连接 try { if (ftpClient != null) { ftpClient.closeServer(); } } catch (IOException e) { e.printStackTrace(); } } 半抄 半写的 有用就拿走package com.excellence.pigeonhole.util;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import org.apache.commons.net.ProtocolCommandEvent;import org.apache.commons.net.ProtocolCommandListener;import org.apache.commons.net.ftp.FTP;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPReply;import com.excellence.pigeonhole.general.Information;public class FtpUtil { private static FtpUtil ftpu = null; private FTPClient ftp = null; private String userName = Information.FTP_USERNAME_STRING; private String passWord = Information.FTP_PASSWORD_STRING; private int port = Integer.parseInt(Information.FTP_PORT_STRING); private String ip = Information.FTP_IP_STRING; private String root = Information.FTP_ROOT_STRING; private FtpUtil() { initFtp(); } public void initFtp() { ftp = new FTPClient(); ftp.addProtocolCommandListener(new ProtocolCommandListener() { String message = ""; public void protocolCommandSent(ProtocolCommandEvent event) { message = event.getMessage(); if ("pass".equalsIgnoreCase(event.getCommand())) { // 隐藏密码 message = message.substring(0, message.indexOf(" ")) + " ******"; } System.out.println(message); } public void protocolReplyReceived(ProtocolCommandEvent event) { System.out.println(event.getMessage()); } }); try { int reply; ftp.connect(ip, port); reply = ftp.getReplyCode(); System.out.println("reply code: " + reply); String msg; if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); msg = "FTP服务器拒绝访问"; System.out.println(msg); } if (!ftp.login(userName, passWord)) { ftp.logout(); msg = "用户名或者密码错误"; System.out.println(msg); } String[] folders = root.split("/"); for (int i = 0; i < folders.length; i++) { String folder = folders[i]; if (folder.trim().length() == 0) continue; if (!ftp.changeWorkingDirectory(folder)) { if (ftp.makeDirectory(folder)) { if (!ftp.changeWorkingDirectory(folder)) { msg = "不能转到" + folder + "目录"; System.out.println(msg); } } else { msg = "不能在ftp服务器上建立" + folder + "目录"; System.out.println(msg); } } } System.out.println("Current working directory: " + ftp.printWorkingDirectory()); } catch (IOException e) { e.printStackTrace(); System.out.println("connectFtp发生异常" + e.getMessage()); disconnectFtp(); } } public static FtpUtil getInstance() { if (ftpu == null) { ftpu = new FtpUtil(); } return ftpu; } public void upLoadFile(String realPath, String fileName) { if (ftp == null) { initFtp(); } File file = new File(realPath); if (!file.exists()) { System.out.println("路径无效:" + realPath); return; } if (!file.isFile()) { System.out.println("路径指向的不是一个有效的文件:" + realPath); return; } String filename = fileName; try { filename = new String(fileName.getBytes(), "iso8859_1"); } catch (UnsupportedEncodingException e) { filename = fileName; } InputStream is = null; try { is = new FileInputStream(file); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.setControlEncoding("GBK"); ftp.storeFile(filename, is); // disconnectFtp(); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void upLoadFile(InputStream is, String fileName) { if (ftp == null) { initFtp(); } String filename = fileName; try { filename = new String(fileName.getBytes(), "iso8859_1"); } catch (UnsupportedEncodingException e) { filename = fileName; } try { ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.setControlEncoding("GBK"); ftp.storeFile(filename, is); // disconnectFtp(); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void disconnectFtp() { if (ftp != null && ftp.isConnected()) { try { ftp.disconnect(); ftp = null; } catch (IOException e) { } } }} RSA 公钥解密时候报错 BadPaddingException异常,麻烦大家看看 请问,JFACE,从一个对话框传一个值到到下一个对话框怎样做或实现? ImageIcon ????????????? 请问一个oracle服务器客户端的问题;答完结贴! 一个关于Scanner类的问题 各位兄弟们帮忙看看我这个菜鸟写的东西? 点击右键如何触发左键的事件??? 大家好!还是关于JBuilder字体问题! 谁要《软件设计文档国家标准》? 一个非常非常棘手的问题,请教各位!!!!!!!! 自定义插入符问题 wsdl 只给了一个url 地址。没有wsdl文件。
private void ftpUpload(String localfile,String remotefile,String path) throws Exception
{
//连接ftp服务器
sun.net.ftp.FtpClient ftpClient = new sun.net.ftp.FtpClient();
ftpClient.openServer("192.168.2.10");
ftpClient.login("root", "root");
//path是ftp服务下主目录的子目录
// if (path.length() != 0) ftpClient.cd(path);
//用2进制上传、下载
ftpClient.cd(path);
ftpClient.binary();
//上传文件
TelnetOutputStream os = null;
FileInputStream is = null;
try {
java.io.File file_in = new java.io.File(localfile);
// if (!file_in.exists()) return -1;
// if (file_in.length()==0) return -2;
os = ftpClient.put(remotefile);
// result = file_in.length();
is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
//传送完毕 关闭ftp连接
try
{
if (ftpClient != null)
{
ftpClient.closeServer();
}
} catch (IOException e) {
e.printStackTrace();
}
}
package com.excellence.pigeonhole.util;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;import org.apache.commons.net.ProtocolCommandEvent;
import org.apache.commons.net.ProtocolCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;import com.excellence.pigeonhole.general.Information;public class FtpUtil {
private static FtpUtil ftpu = null;
private FTPClient ftp = null;
private String userName = Information.FTP_USERNAME_STRING;
private String passWord = Information.FTP_PASSWORD_STRING;
private int port = Integer.parseInt(Information.FTP_PORT_STRING);
private String ip = Information.FTP_IP_STRING;
private String root = Information.FTP_ROOT_STRING; private FtpUtil() {
initFtp();
} public void initFtp() {
ftp = new FTPClient();
ftp.addProtocolCommandListener(new ProtocolCommandListener() {
String message = ""; public void protocolCommandSent(ProtocolCommandEvent event) {
message = event.getMessage();
if ("pass".equalsIgnoreCase(event.getCommand())) {
// 隐藏密码
message = message.substring(0, message.indexOf(" "))
+ " ******";
}
System.out.println(message);
} public void protocolReplyReceived(ProtocolCommandEvent event) {
System.out.println(event.getMessage());
}
});
try {
int reply;
ftp.connect(ip, port); reply = ftp.getReplyCode();
System.out.println("reply code: " + reply);
String msg;
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
msg = "FTP服务器拒绝访问";
System.out.println(msg);
}
if (!ftp.login(userName, passWord)) {
ftp.logout();
msg = "用户名或者密码错误";
System.out.println(msg);
}
String[] folders = root.split("/");
for (int i = 0; i < folders.length; i++) {
String folder = folders[i];
if (folder.trim().length() == 0)
continue; if (!ftp.changeWorkingDirectory(folder)) {
if (ftp.makeDirectory(folder)) {
if (!ftp.changeWorkingDirectory(folder)) {
msg = "不能转到" + folder + "目录";
System.out.println(msg);
}
} else {
msg = "不能在ftp服务器上建立" + folder + "目录";
System.out.println(msg);
}
}
}
System.out.println("Current working directory: "
+ ftp.printWorkingDirectory());
} catch (IOException e) {
e.printStackTrace();
System.out.println("connectFtp发生异常" + e.getMessage());
disconnectFtp();
}
} public static FtpUtil getInstance() {
if (ftpu == null) {
ftpu = new FtpUtil();
}
return ftpu;
} public void upLoadFile(String realPath, String fileName) {
if (ftp == null) {
initFtp();
}
File file = new File(realPath);
if (!file.exists()) {
System.out.println("路径无效:" + realPath);
return;
}
if (!file.isFile()) {
System.out.println("路径指向的不是一个有效的文件:" + realPath);
return;
}
String filename = fileName;
try {
filename = new String(fileName.getBytes(), "iso8859_1");
} catch (UnsupportedEncodingException e) {
filename = fileName;
}
InputStream is = null;
try {
is = new FileInputStream(file);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.setControlEncoding("GBK");
ftp.storeFile(filename, is);
// disconnectFtp();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public void upLoadFile(InputStream is, String fileName) {
if (ftp == null) {
initFtp();
}
String filename = fileName;
try {
filename = new String(fileName.getBytes(), "iso8859_1");
} catch (UnsupportedEncodingException e) {
filename = fileName;
}
try {
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.setControlEncoding("GBK");
ftp.storeFile(filename, is);
// disconnectFtp();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public void disconnectFtp() {
if (ftp != null && ftp.isConnected()) {
try {
ftp.disconnect();
ftp = null;
} catch (IOException e) {
}
}
}
}