Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
chnmuseum-party
Project
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liqin
chnmuseum-party
Commits
42dafa18
Commit
42dafa18
authored
Mar 17, 2021
by
liqin
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug fixed
parent
03e3d5ee
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
276 additions
and
279 deletions
+276
-279
FastDFSUtils.java
...cn/wisenergy/chnmuseum/party/common/dfs/FastDFSUtils.java
+137
-137
FastdfsUtil.java
.../cn/wisenergy/chnmuseum/party/common/dfs/FastdfsUtil.java
+139
-142
No files found.
src/main/java/cn/wisenergy/chnmuseum/party/common/dfs/FastDFSUtils.java
View file @
42dafa18
...
...
@@ -4,152 +4,152 @@ import org.apache.commons.io.FilenameUtils;
import
org.csource.common.MyException
;
import
org.csource.common.NameValuePair
;
import
org.csource.fastdfs.*
;
import
org.springframework.core.io.ClassPathResource
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.util.Properties
;
/**
* 上传图片到FastDFS
*/
public
class
FastDFSUtils
{
static
{
try
{
ClientGlobal
.
initByProperties
(
FastDFSUtils
.
class
.
getClassLoader
().
getResource
(
"fastdfs-client.properties"
).
getPath
());
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
MyException
e
)
{
e
.
printStackTrace
();
}
}
public
static
String
[]
uploadPic
(
String
path
,
String
fileName
,
long
size
)
{
String
[]
fileIds
=
null
;
try
{
// ClientGloble 读配置文件
// 老大客户端
TrackerClient
trackerClient
=
new
TrackerClient
();
TrackerServer
trackerServer
=
trackerClient
.
getConnection
();
StorageServer
storageServer
=
null
;
StorageClient
storageClient
=
new
StorageClient
(
trackerServer
,
storageServer
);
String
extName
=
FilenameUtils
.
getExtension
(
fileName
);
NameValuePair
[]
meta_list
=
new
NameValuePair
[
3
];
meta_list
[
0
]
=
new
NameValuePair
(
"fileName"
,
fileName
);
meta_list
[
1
]
=
new
NameValuePair
(
"fileExt"
,
extName
);
meta_list
[
2
]
=
new
NameValuePair
(
"fileSize"
,
String
.
valueOf
(
size
));
// http://172.16.15.244:8081/group1/M00/00/00/rBAP9FfFG62AZsuBAADeW7MfEHA287.png
// group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
fileIds
=
storageClient
.
upload_file
(
path
,
extName
,
meta_list
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
fileIds
;
}
public
static
String
[]
uploadPic
(
InputStream
inStream
,
String
fileName
,
long
size
)
{
String
[]
fileIds
=
null
;
try
{
// ClientGloble 读配置文件
// 老大客户端
TrackerClient
trackerClient
=
new
TrackerClient
();
TrackerServer
trackerServer
=
trackerClient
.
getConnection
();
StorageServer
storageServer
=
null
;
StorageClient
storageClient
=
new
StorageClient
(
trackerServer
,
storageServer
);
String
extName
=
FilenameUtils
.
getExtension
(
fileName
);
NameValuePair
[]
meta_list
=
new
NameValuePair
[
3
];
meta_list
[
0
]
=
new
NameValuePair
(
"fileName"
,
fileName
);
meta_list
[
1
]
=
new
NameValuePair
(
"fileExt"
,
extName
);
meta_list
[
2
]
=
new
NameValuePair
(
"fileSize"
,
String
.
valueOf
(
size
));
// http://172.16.15.244:8081/group1/M00/00/00/rBAP9FfFG62AZsuBAADeW7MfEHA287.png
// group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
fileIds
=
storageClient
.
upload_file
(
null
,
size
,
new
UploadFileSender
(
inStream
),
extName
,
meta_list
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
fileIds
;
}
public
static
String
uploadPic
(
byte
[]
pic
,
String
fileName
,
long
size
)
{
String
[]
fileIds
=
null
;
try
{
// ClientGloble 读配置文件
// 老客户端
TrackerClient
trackerClient
=
new
TrackerClient
();
TrackerServer
trackerServer
=
trackerClient
.
getConnection
();
StorageServer
storageServer
=
null
;
StorageClient
storageClient
=
new
StorageClient
(
trackerServer
,
storageServer
);
String
extName
=
FilenameUtils
.
getExtension
(
fileName
);
// 设置图片meta信息
NameValuePair
[]
meta_list
=
new
NameValuePair
[
3
];
meta_list
[
0
]
=
new
NameValuePair
(
"fileName"
,
fileName
);
meta_list
[
1
]
=
new
NameValuePair
(
"fileExt"
,
extName
);
meta_list
[
2
]
=
new
NameValuePair
(
"fileSize"
,
String
.
valueOf
(
size
));
// 上传且返回path
fileIds
=
storageClient
.
upload_file
(
pic
,
extName
,
meta_list
);
return
fileIds
[
0
]
+
"/"
+
fileIds
[
1
];
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
public
static
boolean
deletePic
(
String
fileUrl
)
{
try
{
TrackerClient
tracker
=
new
TrackerClient
();
TrackerServer
trackerServer
=
tracker
.
getConnection
();
StorageServer
storageServer
=
null
;
StorageClient
storageClient
=
new
StorageClient
(
trackerServer
,
storageServer
);
StorePath
storePath
=
StorePath
.
praseFromUrl
(
fileUrl
);
int
i
=
storageClient
.
delete_file
(
storePath
.
getGroup
(),
storePath
.
getPath
());
System
.
out
.
println
(
i
==
0
?
"删除成功"
:
"删除失败:"
+
i
);
return
i
==
0
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
false
;
}
public
static
String
deletePic
(
String
[]
fileIds
)
{
try
{
TrackerClient
tracker
=
new
TrackerClient
();
TrackerServer
trackerServer
=
tracker
.
getConnection
();
StorageServer
storageServer
=
null
;
StorageClient
storageClient
=
new
StorageClient
(
trackerServer
,
storageServer
);
int
i
=
storageClient
.
delete_file
(
fileIds
[
0
],
fileIds
[
1
]);
System
.
out
.
println
(
i
==
0
?
"删除成功"
:
"删除失败:"
+
i
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
fileIds
[
1
];
}
private
static
class
UploadFileSender
implements
UploadCallback
{
private
InputStream
inStream
;
public
UploadFileSender
(
InputStream
inStream
)
{
this
.
inStream
=
inStream
;
}
public
int
send
(
OutputStream
out
)
throws
IOException
{
int
readBytes
;
while
((
readBytes
=
inStream
.
read
())
>
0
)
{
out
.
write
(
readBytes
);
}
return
0
;
}
}
static
{
try
{
Properties
props
=
new
Properties
();
final
InputStream
in
=
new
ClassPathResource
(
"fastdfs-client.properties"
).
getInputStream
();
props
.
load
(
in
);
ClientGlobal
.
initByProperties
(
props
);
}
catch
(
IOException
|
MyException
e
)
{
e
.
printStackTrace
();
}
}
public
static
String
[]
uploadPic
(
String
path
,
String
fileName
,
long
size
)
{
String
[]
fileIds
=
null
;
try
{
// ClientGloble 读配置文件
// 老大客户端
TrackerClient
trackerClient
=
new
TrackerClient
();
TrackerServer
trackerServer
=
trackerClient
.
getConnection
();
StorageServer
storageServer
=
null
;
StorageClient
storageClient
=
new
StorageClient
(
trackerServer
,
storageServer
);
String
extName
=
FilenameUtils
.
getExtension
(
fileName
);
NameValuePair
[]
meta_list
=
new
NameValuePair
[
3
];
meta_list
[
0
]
=
new
NameValuePair
(
"fileName"
,
fileName
);
meta_list
[
1
]
=
new
NameValuePair
(
"fileExt"
,
extName
);
meta_list
[
2
]
=
new
NameValuePair
(
"fileSize"
,
String
.
valueOf
(
size
));
// http://172.16.15.244:8081/group1/M00/00/00/rBAP9FfFG62AZsuBAADeW7MfEHA287.png
// group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
fileIds
=
storageClient
.
upload_file
(
path
,
extName
,
meta_list
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
fileIds
;
}
public
static
String
[]
uploadPic
(
InputStream
inStream
,
String
fileName
,
long
size
)
{
String
[]
fileIds
=
null
;
try
{
// ClientGloble 读配置文件
// 老大客户端
TrackerClient
trackerClient
=
new
TrackerClient
();
TrackerServer
trackerServer
=
trackerClient
.
getConnection
();
StorageServer
storageServer
=
null
;
StorageClient
storageClient
=
new
StorageClient
(
trackerServer
,
storageServer
);
String
extName
=
FilenameUtils
.
getExtension
(
fileName
);
NameValuePair
[]
meta_list
=
new
NameValuePair
[
3
];
meta_list
[
0
]
=
new
NameValuePair
(
"fileName"
,
fileName
);
meta_list
[
1
]
=
new
NameValuePair
(
"fileExt"
,
extName
);
meta_list
[
2
]
=
new
NameValuePair
(
"fileSize"
,
String
.
valueOf
(
size
));
// http://172.16.15.244:8081/group1/M00/00/00/rBAP9FfFG62AZsuBAADeW7MfEHA287.png
// group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
fileIds
=
storageClient
.
upload_file
(
null
,
size
,
new
UploadFileSender
(
inStream
),
extName
,
meta_list
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
fileIds
;
}
public
static
String
uploadPic
(
byte
[]
pic
,
String
fileName
,
long
size
)
{
String
[]
fileIds
=
null
;
try
{
// ClientGloble 读配置文件
// 老客户端
TrackerClient
trackerClient
=
new
TrackerClient
();
TrackerServer
trackerServer
=
trackerClient
.
getConnection
();
StorageServer
storageServer
=
null
;
StorageClient
storageClient
=
new
StorageClient
(
trackerServer
,
storageServer
);
String
extName
=
FilenameUtils
.
getExtension
(
fileName
);
// 设置图片meta信息
NameValuePair
[]
meta_list
=
new
NameValuePair
[
3
];
meta_list
[
0
]
=
new
NameValuePair
(
"fileName"
,
fileName
);
meta_list
[
1
]
=
new
NameValuePair
(
"fileExt"
,
extName
);
meta_list
[
2
]
=
new
NameValuePair
(
"fileSize"
,
String
.
valueOf
(
size
));
// 上传且返回path
fileIds
=
storageClient
.
upload_file
(
pic
,
extName
,
meta_list
);
return
fileIds
[
0
]
+
"/"
+
fileIds
[
1
];
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
public
static
boolean
deletePic
(
String
fileUrl
)
{
try
{
TrackerClient
tracker
=
new
TrackerClient
();
TrackerServer
trackerServer
=
tracker
.
getConnection
();
StorageServer
storageServer
=
null
;
StorageClient
storageClient
=
new
StorageClient
(
trackerServer
,
storageServer
);
StorePath
storePath
=
StorePath
.
praseFromUrl
(
fileUrl
);
int
i
=
storageClient
.
delete_file
(
storePath
.
getGroup
(),
storePath
.
getPath
());
System
.
out
.
println
(
i
==
0
?
"删除成功"
:
"删除失败:"
+
i
);
return
i
==
0
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
false
;
}
public
static
String
deletePic
(
String
[]
fileIds
)
{
try
{
TrackerClient
tracker
=
new
TrackerClient
();
TrackerServer
trackerServer
=
tracker
.
getConnection
();
StorageServer
storageServer
=
null
;
StorageClient
storageClient
=
new
StorageClient
(
trackerServer
,
storageServer
);
int
i
=
storageClient
.
delete_file
(
fileIds
[
0
],
fileIds
[
1
]);
System
.
out
.
println
(
i
==
0
?
"删除成功"
:
"删除失败:"
+
i
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
fileIds
[
1
];
}
private
static
class
UploadFileSender
implements
UploadCallback
{
private
InputStream
inStream
;
public
UploadFileSender
(
InputStream
inStream
)
{
this
.
inStream
=
inStream
;
}
public
int
send
(
OutputStream
out
)
throws
IOException
{
int
readBytes
;
while
((
readBytes
=
inStream
.
read
())
>
0
)
{
out
.
write
(
readBytes
);
}
return
0
;
}
}
}
\ No newline at end of file
src/main/java/cn/wisenergy/chnmuseum/party/common/dfs/FastdfsUtil.java
View file @
42dafa18
package
cn
.
wisenergy
.
chnmuseum
.
party
.
common
.
dfs
;
import
org.apache.commons.io.FilenameUtils
;
import
org.csource.common.NameValuePair
;
import
org.csource.fastdfs.ClientGlobal
;
import
org.csource.fastdfs.StorageClient
;
import
org.csource.fastdfs.TrackerClient
;
import
org.csource.fastdfs.TrackerServer
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.core.io.ClassPathResource
;
import
javax.imageio.ImageIO
;
import
java.awt.image.BufferedImage
;
import
java.io.ByteArrayInputStream
;
import
java.io.File
;
import
java.io.InputStream
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Date
;
import
javax.imageio.ImageIO
;
import
org.apache.commons.io.FilenameUtils
;
import
org.csource.common.NameValuePair
;
import
org.csource.fastdfs.*
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.Properties
;
/**
* ueditor
*/
public
class
FastdfsUtil
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
FastdfsUtil
.
class
);
private
static
boolean
isInit
=
false
;
private
static
void
init
()
throws
Exception
{
String
classPath
=
new
File
(
FastdfsUtil
.
class
.
getResource
(
"/"
).
getFile
()).
getCanonicalPath
();
String
configFilePath
=
classPath
+
File
.
separator
+
"fastdfs-client.properties"
;
logger
.
info
(
"FastdfsUtils.init加载配置文件:"
+
configFilePath
);
ClientGlobal
.
initByProperties
(
configFilePath
);
}
private
static
TrackerClient
getTrackerClient
()
throws
Exception
{
if
(!
isInit
)
{
init
();
isInit
=
true
;
}
TrackerClient
trackerClient
=
new
TrackerClient
();
return
trackerClient
;
}
private
static
TrackerServer
getTrackerServer
()
throws
Exception
{
return
getTrackerClient
().
getConnection
();
}
private
static
StorageClient
getStorageClient
()
throws
Exception
{
TrackerServer
trackerServer
=
getTrackerServer
();
// StorageServer storageServer = null;
// StorageClient storageClient = new StorageClient(trackerServer,
// storageServer);
StorageClient
storageClient
=
new
StorageClient
(
trackerServer
,
null
);
return
storageClient
;
}
/**
* 文件方式上传
*/
public
static
Map
<
String
,
Object
>
uploadFile
(
File
file
)
{
String
fileName
=
file
.
getName
();
byte
[]
fileBuff
=
FileUtil
.
getBytes
(
file
);
return
uploadFile
(
fileBuff
,
fileName
);
}
/**
* 是否是图片
*/
private
static
boolean
isImage
(
String
fileName
)
{
return
FileTypeUtil
.
isImageByExtension
(
fileName
);
}
/**
* 字节流方式上传
*/
public
static
Map
<
String
,
Object
>
uploadFile
(
byte
[]
fileBuff
,
String
fileName
)
{
String
originalFileName
=
FilenameUtils
.
getName
(
fileName
);
// 文件名
String
fileExtName
=
FilenameUtils
.
getExtension
(
originalFileName
);
// 文件后缀名
long
length
=
fileBuff
.
length
;
// 字节
boolean
isImage
=
isImage
(
originalFileName
);
String
mimeType
=
FileUtil
.
getMimeType
(
fileName
);
int
width
=
0
;
int
height
=
0
;
if
(
isImage
)
{
int
[]
imageInfo
=
getImageInfo
(
fileBuff
);
if
(
imageInfo
!=
null
)
{
width
=
imageInfo
[
0
];
height
=
imageInfo
[
1
];
}
}
NameValuePair
[]
metaList
=
new
NameValuePair
[]
{
new
NameValuePair
(
"fileName"
,
fileName
),
new
NameValuePair
(
"isImage"
,
isImage
+
""
),
new
NameValuePair
(
"mimeType"
,
mimeType
),
new
NameValuePair
(
"width"
,
width
+
""
),
new
NameValuePair
(
"height"
,
height
+
""
),
new
NameValuePair
(
"author"
,
"FastdfsUtils"
)
};
boolean
status
=
false
;
String
message
=
"文件上传失败!"
;
logger
.
info
(
"startTIme:"
+
(
new
Date
().
getMinutes
()));
String
[]
responseData
=
storeFile
(
fileBuff
,
fileExtName
,
metaList
);
logger
.
info
(
"startTIme:"
+
(
new
Date
().
getMinutes
()));
Map
<
String
,
Object
>
uploadResult
=
new
HashMap
<
String
,
Object
>();
if
(
responseData
!=
null
)
{
status
=
true
;
message
=
"文件上传成功!"
;
uploadResult
.
put
(
"isImage"
,
isImage
);
if
(
isImage
)
{
uploadResult
.
put
(
"width"
,
width
);
uploadResult
.
put
(
"height"
,
height
);
}
uploadResult
.
put
(
"groupName"
,
responseData
[
0
]);
uploadResult
.
put
(
"storageFileName"
,
responseData
[
1
]);
uploadResult
.
put
(
"link"
,
responseData
[
0
]
+
"/"
+
responseData
[
1
]);
// 文件访问链接
}
uploadResult
.
put
(
"status"
,
status
);
uploadResult
.
put
(
"message"
,
message
);
uploadResult
.
put
(
"fileName"
,
fileName
);
uploadResult
.
put
(
"mimeType"
,
mimeType
);
uploadResult
.
put
(
"length"
,
length
);
return
uploadResult
;
}
private
static
int
[]
getImageInfo
(
byte
[]
fileBuff
)
{
try
{
// File转为BufferedImage
// BufferedImage buff = ImageIO.read(new
// FileImageInputStream(file));
// BufferedImage buff = ImageIO.read(file);
// byte[]转为BufferedImage
ByteArrayInputStream
in
=
new
ByteArrayInputStream
(
fileBuff
);
// 将byte[]作为输入流;
BufferedImage
image
=
ImageIO
.
read
(
in
);
// 将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream();
int
width
=
image
.
getWidth
();
int
height
=
image
.
getHeight
();
return
new
int
[]
{
width
,
height
};
}
catch
(
Exception
e
)
{
logger
.
error
(
"FastdfsUtils.getImageInfo时发生异常:"
,
e
);
}
return
new
int
[]
{
0
,
0
};
}
private
static
String
[]
storeFile
(
byte
[]
fileBuff
,
String
fileExtName
,
NameValuePair
[]
metaList
)
{
String
[]
responseData
=
null
;
try
{
StorageClient
storageClient
=
getStorageClient
();
responseData
=
storageClient
.
upload_file
(
fileBuff
,
fileExtName
.
toLowerCase
(),
metaList
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"FastdfsUtils.storeFile时发生异常:"
,
e
);
}
return
responseData
;
}
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
FastdfsUtil
.
class
);
private
static
boolean
isInit
=
false
;
private
static
void
init
()
throws
Exception
{
Properties
props
=
new
Properties
();
final
InputStream
in
=
new
ClassPathResource
(
"fastdfs-client.properties"
).
getInputStream
();
props
.
load
(
in
);
ClientGlobal
.
initByProperties
(
props
);
}
private
static
TrackerClient
getTrackerClient
()
throws
Exception
{
if
(!
isInit
)
{
init
();
isInit
=
true
;
}
return
new
TrackerClient
();
}
private
static
TrackerServer
getTrackerServer
()
throws
Exception
{
return
getTrackerClient
().
getConnection
();
}
private
static
StorageClient
getStorageClient
()
throws
Exception
{
TrackerServer
trackerServer
=
getTrackerServer
();
return
new
StorageClient
(
trackerServer
,
null
);
}
/**
* 文件方式上传
*/
public
static
Map
<
String
,
Object
>
uploadFile
(
File
file
)
{
String
fileName
=
file
.
getName
();
byte
[]
fileBuff
=
FileUtil
.
getBytes
(
file
);
return
uploadFile
(
fileBuff
,
fileName
);
}
/**
* 是否是图片
*/
private
static
boolean
isImage
(
String
fileName
)
{
return
FileTypeUtil
.
isImageByExtension
(
fileName
);
}
/**
* 字节流方式上传
*/
public
static
Map
<
String
,
Object
>
uploadFile
(
byte
[]
fileBuff
,
String
fileName
)
{
String
originalFileName
=
FilenameUtils
.
getName
(
fileName
);
// 文件名
String
fileExtName
=
FilenameUtils
.
getExtension
(
originalFileName
);
// 文件后缀名
long
length
=
fileBuff
.
length
;
// 字节
boolean
isImage
=
isImage
(
originalFileName
);
String
mimeType
=
FileUtil
.
getMimeType
(
fileName
);
int
width
=
0
;
int
height
=
0
;
if
(
isImage
)
{
int
[]
imageInfo
=
getImageInfo
(
fileBuff
);
width
=
imageInfo
[
0
];
height
=
imageInfo
[
1
];
}
NameValuePair
[]
metaList
=
new
NameValuePair
[]{
new
NameValuePair
(
"fileName"
,
fileName
),
new
NameValuePair
(
"isImage"
,
isImage
+
""
),
new
NameValuePair
(
"mimeType"
,
mimeType
),
new
NameValuePair
(
"width"
,
width
+
""
),
new
NameValuePair
(
"height"
,
height
+
""
),
new
NameValuePair
(
"author"
,
"FastdfsUtils"
)};
boolean
status
=
false
;
String
message
=
"文件上传失败!"
;
logger
.
info
(
"startTIme:"
+
(
new
Date
().
getMinutes
()));
String
[]
responseData
=
storeFile
(
fileBuff
,
fileExtName
,
metaList
);
logger
.
info
(
"startTIme:"
+
(
new
Date
().
getMinutes
()));
Map
<
String
,
Object
>
uploadResult
=
new
HashMap
<
String
,
Object
>();
if
(
responseData
!=
null
)
{
status
=
true
;
message
=
"文件上传成功!"
;
uploadResult
.
put
(
"isImage"
,
isImage
);
if
(
isImage
)
{
uploadResult
.
put
(
"width"
,
width
);
uploadResult
.
put
(
"height"
,
height
);
}
uploadResult
.
put
(
"groupName"
,
responseData
[
0
]);
uploadResult
.
put
(
"storageFileName"
,
responseData
[
1
]);
uploadResult
.
put
(
"link"
,
responseData
[
0
]
+
"/"
+
responseData
[
1
]);
// 文件访问链接
}
uploadResult
.
put
(
"status"
,
status
);
uploadResult
.
put
(
"message"
,
message
);
uploadResult
.
put
(
"fileName"
,
fileName
);
uploadResult
.
put
(
"mimeType"
,
mimeType
);
uploadResult
.
put
(
"length"
,
length
);
return
uploadResult
;
}
private
static
int
[]
getImageInfo
(
byte
[]
fileBuff
)
{
try
{
// File转为BufferedImage
// BufferedImage buff = ImageIO.read(new
// FileImageInputStream(file));
// BufferedImage buff = ImageIO.read(file);
// byte[]转为BufferedImage
ByteArrayInputStream
in
=
new
ByteArrayInputStream
(
fileBuff
);
// 将byte[]作为输入流;
BufferedImage
image
=
ImageIO
.
read
(
in
);
// 将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream();
int
width
=
image
.
getWidth
();
int
height
=
image
.
getHeight
();
return
new
int
[]{
width
,
height
};
}
catch
(
Exception
e
)
{
logger
.
error
(
"FastdfsUtils.getImageInfo时发生异常:"
,
e
);
}
return
new
int
[]{
0
,
0
};
}
private
static
String
[]
storeFile
(
byte
[]
fileBuff
,
String
fileExtName
,
NameValuePair
[]
metaList
)
{
String
[]
responseData
=
null
;
try
{
StorageClient
storageClient
=
getStorageClient
();
responseData
=
storageClient
.
upload_file
(
fileBuff
,
fileExtName
.
toLowerCase
(),
metaList
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"FastdfsUtils.storeFile时发生异常:"
,
e
);
}
return
responseData
;
}
}
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