* 功能描述: 通过浏览器的方式下载ftp文件
* @Param: [url, port, username, password, ftpFilePath, fileName, response]
* 文件host地址,端口,用户名,密码,ftp虚拟路径,文件名称(包含后缀)
public String downloadFtp(String url,String port,String username,Stirng password,String ftpFilePath,String fileName,HttpServletResponse response) {
String ip = url.substring(0,url.indexOf("/"));
FtpUtil ftpUtil = null;
try {
ftpUtil = new FtpUtil(ip, port, username, password);
Pair<Boolean, String> pair = ftpUtil.downloadFile(ftpFilePath, fileName, response);
return pair.getRight();
} catch (Exception e) {
} finally {
if (ftpUtil != null) {
ftpUtil.close();
}
}
}
图片流方式实现页面预览FTP图片
public byte[] getImage(String url,String port,String username,Stirng password,String ftpFilePath,String fileName){
String ip = url.substring(0,url.indexOf("/"));
FtpUtil ftpUtil = null;
byte[] b = new byte[0];
try {
ftpUtil = new FtpUtil(ip, port, username, password);
b = ftpUtil.getImage(ftpFilePath,fileName);
return b;
} catch (Exception e) {
} finally {
if (ftpUtil != null) {
ftpUtil.close();
}
}
return b;
}
连接Ftp代码
public FtpUtil(String hostName, int port, String userName, String password) throws IOException {
ftpClient = new FTPClient();
//设置编码格式,FTP Server 协议里规定文件名编码是iso-8859-1
ftpClient.setControlEncoding("GBK");
//设置传输命令的超时
ftpClient.setDefaultTimeout(20000);//毫秒
//设置两个服务连接超时时间
ftpClient.setConnectTimeout(10000);//毫秒
//被动模式下设置数据传输的超时时间
ftpClient.setDataTimeout(15000);//毫秒
//连接FTP
ftpClient.connect(hostName, port);
//更加账户密码登录服务
ftpClient.login(userName, password);
//被动模式
ftpClient.enterLocalPassiveMode();
}
FTP文件下载代码
public Pair<Boolean, String> downloadFile(String ftpFilePath, String fileName, HttpServletResponse response) {
InputStream input = null;
OutputStream out = null;
try {
response.reset();
//此处你就根据你要下载的类型去设置即可,比如我下载.avi格式的文件,可以设置response.setContentType("video/avi");
response.setContentType("application/download");
//解决中文不能生成文件(包含空格)
response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20") + "\"");
//传输模式
ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
// 设置以二进制流的方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//进入目录
ftpClient.changeWorkingDirectory(ftpFilePath);
FTPFile[] files = ftpClient.listFiles();
if (files.length < 1) {
return Pair.of(false, "目录为空");
}
boolean fileExist = false;
boolean downloadFlag = false;
for (FTPFile ftpFile : files) {
String ftpFileName = ftpFile.getName();
if (fileName.equals(ftpFileName)) {
fileExist = true;
input = ftpClient.retrieveFileStream(fileName);
out = response.getOutputStream();
int len;
byte[] bytes = new byte[1024];
while ((len = input.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
out.flush();
downloadFlag = true;
break;
}
}
if (!fileExist) {
return Pair.of(false, "FTP服务器上文件不存在");
}
return Pair.of(downloadFlag, downloadFlag ? "下载成功" : "下载失败");
} catch (IOException e) {
e.printStackTrace();
return Pair.of(false, "下载文件异常");
} finally {
try {
if (out != null) {
out.close();
}
if (input != null) {
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
图片流代码
public byte[] getImage(String ftpFilePath, String fileName){
byte[] byteArray = new byte[0];
InputStream input = null;
try{
//进入目录
ftpClient.changeWorkingDirectory(ftpFilePath);
FTPFile[] files = ftpClient.listFiles();
if (files.length < 1) {
return byteArray;
}
boolean fileExist = false;
boolean downloadFlag = false;
for (FTPFile ftpFile : files) {
String ftpFileName = ftpFile.getName();
if (fileName.equals(ftpFileName)) {
// 获得文件大小
int size = (int) ftpFile.getSize();
byteArray = new byte[size];
ByteArrayOutputStream os = new ByteArrayOutputStream();
// 写入输出流
ftpClient.retrieveFile(fileName,os);
byteArray = os.toByteArray();
return byteArray;
}
}
}catch (Exception e){
log.info("错误信息:{}",e.getMessage());
}
return byteArray;
}
最后是FTP链接关闭
public void close() {
try {
if (ftpClient != null && ftpClient.isConnected()) {
ftpClient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
图片预览前端代码(ajax请求返回乱码)
var url =local host:1103/ftp/downloadFromFtp?url="+url+"&por=21&username=user&password=123&ftpFilePath=/abc/123/&fileName=123.doc");
window.location.href =url;