Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gic-webapp-plug
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
base_platform_enterprise
gic-webapp-plug
Commits
fe424a58
Commit
fe424a58
authored
May 29, 2020
by
guojuxing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
上传文件
parent
4e389b89
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
127 additions
and
4 deletions
+127
-4
UploadImgController.java
...java/com/gic/plug/web/controller/UploadImgController.java
+127
-4
No files found.
src/main/java/com/gic/plug/web/controller/UploadImgController.java
View file @
fe424a58
...
@@ -3,22 +3,25 @@ package com.gic.plug.web.controller;
...
@@ -3,22 +3,25 @@ package com.gic.plug.web.controller;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSON
;
import
com.gic.commons.webapi.reponse.RestResponse
;
import
com.gic.commons.webapi.reponse.RestResponse
;
import
com.gic.enterprise.ano.IgnoreLogin
;
import
com.gic.enterprise.ano.IgnoreLogin
;
import
com.gic.thirdparty.BucketNameEnum
;
import
com.gic.thirdparty.FileOperateUtils
;
import
com.gic.thirdparty.pic.QQCloudPicUtils
;
import
com.gic.thirdparty.pic.QQCloudPicUtils
;
import
com.gic.thirdparty.pic.UploadResult
;
import
com.gic.thirdparty.pic.UploadResult
;
import
org.apache.log4j.LogManager
;
import
org.apache.log
ging.log
4j.LogManager
;
import
org.apache.log4j.Logger
;
import
org.apache.log
ging.log
4j.Logger
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartHttpServletRequest
;
import
org.springframework.web.multipart.MultipartHttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.IOException
;
import
java.io.*
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
import
java.util.*
;
@RestController
@RestController
public
class
UploadImgController
{
public
class
UploadImgController
{
private
Logger
logger
=
LogManager
.
getLogger
(
UploadImgController
.
class
);
private
static
Logger
logger
=
LogManager
.
getLogger
(
UploadImgController
.
class
);
@RequestMapping
(
"upload-images"
)
@RequestMapping
(
"upload-images"
)
@IgnoreLogin
@IgnoreLogin
...
@@ -43,4 +46,124 @@ public class UploadImgController {
...
@@ -43,4 +46,124 @@ public class UploadImgController {
}
}
return
RestResponse
.
success
(
picList
);
return
RestResponse
.
success
(
picList
);
}
}
@RequestMapping
(
"upload-file"
)
@IgnoreLogin
public
RestResponse
uploadFile
(
HttpServletRequest
request
)
throws
IOException
{
MultipartHttpServletRequest
multiRequest
=
(
MultipartHttpServletRequest
)
request
;
Iterator
<
String
>
iter
=
multiRequest
.
getFileNames
();
java
.
util
.
List
<
Map
<
String
,
Object
>>
picList
=
new
ArrayList
<
Map
<
String
,
Object
>>();
while
(
iter
.
hasNext
())
{
String
fileName
=
iter
.
next
();
List
<
MultipartFile
>
list
=
multiRequest
.
getMultiFileMap
().
get
(
fileName
);
for
(
int
i
=
0
;
i
<
list
.
size
();
i
++)
{
MultipartFile
mf
=
list
.
get
(
i
);
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"YYYYMMddHHmmss"
);
//获得原始文件名;
String
fileRealName
=
mf
.
getOriginalFilename
();
//点号的位置
int
pointIndex
=
fileRealName
.
lastIndexOf
(
"."
);
//截取文件后缀
String
fileSuffix
=
fileRealName
.
substring
(
pointIndex
);
logger
.
info
(
"上传的文件{},后缀是:{}"
,
fileRealName
,
fileSuffix
);
String
fieldCode
=
"/"
+
fileName
+
"_"
+
sdf
.
format
(
new
Date
())
+
fileSuffix
;
File
targetFile
=
null
;
try
{
targetFile
=
multipartFileToFile
(
mf
);
//默认是其他
String
bucketName
=
BucketNameEnum
.
OTHER_90000
.
getName
();
if
(
isPicture
(
fileSuffix
))
{
bucketName
=
BucketNameEnum
.
IMAGE_10000
.
getName
();
}
else
if
(
isOffice
(
fileSuffix
))
{
bucketName
=
BucketNameEnum
.
REPORT_50000
.
getName
();
}
else
if
(
isMusic
(
fileSuffix
))
{
bucketName
=
BucketNameEnum
.
MUSIC_20000
.
getName
();
}
String
url
=
FileOperateUtils
.
simpleUploadFileFromLocal
(
targetFile
,
fieldCode
,
bucketName
);
Map
<
String
,
Object
>
pic
=
new
HashMap
<>();
pic
.
put
(
"fileId"
,
fieldCode
);
pic
.
put
(
"url"
,
url
);
pic
.
put
(
"fileName"
,
fileRealName
);
picList
.
add
(
pic
);
}
catch
(
Exception
e
)
{
logger
.
warn
(
"上传文件{}错误:{}"
,
fileRealName
,
e
.
getMessage
(),
e
);
}
finally
{
delteTempFile
(
targetFile
);
}
}
}
return
RestResponse
.
success
(
picList
);
}
public
static
File
multipartFileToFile
(
MultipartFile
file
)
throws
Exception
{
File
toFile
=
null
;
if
(
file
.
equals
(
""
)
||
file
.
getSize
()
<=
0
)
{
file
=
null
;
}
else
{
InputStream
ins
=
null
;
ins
=
file
.
getInputStream
();
toFile
=
new
File
(
file
.
getOriginalFilename
());
inputStreamToFile
(
ins
,
toFile
);
ins
.
close
();
}
return
toFile
;
}
/**
* 获取流文件
* @param ins
* @param file
*/
private
static
void
inputStreamToFile
(
InputStream
ins
,
File
file
)
{
try
{
OutputStream
os
=
new
FileOutputStream
(
file
);
int
bytesRead
=
0
;
byte
[]
buffer
=
new
byte
[
8192
];
while
((
bytesRead
=
ins
.
read
(
buffer
,
0
,
8192
))
!=
-
1
)
{
os
.
write
(
buffer
,
0
,
bytesRead
);
}
os
.
close
();
ins
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
/**
* 删除本地临时文件
* @param file
*/
public
static
void
delteTempFile
(
File
file
)
{
if
(
file
!=
null
)
{
File
del
=
new
File
(
file
.
toURI
());
del
.
delete
();
}
}
/**
* 是否是图片
* @param fileSuffix
* @return
* @throws Exception
*/
public
static
boolean
isPicture
(
String
fileSuffix
)
throws
Exception
{
String
[]
imageArray
=
{
"bmp"
,
"dib"
,
"gif"
,
"jfif"
,
"jpe"
,
"jpeg"
,
"jpg"
,
"png"
,
"tif"
,
"tiff"
,
"ico"
,
"10"
};
return
Arrays
.
stream
(
imageArray
).
anyMatch
(
e
->
e
.
equals
(
fileSuffix
));
}
public
static
boolean
isOffice
(
String
fileSuffix
)
throws
Exception
{
String
[]
arr
=
{
"doc"
,
"docx"
,
"xls"
,
"xlsx"
,
"pptx"
,
"ppt"
};
return
Arrays
.
stream
(
arr
).
anyMatch
(
e
->
e
.
equals
(
fileSuffix
));
}
public
static
boolean
isMusic
(
String
fileSuffix
)
throws
Exception
{
String
[]
arr
=
{
"wma"
,
"mpc"
,
"ogg"
,
"wav"
,
"mpeg"
,
"mp3"
};
return
Arrays
.
stream
(
arr
).
anyMatch
(
e
->
e
.
equals
(
fileSuffix
));
}
}
}
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