packagecom.test.domi.common.utils;importorg.apache.commons.net.ftp.FTP;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPFile;importorg.apache.commons.net.ftp.FTPReply;importjavax.servlet.http.HttpServletResponse;import java.io.*;public classFtpUtil {public static boolean uploadFile(String host, intport, String username, String password,
String basePath, String filePath, String filename, InputStream input) {
FTPClient ftp= newFTPClient();
Boolean result= false;try{//连接FTP服务器
ftp.connect(host, port);//如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
ftp.login(username, password);//登录
int reply =ftp.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();returnresult;
}//切换到上传目录
String path = basePath +filePath;if (!ftp.changeWorkingDirectory(path)) {//如果目录不存在则创建目录
String[] dirs = path.split("/");
StringBuffer tempPath= newStringBuffer();for(String dir : dirs) {if (null == dir || "".equals(dir)) {continue;
}
tempPath.append("/").append(dir);if (!ftp.changeWorkingDirectory(tempPath.toString())) {if (!ftp.makeDirectory(tempPath.toString())) {returnresult;
}else{
ftp.changeWorkingDirectory(tempPath.toString());
}
}
}
}//设置上传文件为二进制类型
ftp.setFileType(FTP.BINARY_FILE_TYPE);//上传文件默认是10M大小//客户端编码等基本设置//FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);设置unix下下载大小的限制
if (!ftp.storeFile(filename, input)) {returnresult;
}
input.close();
ftp.logout();
result= true;
}catch(IOException e) {
e.printStackTrace();
}finally{if(ftp.isConnected()) {try{
ftp.disconnect();
}catch(IOException ioe) {
}
}
}returnresult;
}public static void udownLoadFile(String host, intport, String username, String password,
String remotePath,String fileName, HttpServletResponse response) {
FTPClient ftp= newFTPClient();try{intreply;
ftp.connect(host, port);//如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
ftp.login(username, password);//登录
reply =ftp.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();return;
}
ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
FTPFile[] fs =ftp.listFiles();for(FTPFile ff : fs) {
String name=ff.getName();if(name.equals(fileName)) {
response.setContentType("application/form-data");
String realFileName= "我的重命名" + name.substring(name.lastIndexOf("."));//"inline; filename=\""//+ URLEncoder.encode(title, "UTF-8")//+ ".txt\"");
response.setHeader("Content-disposition","attachment; filename=" + new String(realFileName.getBytes("utf-8"), "ISO8859-1"));
response.setCharacterEncoding("utf-8");
ftp.retrieveFile(ff.getName(), response.getOutputStream());
}
}
ftp.logout();
}catch(Exception e) {
e.printStackTrace();
}finally{if(ftp.isConnected()) {try{
ftp.disconnect();
}catch(IOException ioe) {
}
}
}
}
}