Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
ftp_proxy
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李辅翼
ftp_proxy
Commits
dd1618c1
Commit
dd1618c1
authored
5 years ago
by
李辅翼
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
v1
parent
146a0547
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
320 additions
and
60 deletions
+320
-60
src/main/java/com/hikcreate/ftp/proxy/modules/FtpProxyWebApi.java
+156
-60
src/main/java/com/hikcreate/ftp/proxy/utils/FtpUtil.java
+164
-0
No files found.
src/main/java/com/hikcreate/ftp/proxy/modules/FtpProxyWebApi.java
View file @
dd1618c1
...
...
@@ -3,7 +3,7 @@ package com.hikcreate.ftp.proxy.modules;
import
cn.hutool.core.codec.Base64Encoder
;
import
com.alibaba.fastjson.JSONObject
;
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.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
...
...
@@ -12,13 +12,12 @@ import org.springframework.web.bind.annotation.*;
import
org.apache.commons.net.ftp.FTPClient
;
import
sun.net.ftp.FtpClient
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.*
;
import
java.net.
InetSocketAddress
;
import
java.net.
SocketAddress
;
import
java.net.
HttpURLConnection
;
import
java.net.
URL
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
...
...
@@ -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"
)
public
synchronized
PicByte
list2
(
@RequestParam
(
"urls"
)
String
urls
,
HttpServletResponse
response
){
System
.
out
.
println
(
"请求来了——————————————————————————————————————"
);
PicByte
picByte
=
new
PicByte
();
FTPClient
ftpClient
=
null
;
try
{
Map
<
String
,
byte
[]>
map
=
new
HashMap
<>();
public
synchronized
PicByte
list2
(
@RequestParam
(
"urls"
)
String
urls
,
HttpServletResponse
response
)
{
PicByte
picByte
=
new
PicByte
();
FTPClient
ftpClient
=
null
;
try
{
Map
<
String
,
byte
[]>
map
=
new
HashMap
<>();
String
[]
urlArr
=
urls
.
split
(
","
);
for
(
String
str:
urlArr
){
String
[]
split
=
str
.
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
];
for
(
String
str
:
urlArr
)
{
paramUrl
(
str
);
ftpClient
=
new
FTPClient
();
ftpClient
.
connect
(
host
,
21
);
ftpClient
.
login
(
userName
,
password
);
ftpClient
.
changeWorkingDirectory
(
path
);
ftpClient
.
setFileType
(
FTP
.
BINARY_FILE_TYPE
);
inputStream
=
ftpClient
.
retrieveFileStream
(
filename
);
ByteArrayOutputStream
bos
=
new
ByteArrayOutputStream
();
int
length
;
byte
[]
buf
=
new
byte
[
2048
];
while
(-
1
!=
(
length
=
inputStream
.
read
(
buf
,
0
,
buf
.
length
)))
{
bos
.
write
(
buf
,
0
,
length
);
if
(
inputStream
!=
null
&&
inputStream
.
available
()
>
0
)
{
ByteArrayOutputStream
bos
=
new
ByteArrayOutputStream
();
int
length
;
byte
[]
buf
=
new
byte
[
2048
];
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
);
}
catch
(
Throwable
e
)
{
}
catch
(
Throwable
e
)
{
e
.
printStackTrace
();
Writer
writer
=
new
StringWriter
();
e
.
printStackTrace
(
new
PrintWriter
(
writer
));
logger
.
error
(
"打印错误:"
+
writer
.
toString
());
}
finally
{
if
(
inputStream
!=
null
)
{
}
finally
{
if
(
inputStream
!=
null
)
{
try
{
inputStream
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
if
(
br
!=
null
)
{
if
(
br
!=
null
)
{
try
{
br
.
close
();
}
catch
(
IOException
e
)
{
...
...
@@ -148,13 +249,7 @@ public class FtpProxyWebApi {
List
<
String
>
list
=
new
ArrayList
<
String
>();
//解析参数
String
param
=
jsonParam
.
getString
(
"param"
);
String
[]
split
=
param
.
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
];
paramUrl
(
param
);
//方式二
...
...
@@ -162,12 +257,10 @@ public class FtpProxyWebApi {
ftpClient
.
connect
(
host
,
21
);
ftpClient
.
login
(
userName
,
password
);
ftpClient
.
changeWorkingDirectory
(
path
);
System
.
out
.
println
(
"文件名称:"
+
filename
);
ftpClient
.
setFileType
(
FTP
.
BINARY_FILE_TYPE
);
inputStream
=
ftpClient
.
retrieveFileStream
(
filename
);
System
.
out
.
println
(
"流长+++++++++"
+
inputStream
.
available
());
ByteArrayOutputStream
bos
=
new
ByteArrayOutputStream
();
ByteArrayOutputStream
bos
=
new
ByteArrayOutputStream
();
int
length
;
byte
[]
buf
=
new
byte
[
2048
];
while
(-
1
!=
(
length
=
inputStream
.
read
(
buf
,
0
,
buf
.
length
)))
{
...
...
@@ -186,7 +279,7 @@ public class FtpProxyWebApi {
throw
new
IOException
(
"Could not completely read file "
);
}
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()];
System.out.println("字节长度--------------:"+byt.length);
...
...
@@ -195,8 +288,6 @@ public class FtpProxyWebApi {
String imgStr = new String(base, "utf-8");
System.out.println("图片:"+imgStr);*/
System
.
out
.
println
(
"图片:"
+
asB64
);
System
.
out
.
println
(
"图片地址:"
+
param
);
response
.
setContentType
(
"application/json;charset=UTF-8"
);
response
.
getWriter
().
write
(
asB64
);
...
...
@@ -205,15 +296,15 @@ public class FtpProxyWebApi {
Writer
writer
=
new
StringWriter
();
e
.
printStackTrace
(
new
PrintWriter
(
writer
));
logger
.
error
(
"打印错误:"
+
writer
.
toString
());
}
finally
{
if
(
inputStream
!=
null
)
{
}
finally
{
if
(
inputStream
!=
null
)
{
try
{
inputStream
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
if
(
br
!=
null
)
{
if
(
br
!=
null
)
{
try
{
br
.
close
();
}
catch
(
IOException
e
)
{
...
...
@@ -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
];
}
}
This diff is collapsed.
Click to expand it.
src/main/java/com/hikcreate/ftp/proxy/utils/FtpUtil.java
0 → 100644
View file @
dd1618c1
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
;
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment