public class FTPUtil {
private FTPClient ftpClient;
public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;
static Logger logger = org.slf4j.LoggerFactory.getLogger(FTPUtil.class);
/**
* 使用详细信息进行ftp服务器连接: <br>
* 〈功能详细描述〉
*
* @param host :服务器地址名称
* @param port :端口号
* @param user :用户名
* @param password :appendFile
* @param path :转移到FTP服务器目录
* @throws SocketException
* @throws IOException
*/
public void connectServer(String host, int port, String user, String password, String path) throws SocketException,
IOException {
ftpClient = new FTPClient();
ftpClient.connect(host, port);
ftpClient.login(user, password);
if (path != null && path.length() != 0) {
boolean changeok = ftpClient.changeWorkingDirectory(path);
if (!changeok) {
// 变更目录失败,生成目录
ftpClient.mkd(path);
ftpClient.changeWorkingDirectory(path);
}
}
ftpClient.setBufferSize(1024);// 设置上传缓存大小
ftpClient.setControlEncoding("UTF-8");// 设置编码
ftpClient.setFileType(BINARY_FILE_TYPE);// 设置文件类型
}
/**
* 关闭连接
*
* @throws IOException
*/
public void closeServer() throws IOException {
if (ftpClient != null && ftpClient.isConnected()) {
ftpClient.logout();// 退出FTP服务器
ftpClient.disconnect();// 关闭FTP连接
}
}
/**
* 删除服务器上的文件
*
* @param pathName
* @return
* @throws IOException
*/
public boolean deleteFile(String pathName) throws IOException {
return ftpClient.deleteFile(pathName);
}
/**
* 以流的方式上传文件到ftp服务器: <br>
* 〈功能详细描述〉
*
* @param iStream :输入流
* @param remoteFileName :上传到服务器的文件名称
* @return
* @throws IOException
*/
public boolean uploadFile(InputStream iStream, String remoteFileName) throws IOException {
boolean flag = false;
try {
// 将文件名转码,防止中文文件名上传失败失败问题,
flag = ftpClient.storeFile(new String(remoteFileName.getBytes("UTF-8"), "iso-8859-1"), iStream);
} catch (IOException e) {
flag = false;
logger.error("uploadFile 文件转码失败 :" + e);
return flag;
} finally {
if (iStream != null) {
iStream.close();
}
}
return flag;
}
/**
* 从ftp服务器上下载文件到本地输入流: <br>
* 〈功能详细描述〉
*
* @param sourceFileName :服务器资源文件名称
* @param oStream :本地输入流
* @throws IOException
*/
public void retrieveFile(String sourceFileName, OutputStream oStream) throws IOException {
ftpClient.retrieveFile(new String(sourceFileName.getBytes("UTF-8"), "iso-8859-1"), oStream);
}
/**
* 上传文件,从ftp服务器上拿到OutputStream,往流中写入数据: <br>
* 〈功能详细描述〉
*
* @param remoteFileName
* @return
* @throws IOException
*/
public OutputStream storeUniqueFileStream(String remoteFileName) throws IOException {
OutputStream oStream = ftpClient.storeUniqueFileStream(remoteFileName);
return oStream;
}
public void downLoad(String remoteFileName, String fileName) {
try {
ftpClient.retrieveFile("/" + remoteFileName, new FileOutputStream(fileName));
} catch (FileNotFoundException e) {
logger.error(e.getMessage(), e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
/**
* 从ftp服务器上获取上传的文件 <br>
* 〈功能详细描述〉
*
* @return
*/
public List<FTPFile> getListFiles(String ftpFilePath) {
try {
List<FTPFile> fileList = Arrays.asList(ftpClient.listFiles(ftpFilePath));
return fileList;
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
}
}
文件下载设置相应头
public void downLoadCsv(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws BizServiceException {
Long batchId = Long.parseLong(request.getParameter("batchId")); // 获取批次id
String fileName = batchId + ".csv";// 文件名
// 设置响应头
response.setContentType("application/x-download");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
// 文件下载
try {
hService.downLoadFile(fileName, response.getOutputStream());
} catch (IOException e) {
logger.error("下载文件异常", e);
throw new BizServiceException("文件下载异常");
}
}