工作需要,java实现了一个FTP上传文件的例子。
代码如下:
首先把FTP使用过程中需要的一些信息包装成一个类。
FTPModel.java
import java.io.InputStream;
import java.io.Serializable;
public class FTPModel implements Serializable{
private String url;
private int port;
private String username;
private String password;
private String path;
private String filename;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
}
FTP连接需要的配置信息
ftp.properties
ftp.url = 172.16.100.50
ftp.port = 22
ftp.username = username
ftp.password = password
ftp.path = /home/ihome/House/
使用spring来读取配置文件
spring配置信息
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/classes/ftp.properties</value>
</list>
</property>
</bean>
假设要在ChomeFileUpload.java中读取配置信息
ChomeFileUpload.java中添加代码
private String url;
private int port;
private String username;
private String password;
private String path;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
spring配置文件中配置信息
<bean id="FileUpload_" class="com.agree.ihome.action.paramanage.ChomeFileUpload" scope="prototype" >
<property name="url" value="${ftp.url}"></property>
<property name="port" value="${ftp.port}"></property>
<property name="username" value="${ftp.username}"></property>
<property name="password" value="${ftp.password}"></property>
<property name="path" value="${ftp.path}"></property>
</bean>
FTPUtil.java
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import com.agree.ihome.models.FTPModel;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class FTPUtil {
/**
* ftp方式连接
*/
public static boolean uploadFileByFtp(FTPModel ftpModel,InputStream input) throws Exception{
boolean success = false;
FTPClient ftp = new FTPClient();
try {
ftp.connect(ftpModel.getUrl(), ftpModel.getPort());
ftp.login(ftpModel.getUsername(), ftpModel.getPassword());
int reply = ftp.getReplyCode();//获取状态码
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(ftpModel.getPath());
ftp.storeFile(ftpModel.getFilename(), input);
input.close();
ftp.logout();
success = true;
} catch (Exception e) {
e.printStackTrace();
}finally{
if (ftp.isConnected()) {
try {
ftp.disconnect();//断开连接
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* sftp方式连接
* @param ftpModel
* @param input
* @return
* @throws Exception
*/
public static boolean uploadFileBySFtp(FTPModel ftpModel,InputStream input) throws Exception{
boolean success = false;
ChannelSftp sftp = null;
Channel channel = null;
try {
JSch jsch = new JSch();
Session sshSession = jsch.getSession(ftpModel.getUsername(), ftpModel.getUrl(), ftpModel.getPort());
sshSession.setPassword(ftpModel.getPassword());
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
sftp.cd(ftpModel.getPath());
sftp.put(input,ftpModel.getFilename());
success =true;
} catch (Exception e) {
e.printStackTrace();
}finally{
channel.disconnect();
input.close();
}
return success;
}
}
THE END