Commit dd1618c1 by 李辅翼

v1

parent 146a0547
...@@ -3,7 +3,7 @@ package com.hikcreate.ftp.proxy.modules; ...@@ -3,7 +3,7 @@ package com.hikcreate.ftp.proxy.modules;
import cn.hutool.core.codec.Base64Encoder; import cn.hutool.core.codec.Base64Encoder;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.hikcreate.ftp.proxy.entity.PicByte; import com.hikcreate.ftp.proxy.entity.PicByte;
import org.apache.commons.codec.binary.Base64; import com.hikcreate.ftp.proxy.utils.FtpUtil;
import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTP;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -12,13 +12,12 @@ import org.springframework.web.bind.annotation.*; ...@@ -12,13 +12,12 @@ import org.springframework.web.bind.annotation.*;
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClient;
import sun.net.ftp.FtpClient;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.net.InetSocketAddress; import java.net.HttpURLConnection;
import java.net.SocketAddress; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -70,63 +69,165 @@ public class FtpProxyWebApi { ...@@ -70,63 +69,165 @@ public class FtpProxyWebApi {
} }
} }
@PostMapping("/testFtpUtil")
public synchronized PicByte testFtpUtil(@RequestParam("urls") String urls) {
PicByte picByte = new PicByte();
FtpUtil ftpUtil = null;
try {
Map<String, byte[]> map = new HashMap<>();
String[] urlArr = urls.split(",");
for (String str : urlArr) {
paramUrl(str);
ftpUtil = new FtpUtil(host, 21, userName, password);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
boolean flag = ftpUtil.downloadPicFile(path, filename, bos);
if (flag) {
byte[] bytes = bos.toByteArray();
map.put(str, bytes);
} else {
logger.info("获取图片失败");
}
}
picByte.setMap(map);
} catch (Throwable e) {
e.printStackTrace();
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
logger.error("打印错误:" + writer.toString());
} finally {
if (ftpUtil != null) {
ftpUtil = null;
}
}
return picByte;
}
@PostMapping("/testHttpUtil")
public synchronized PicByte testHttpUtil(@RequestParam("urls") String urls) {
String[] urlArr = urls.split(",");
Map<String, byte[]> map = new HashMap<>();
PicByte picByte = new PicByte();
HttpURLConnection httpURLConnection = null;
InputStream inputStream = null;
ByteArrayOutputStream bos =null;
try {
for (String str : urlArr) {
URL url = new URL(str);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
// 从服务器返回一个输入流
inputStream = httpURLConnection.getInputStream();
if(inputStream != null && inputStream.available() > 0){
bos = new ByteArrayOutputStream();
int length;
byte[] buf = new byte[1024*2];
while (-1 != (length = inputStream.read(buf, 0, buf.length))) {
bos.write(buf, 0, length);
}
ByteArrayInputStream fis = new ByteArrayInputStream(bos.toByteArray());
bos.flush();
bos.close();
byte[] buffer = new byte[fis.available()];
int offset = 0;
int numRead = 0;
while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
if (offset != buffer.length) {
throw new IOException("Could not completely read file ");
}
fis.close();
map.put(str, buffer);
}
inputStream.close();
}
picByte.setMap(map);
}
} catch (Throwable e) {
e.printStackTrace();
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
logger.error("打印错误:" + writer.toString());
} finally {
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return picByte;
}
@PostMapping("/downloadByUrls") @PostMapping("/downloadByUrls")
public synchronized PicByte list2(@RequestParam("urls") String urls, HttpServletResponse response){ public synchronized PicByte list2(@RequestParam("urls") String urls, HttpServletResponse response) {
System.out.println("请求来了——————————————————————————————————————"); PicByte picByte = new PicByte();
PicByte picByte=new PicByte(); FTPClient ftpClient = null;
FTPClient ftpClient=null; try {
try{ Map<String, byte[]> map = new HashMap<>();
Map<String,byte[]> map=new HashMap<>();
String[] urlArr = urls.split(","); String[] urlArr = urls.split(",");
for(String str:urlArr){ for (String str : urlArr) {
String[] split =str.split("@"); paramUrl(str);
host=split[1].split(":")[0];
userName=split[0].split(":")[1].substring(2);
password=split[0].split(":")[2];
path=split[1].split(":")[1].substring(2).substring(0,split[1].split(":")[1].substring(2).lastIndexOf("/")+1);
String[] split1 = split[1].split(":")[1].substring(2).split("/");
filename=split1[split1.length-1];
ftpClient = new FTPClient(); ftpClient = new FTPClient();
ftpClient.connect(host, 21); ftpClient.connect(host, 21);
ftpClient.login(userName, password); ftpClient.login(userName, password);
ftpClient.changeWorkingDirectory(path); ftpClient.changeWorkingDirectory(path);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
inputStream = ftpClient.retrieveFileStream(filename); inputStream = ftpClient.retrieveFileStream(filename);
ByteArrayOutputStream bos=new ByteArrayOutputStream(); if (inputStream != null && inputStream.available() > 0) {
int length; ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[2048]; int length;
while (-1 != (length = inputStream.read(buf, 0, buf.length))) { byte[] buf = new byte[2048];
bos.write(buf, 0, length); while (-1 != (length = inputStream.read(buf, 0, buf.length))) {
bos.write(buf, 0, length);
}
ByteArrayInputStream fis = new ByteArrayInputStream(bos.toByteArray());
bos.flush();
bos.close();
byte[] buffer = new byte[fis.available()];
int offset = 0;
int numRead = 0;
while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
if (offset != buffer.length) {
throw new IOException("Could not completely read file ");
}
fis.close();
map.put(str, buffer);
} }
ByteArrayInputStream fis = new ByteArrayInputStream(bos.toByteArray());
bos.flush();
bos.close();
byte[] buffer = new byte[fis.available()];
int offset = 0;
int numRead = 0;
while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}if (offset != buffer.length) {
throw new IOException("Could not completely read file ");
}
fis.close();
map.put(str,buffer);
} }
picByte.setMap(map); picByte.setMap(map);
}catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
Writer writer = new StringWriter(); Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer)); e.printStackTrace(new PrintWriter(writer));
logger.error("打印错误:" + writer.toString()); logger.error("打印错误:" + writer.toString());
}finally { } finally {
if(inputStream!=null){ if (inputStream != null) {
try { try {
inputStream.close(); inputStream.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
if(br!=null){ if (br != null) {
try { try {
br.close(); br.close();
} catch (IOException e) { } catch (IOException e) {
...@@ -148,13 +249,7 @@ public class FtpProxyWebApi { ...@@ -148,13 +249,7 @@ public class FtpProxyWebApi {
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
//解析参数 //解析参数
String param = jsonParam.getString("param"); String param = jsonParam.getString("param");
String[] split =param .split("@"); paramUrl(param);
host=split[1].split(":")[0];
userName=split[0].split(":")[1].substring(2);
password=split[0].split(":")[2];
path=split[1].split(":")[1].substring(2).substring(0,split[1].split(":")[1].substring(2).lastIndexOf("/")+1);
String[] split1 = split[1].split(":")[1].substring(2).split("/");
filename=split1[split1.length-1];
//方式二 //方式二
...@@ -162,12 +257,10 @@ public class FtpProxyWebApi { ...@@ -162,12 +257,10 @@ public class FtpProxyWebApi {
ftpClient.connect(host, 21); ftpClient.connect(host, 21);
ftpClient.login(userName, password); ftpClient.login(userName, password);
ftpClient.changeWorkingDirectory(path); ftpClient.changeWorkingDirectory(path);
System.out.println("文件名称:"+filename);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
inputStream = ftpClient.retrieveFileStream(filename); inputStream = ftpClient.retrieveFileStream(filename);
System.out.println("流长+++++++++"+inputStream.available()); ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int length; int length;
byte[] buf = new byte[2048]; byte[] buf = new byte[2048];
while (-1 != (length = inputStream.read(buf, 0, buf.length))) { while (-1 != (length = inputStream.read(buf, 0, buf.length))) {
...@@ -186,7 +279,7 @@ public class FtpProxyWebApi { ...@@ -186,7 +279,7 @@ public class FtpProxyWebApi {
throw new IOException("Could not completely read file "); throw new IOException("Could not completely read file ");
} }
fis.close(); fis.close();
String asB64 = "data:image/jpg;base64,"+new Base64Encoder().encode(buffer); String asB64 = "data:image/jpg;base64," + new Base64Encoder().encode(buffer);
/* byte[] byt = new byte[inputStream.available()]; /* byte[] byt = new byte[inputStream.available()];
System.out.println("字节长度--------------:"+byt.length); System.out.println("字节长度--------------:"+byt.length);
...@@ -195,8 +288,6 @@ public class FtpProxyWebApi { ...@@ -195,8 +288,6 @@ public class FtpProxyWebApi {
String imgStr = new String(base, "utf-8"); String imgStr = new String(base, "utf-8");
System.out.println("图片:"+imgStr);*/ System.out.println("图片:"+imgStr);*/
System.out.println("图片:"+asB64);
System.out.println("图片地址:"+param);
response.setContentType("application/json;charset=UTF-8"); response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(asB64); response.getWriter().write(asB64);
...@@ -205,15 +296,15 @@ public class FtpProxyWebApi { ...@@ -205,15 +296,15 @@ public class FtpProxyWebApi {
Writer writer = new StringWriter(); Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer)); e.printStackTrace(new PrintWriter(writer));
logger.error("打印错误:" + writer.toString()); logger.error("打印错误:" + writer.toString());
}finally { } finally {
if(inputStream!=null){ if (inputStream != null) {
try { try {
inputStream.close(); inputStream.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
if(br!=null){ if (br != null) {
try { try {
br.close(); br.close();
} catch (IOException e) { } catch (IOException e) {
...@@ -223,10 +314,15 @@ public class FtpProxyWebApi { ...@@ -223,10 +314,15 @@ public class FtpProxyWebApi {
} }
} }
private static void paramUrl(String url) {
String[] split = url.split("@");
host = split[1].split(":")[0];
userName = split[0].split(":")[1].substring(2);
password = split[0].split(":")[2];
path = split[1].split(":")[1].substring(2).substring(0, split[1].split(":")[1].substring(2).lastIndexOf("/") + 1);
String[] split1 = split[1].split(":")[1].substring(2).split("/");
filename = split1[split1.length - 1];
}
} }
package com.hikcreate.ftp.proxy.utils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* FTP工具类,用于获取ftp连接,上传文件,删除文件等对ftp的操作
* @author hulei
*/
public class FtpUtil {
private static Logger logger = Logger.getLogger(FtpUtil.class);
/**
* ftp 根目录
*/
private static final String ROOTPATH = "/";
/**
* ftp 字符集
*/
private static final String FTP_ENCODE = "UTF-8";
/**
* ftp 连接默认超时时间
*/
private static final int DEFAULT_TIMEOUT = 30*1000;
/**
* FTP客户端
*/
FTPClient ftpClient = null;
/**
* 获取ftpUtil实例
* @param ftpIp ftp Ip
* @param port ftp 端口
* @param userName ftp用户名
* @param passWord ftp 用户密码
* @return FTPClient
* @throws IOException
*/
public FtpUtil(String ftpIp, int port, String userName, String passWord) {
try {
ftpClient = new FTPClient();
ftpClient.connect(ftpIp);
ftpClient.setDefaultPort(port);
ftpClient.setDataTimeout(DEFAULT_TIMEOUT);
ftpClient.setControlEncoding(FTP_ENCODE);
boolean isConnect = ftpClient.login(userName,passWord);
if(!isConnect) {
logger.error("Connection the FtpServer Is Failed!");
ftpClient.disconnect();
}
} catch (IOException e) {
logger.error("Connection the FtpServer Is Failed! " + e.toString());
}
}
/**
* ftp文件上传
* @param ins 文件输入流
* @param uploadPath 上传路径
* @param fileName 文件名
* @return boolean true/false
*/
public boolean uploadFile(InputStream ins, String uploadPath, String fileName) {
boolean flag = false;
if(ftpClient != null) {
try {
//设置上传文件类型,默认为文本,图片需要设置成二进制,否则图片失真
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//切换到上传目录
boolean change = ftpClient.changeWorkingDirectory(uploadPath);
if(change) {
flag = ftpClient.storeFile(new String(fileName.getBytes("GBK"),"iso-8859-1"), ins);
} else {
ftpClient.makeDirectory(uploadPath);
ftpClient.changeWorkingDirectory(uploadPath);
flag = ftpClient.storeFile(fileName, ins);
}
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
/**
* 切换回上级菜单
*/
public void changeToParentDirectory() {
try {
ftpClient.changeToParentDirectory();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭ftp连接
*/
public void closeFtp() {
try {
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 下载文件
* @param remotePath
* @param fileName
* @param os
* @return
*/
public boolean downloadPicFile(String remotePath,String fileName,OutputStream os) {
boolean flag = false;
try {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
flag = ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(),"ISO-8859-1"));
if (flag) {
ftpClient.retrieveFile(fileName, os);
os.flush();
os.close();
flag = true;
} else {
flag = false;
}
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return flag;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment