ftp默认端口21
1转化为base64
public static String download(String ftpUrl,int index){
//ftpUrl :文件夹路径 afzh:图片路径
FTPClient ftpClient = new FTPClient();
int temp=index+1;
InputStream inputStream = null;
String re=null;
try {
ftpClient.connect(ip,21);//ip地址,端口号
ftpClient.login("", "");//账户,密码
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//是否成功登录服务器
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
}
ftpClient.enterLocalPassiveMode();--重要 不然linux上不行
//跳转到指定目录
ftpClient.changeWorkingDirectory(ftpUrl);
//5.遍历下载的目录
FTPFile[] fs = ftpClient.listFiles();
//数组下标
int count=0;
String picType;
for (FTPFile ff : fs){
//解决中文乱码问题,两次解码
byte[] bytes=ff.getName().getBytes("iso-8859-1");
String fileName=new String(bytes,"utf-8");
/*if(sfzh.equals(fileName)){
inputStream = ftpClient.retrieveFileStream(fileName);
}*/
if(count==temp){
inputStream = ftpClient.retrieveFileStream(fileName);
}
count++;
}
if (inputStream != null) {
byte[] data=null;
ByteArrayOutputStream outStream =new ByteArrayOutputStream();
data=new byte[inputStream.available()];
int len=0;
while((len=inputStream.read(data))!=-1){
outStream.write(data,0,len);
}
data=outStream.toByteArray();
Base64.Encoder encoder= Base64.getEncoder();
re=encoder.encodeToString(data);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
if(null != inputStream){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return re;
}
2流下载图片
@GetMapping("/xx/getXXX")
public static void download1(String ftpUrl,int index,HttpServletResponse response){
//ftpUrl :文件夹路径 afzh:图片路径
FTPClient ftpClient = new FTPClient();
int temp=index+1;
InputStream inputStream = null;
String re=null;
OutputStream outStream = null;
try {
ftpClient.connect("",);//ip地址,端口号
ftpClient.login("", "");//账户,密码
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//是否成功登录服务器
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
}
ftpClient.enterLocalPassiveMode();--重要 不然linux上不行
//跳转到指定目录
ftpClient.changeWorkingDirectory(ftpUrl);
//5.遍历下载的目录
FTPFile[] fs = ftpClient.listFiles();
//数组下标
int count=0;
String picType;
for (FTPFile ff : fs){
//解决中文乱码问题,两次解码
byte[] bytes=ff.getName().getBytes("iso-8859-1");
String fileName=new String(bytes,"utf-8");
if(count==temp){
inputStream = ftpClient.retrieveFileStream(fileName);
picType = fileName.split("\\.")[1];
BufferedImage bufImg = null;
bufImg = ImageIO.read(inputStream);
outStream = response.getOutputStream();
ImageIO.write(bufImg, picType, outStream);
break;
}
count++;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
if(null != inputStream){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<img src="http://ip:port/xx/getXXX?ftpUrl=xxx&index=x" width="400" height="266" />
</body>
</html>