上传
/**
* fit 文件上传
*
* @param ip 服务器地址
* @param userName 用户名
* @param passWord 密 码
* @param pathName 文件路径
* @param filepath 源文件路径
* @param nameFile 新文件名称
*/
public static void testUpload(String ip, int port, String userName,
String passWord,String pathName, byte[] bs, String nameFile) {
FTPClient ftpClient = new FTPClient();
InputStream inputStream = null;
inputStream = new ByteArrayInputStream(bs);
System.out.println("上传文件....");
try {
ftpClient.connect(ip);
ftpClient.setDefaultPort(port);
ftpClient.login(userName, passWord);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.makeDirectory(pathName);
ftpClient.changeWorkingDirectory(pathName);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
if (bs != null) {
ftpClient.storeFile(nameFile,inputStream);
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(inputStream);
System.out.println("文件上传成功··");
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
}
下载
/**
* 文件下载
* @param ip
* @param port
* @param userName
* @param passWord
* @param pathName
* @param filepath
* @param nameFile
* @param request
* @param response
*/
public static void testDownload(String ip, int port, String userName,
String passWord, String pathName, String FileName, String realName,HttpServletRequest request,
HttpServletResponse response) {
FTPClient ftpClient = new FTPClient();
FileOutputStream fos = null;
System.out.println("下载文件....");
try {
ftpClient.connect(ip);
ftpClient.setDefaultPort(port);
ftpClient.login(userName, passWord);
ftpClient.changeWorkingDirectory(pathName);// 转移到FTP服务器目录
int reply;
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
}
FTPFile[] fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(realName)) {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String checkBrowse =request.getHeader("user-agent");
//request.getHeader(“User-agent”)返回客户端浏览器的版本号、类型
String FIREFOX ="Firefox";
Pattern p =Pattern.compile(FIREFOX,Pattern.MULTILINE);
Matcher m=p.matcher(checkBrowse);
boolean is1 = m.find();
String fileName = new String(FileName.getBytes("utf-8"), "ISO8859_1");
if(!is1){
fileName = URLEncoder.encode(FileName,"UTF-8");
}
response.setContentType("application/binary;charset=ISO8859-1");
response.setHeader("Content-disposition", "attachment; filename="+ fileName);
request.setCharacterEncoding("UTF-8");
response.setHeader("Content-Length", String.valueOf(ff.getSize()));
ftpClient.retrieveFile(ff.getName(),response.getOutputStream());
break;
}
}
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
IOUtils.closeQuietly(fos);
System.out.println("文件下载成功····");
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
}
删除
/**
*
* @param ip
* @param port
* @param userName
* @param passWord
* @param pathName
* @param filepath
* @param nameFile
*/
public static void testDeleteFile(String ip, int port, String userName,
String passWord, String pathName, String filepath, String nameFile) {
FTPClient ftpClient = new FTPClient();
System.out.println("删除文件.......");
try {
ftpClient.connect(ip);
ftpClient.login(userName, passWord);
ftpClient.changeWorkingDirectory(pathName);// 转移到FTP服务器目录
ftpClient.deleteFile(nameFile);
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
System.out.println("文件删除成功····");
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
}
下载前检测
public static boolean hashFIle(String ip, int port, String userName,
String passWord, String ownerId, String FileName, String realName) {
FTPClient ftpClient = new FTPClient();
boolean has = false;
System.out.println("检测文件....");
try {
ftpClient.connect(ip);
ftpClient.setDefaultPort(port);
ftpClient.login(userName, passWord);
ftpClient.changeWorkingDirectory(ownerId);// 转移到FTP服务器目录
// ftpClient.mkd(pathName);
int reply;
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
}
FTPFile[] fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(realName)) {
has = true;
}
}
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
System.out.println("文件已经找到····");
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return has;
}