ftp文件共享 java_ftp服务器上传下载共享文件

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) {

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值