Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
OSmall
/
linjiashop
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 78d6e5bb
authored
Dec 24, 2020
by
tyfanchz
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
调整文件上传接口为公共接口
1 parent
7ba0e58c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
287 additions
and
2 deletions
doc/http-request/http-client.env.json
shop-core/src/main/java/com/glsx/shop/web/controller/FileController.java
doc/http-request/http-client.env.json
View file @
78d6e5b
{
"local"
:
{
"domain_admin"
:
"http://localhost:8082"
,
"domain_mobile"
:
"http://localhost:8081"
"domain_admin"
:
"http://localhost:9402/shop-admin"
,
"domain_mobile"
:
"http://localhost:8209/shop-app"
},
"test"
:
{
"domain_admin"
:
"https://aiot-shop-mobile.frontend.test.glsx.com.cn/shop-admin"
,
"domain_mobile"
:
"https://aiot-shop-mobile.frontend.test.glsx.com.cn/shop-app"
}
}
shop-core/src/main/java/com/glsx/shop/web/controller/FileController.java
0 → 100644
View file @
78d6e5b
package
com
.
glsx
.
shop
.
web
.
controller
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.BufferedInputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.util.ArrayList
;
import
java.util.List
;
import
com.glsx.shop.bean.entity.shop.ShopUser
;
import
com.glsx.shop.bean.entity.system.FileInfo
;
import
com.glsx.shop.bean.exception.ApplicationExceptionEnum
;
import
com.glsx.shop.bean.vo.front.Rets
;
import
com.glsx.shop.bean.vo.shop.Base64File
;
import
com.glsx.shop.service.shop.ShopUserService
;
import
com.glsx.shop.service.system.FileService
;
import
com.glsx.shop.utils.CryptUtil
;
import
com.glsx.shop.utils.FdfsUtils
;
import
com.glsx.shop.utils.HttpUtil
;
import
com.glsx.shop.utils.JsonUtil
;
import
com.glsx.shop.utils.Maps
;
import
com.glsx.shop.utils.StringUtil
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.RequestPart
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.multipart.MultipartFile
;
@RestController
@RequestMapping
(
"/file"
)
public
class
FileController
extends
BaseController
{
@Autowired
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
FileController
.
class
);
@Autowired
private
FileService
fileService
;
@Autowired
private
ShopUserService
shopUserService
;
/**
* 上传文件
*
* @param file 文件
* @return 结果
*/
@RequestMapping
(
value
=
"/uploadFile"
,
method
=
{
RequestMethod
.
POST
})
public
Object
uploadFile
(
@RequestPart
(
"file"
)
MultipartFile
file
)
{
if
(
file
==
null
)
{
throw
ApplicationExceptionEnum
.
PARAM_ERROR
.
exception
();
}
try
{
String
fileUrl
=
FdfsUtils
.
uploadFile
(
file
);
super
.
logger
.
info
(
"$$$$ /uploadFile result: {}"
,
fileUrl
);
return
Rets
.
success
(
fileUrl
);
}
catch
(
IOException
e
)
{
throw
ApplicationExceptionEnum
.
FILE_UPLOAD_ERROR
.
exception
();
}
}
/**
* 上传多个文件
*
* @param files 文件
* @return 结果
*/
@RequestMapping
(
value
=
"/uploadMultipleFile"
,
method
=
{
RequestMethod
.
POST
})
public
Object
uploadMultipleFile
(
@RequestPart
(
"file"
)
MultipartFile
[]
files
)
{
if
(
files
==
null
||
files
.
length
==
0
)
{
throw
ApplicationExceptionEnum
.
PARAM_ERROR
.
exception
();
}
try
{
List
<
String
>
fileUrlList
=
new
ArrayList
<>();
for
(
MultipartFile
file
:
files
)
{
fileUrlList
.
add
(
FdfsUtils
.
uploadFile
(
file
));
}
super
.
logger
.
info
(
"$$$$ /uploadMultipleFile result: {}"
,
JsonUtil
.
toJson
(
fileUrlList
));
return
Rets
.
success
(
fileUrlList
);
}
catch
(
IOException
e
)
{
throw
ApplicationExceptionEnum
.
FILE_UPLOAD_ERROR
.
exception
();
}
}
/**
* 上传文件
*
* @return 结果
*/
@RequestMapping
(
value
=
"/uploadFile2"
,
method
=
{
RequestMethod
.
POST
})
public
Object
uploadFile2
()
{
String
fileUrl
=
FdfsUtils
.
uploadFile
(
new
byte
[]
{
65
,
10
},
"sql"
);
System
.
out
.
println
(
fileUrl
);
return
Rets
.
success
(
fileUrl
);
}
/**
* 删除服务器上的文件
*
* @param fileUrl 文件路径
* @return 结果
*/
@RequestMapping
(
value
=
"/deleteFile"
,
method
=
{
RequestMethod
.
POST
})
public
Object
deleteFile
(
String
fileUrl
)
{
if
(
StringUtils
.
isBlank
(
fileUrl
))
{
throw
ApplicationExceptionEnum
.
PARAM_ERROR
.
exception
();
}
try
{
super
.
logger
.
info
(
"$$$$ /deleteFile param: {}"
,
fileUrl
);
FdfsUtils
.
deleteFile
(
fileUrl
);
return
Rets
.
success
();
}
catch
(
RuntimeException
e
)
{
throw
ApplicationExceptionEnum
.
FILE_DELETE_ERROR
.
exception
();
}
}
/**
* 上传文件
*
* @param multipartFile
* @return
*/
@RequestMapping
(
value
=
"upload"
,
method
=
RequestMethod
.
POST
)
public
Object
upload
(
@RequestPart
(
"file"
)
MultipartFile
multipartFile
)
{
try
{
FileInfo
fileInfo
=
fileService
.
upload
(
multipartFile
);
return
Rets
.
success
(
fileInfo
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"上传文件异常"
,
e
);
return
Rets
.
failure
(
"上传文件失败"
);
}
}
@RequestMapping
(
value
=
"upload/base64"
,
method
=
RequestMethod
.
POST
)
public
Object
uploadUploadFileBase64
(
@RequestBody
Base64File
base64File
)
{
try
{
FileInfo
fileInfo
=
fileService
.
upload
(
base64File
);
ShopUser
user
=
shopUserService
.
getCurrentUser
();
user
.
setAvatar
(
String
.
valueOf
(
fileInfo
.
getRealFileName
()));
shopUserService
.
update
(
user
);
return
Rets
.
success
(
user
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"上传文件异常"
,
e
);
return
Rets
.
failure
(
"上传文件失败"
);
}
}
/**
* 下载文件
*
* @param fileName
*/
@RequestMapping
(
value
=
"download"
,
method
=
RequestMethod
.
GET
)
public
void
download
(
@RequestParam
(
value
=
"fileName"
,
required
=
false
)
String
fileName
,
@RequestParam
(
value
=
"idFile"
,
required
=
false
)
Long
idFile
)
{
FileInfo
fileInfo
;
if
(
StringUtil
.
isNotEmpty
(
fileName
))
{
fileInfo
=
fileService
.
getByName
(
fileName
);
}
else
if
(
idFile
!=
null
)
{
fileInfo
=
fileService
.
get
(
idFile
);
}
else
{
throw
ApplicationExceptionEnum
.
REQUEST_NULL
.
exception
();
}
fileName
=
fileInfo
.
getOriginalFileName
();
HttpServletResponse
response
=
HttpUtil
.
getResponse
();
response
.
setContentType
(
"application/x-download"
);
try
{
fileName
=
new
String
(
fileName
.
getBytes
(),
"ISO-8859-1"
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment;filename="
+
fileName
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
int
length
=
1024
;
byte
[]
buffer
=
new
byte
[
length
];
FileInputStream
fis
=
null
;
BufferedInputStream
bis
=
null
;
OutputStream
os
;
try
{
File
file
=
new
File
(
fileInfo
.
getAblatePath
());
os
=
response
.
getOutputStream
();
fis
=
new
FileInputStream
(
file
);
bis
=
new
BufferedInputStream
(
fis
);
int
i
=
bis
.
read
(
buffer
);
while
(
i
!=
-
1
)
{
os
.
write
(
buffer
);
buffer
=
new
byte
[
length
];
i
=
bis
.
read
(
buffer
);
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"download error"
,
e
);
}
finally
{
try
{
bis
.
close
();
fis
.
close
();
}
catch
(
IOException
e
)
{
logger
.
error
(
"close inputstream error"
,
e
);
}
}
}
/**
* 获取base64图片数据
*
* @param fileName
* @return
*/
@RequestMapping
(
value
=
"getImgBase64"
,
method
=
RequestMethod
.
GET
)
public
Object
getImgBase64
(
@RequestParam
(
"idFile"
)
String
fileName
)
{
FileInfo
fileInfo
=
fileService
.
getByName
(
fileName
);
FileInputStream
fis
=
null
;
try
{
File
file
=
new
File
(
fileInfo
.
getAblatePath
());
byte
[]
bytes
=
new
byte
[(
int
)
file
.
length
()];
fis
=
new
FileInputStream
(
file
);
BufferedInputStream
bis
=
new
BufferedInputStream
(
fis
);
bis
.
read
(
bytes
);
String
base64
=
CryptUtil
.
encodeBASE64
(
bytes
);
return
Rets
.
success
(
Maps
.
newHashMap
(
"imgContent"
,
base64
));
}
catch
(
Exception
e
)
{
logger
.
error
(
"get img error"
,
e
);
return
Rets
.
failure
(
"获取图片异常"
);
}
finally
{
try
{
fis
.
close
();
}
catch
(
Exception
e
)
{
logger
.
error
(
"close getImgBase64 error"
,
e
);
}
}
}
/**
* 获取图片流
*
* @param response
* @param fileName
*/
@RequestMapping
(
value
=
"getImgStream"
,
method
=
RequestMethod
.
GET
)
public
void
getImgStream
(
HttpServletResponse
response
,
@RequestParam
(
"idFile"
)
String
fileName
)
{
FileInfo
fileInfo
=
fileService
.
getByName
(
fileName
);
FileInputStream
fis
=
null
;
response
.
setContentType
(
"image/"
+
fileInfo
.
getRealFileName
().
split
(
"\\."
)[
1
]);
try
{
OutputStream
out
=
response
.
getOutputStream
();
File
file
=
new
File
(
fileInfo
.
getAblatePath
());
fis
=
new
FileInputStream
(
file
);
byte
[]
b
=
new
byte
[
fis
.
available
()];
fis
.
read
(
b
);
out
.
write
(
b
);
out
.
flush
();
}
catch
(
Exception
e
)
{
logger
.
error
(
"getImgStream error"
,
e
);
}
finally
{
if
(
fis
!=
null
)
{
try
{
fis
.
close
();
}
catch
(
IOException
e
)
{
logger
.
error
(
"close getImgStream error"
,
e
);
}
}
}
}
}
Write
Preview
Markdown
is supported
Attach a file
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 post a comment