Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
open-source
/
open-platform-admin
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 7873393f
authored
Dec 21, 2020
by
tyfanchz
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
增加文件上传接口
1 parent
d6d9a20e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
111 additions
and
0 deletions
src/main/java/com/glsx/controller/FileController.java
src/main/java/com/glsx/controller/FileController.java
0 → 100644
View file @
7873393
package
com
.
glsx
.
controller
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.util.ArrayList
;
import
java.util.List
;
import
cn.com.glsx.openplatform.enums.ResultCodeEnum
;
import
cn.com.glsx.openplatform.util.FdfsUtils
;
import
cn.com.glsx.openplatform.vo.R
;
import
com.alibaba.fastjson.JSON
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
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
{
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
FileController
.
class
);
/**
* 上传文件
*
* @param file 文件
* @return 结果
*/
@RequestMapping
(
value
=
"/uploadFile"
,
method
=
{
RequestMethod
.
POST
})
public
R
uploadFile
(
@RequestPart
(
"file"
)
MultipartFile
file
)
{
if
(
file
==
null
)
{
throw
ResultCodeEnum
.
PARAM_ERROR
.
exception
();
}
try
{
String
fileUrl
=
FdfsUtils
.
uploadFile
(
file
);
this
.
logger
.
info
(
"$$$$ /uploadFile result: {}"
,
fileUrl
);
return
R
.
ok
().
data
(
fileUrl
);
}
catch
(
IOException
e
)
{
throw
ResultCodeEnum
.
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
ResultCodeEnum
.
PARAM_ERROR
.
exception
();
}
try
{
List
<
String
>
fileUrlList
=
new
ArrayList
<>();
for
(
MultipartFile
file
:
files
)
{
fileUrlList
.
add
(
FdfsUtils
.
uploadFile
(
file
));
}
this
.
logger
.
info
(
"$$$$ /uploadMultipleFile result: {}"
,
JSON
.
toJSONString
(
fileUrlList
));
return
R
.
ok
().
data
(
fileUrlList
);
}
catch
(
IOException
e
)
{
throw
ResultCodeEnum
.
FILE_UPLOAD_ERROR
.
exception
();
}
}
/**
* 上传文件
*
* @return 结果
*/
@RequestMapping
(
value
=
"/uploadFile2"
,
method
=
{
RequestMethod
.
POST
})
public
Object
uploadFile2
()
{
byte
[]
bytes
;
try
{
bytes
=
Files
.
readAllBytes
(
Paths
.
get
(
"D:/Users/wbh/Desktop/glsx_car_service_sys_area.sql"
));
String
fileUrl
=
FdfsUtils
.
uploadFile
(
bytes
,
"sql"
);
System
.
out
.
println
(
fileUrl
);
return
R
.
ok
().
data
(
fileUrl
);
}
catch
(
IOException
e
)
{
throw
ResultCodeEnum
.
FILE_UPLOAD_ERROR
.
exception
();
}
}
/**
* 删除服务器上的文件
*
* @param fileUrl 文件路径
* @return 结果
*/
@RequestMapping
(
value
=
"/deleteFile"
,
method
=
{
RequestMethod
.
POST
})
public
Object
deleteFile
(
String
fileUrl
)
{
if
(
StringUtils
.
isBlank
(
fileUrl
))
{
throw
ResultCodeEnum
.
PARAM_ERROR
.
exception
();
}
try
{
this
.
logger
.
info
(
"$$$$ /deleteFile param: {}"
,
fileUrl
);
FdfsUtils
.
deleteFile
(
fileUrl
);
return
R
.
ok
();
}
catch
(
RuntimeException
e
)
{
throw
ResultCodeEnum
.
FILE_DELETE_ERROR
.
exception
();
}
}
}
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