Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
nyx
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
发现
nyx
Commits
215eece7
Commit
215eece7
authored
Nov 22, 2020
by
jscat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
nyx jaapp: 功能新增
1. 添加图片上传接口
parent
c854d6f0
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
205 行增加
和
4 行删除
+205
-4
app/nyx_app_key/api/src/main/java/cn/com/fun/nyxkey/api/utils/UploadFile.java
+86
-0
app/nyx_app_key/api/src/main/java/cn/com/fun/nyxkey/api/web/controller/FileController.java
+109
-0
app/nyx_app_key/api/src/main/resources/config/application-dev.yml
+5
-2
app/nyx_app_key/api/src/main/resources/config/application-production.yml
+5
-2
没有找到文件。
app/nyx_app_key/api/src/main/java/cn/com/fun/nyxkey/api/utils/UploadFile.java
0 → 100644
查看文件 @
215eece7
package
cn
.
com
.
fun
.
nyxkey
.
api
.
utils
;
import
java.io.BufferedOutputStream
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.UUID
;
import
org.springframework.web.multipart.MultipartFile
;
public
class
UploadFile
{
/**
*
* 多张/单张都可以用这个
* 保存图片
*
* refer: https://blog.csdn.net/zhangleiyes123/article/details/82792010
*
* @param List<MultipartFile>
* 要批量上传的文件
* @param path
* 图片保存的路径
* @return "WRONG_FILE_EXTENSION"-错误的后缀, "FILE_EMPTY"-空文件 或者 保存后的绝对路径
* @throws IOException
*/
public
static
List
<
String
>
uploadFiles
(
List
<
MultipartFile
>
files
,
String
path
,
String
subpath
)
throws
IOException
{
List
<
String
>
msgs
=
new
ArrayList
<>();
if
(
files
.
size
()
<
1
)
{
return
msgs
;
}
for
(
int
i
=
0
;
i
<
files
.
size
();
++
i
)
{
MultipartFile
file
=
files
.
get
(
i
);
if
(!
file
.
isEmpty
())
{
String
filename
=
file
.
getOriginalFilename
();
String
type
=
filename
.
indexOf
(
"."
)
!=
-
1
?
filename
.
substring
(
filename
.
lastIndexOf
(
"."
),
filename
.
length
())
:
null
;
if
(
type
==
null
)
{
// msgs.add("file_empty");
// return msgs;
continue
;
}
if
(!(
".PNG"
.
equals
(
type
.
toUpperCase
())
||
".JPG"
.
equals
(
type
.
toUpperCase
())))
{
// msgs.add("wrong_file_extension");
// return msgs;
continue
;
}
}
}
for
(
int
i
=
0
;
i
<
files
.
size
();
++
i
)
{
MultipartFile
file
=
files
.
get
(
i
);
String
filename
=
file
.
getOriginalFilename
();
String
type
=
filename
.
indexOf
(
"."
)
!=
-
1
?
filename
.
substring
(
filename
.
lastIndexOf
(
"."
),
filename
.
length
())
:
null
;
String
newname
=
subpath
+
UUID
.
randomUUID
()
+
type
;
String
filepath
=
path
+
newname
;
File
filesPath
=
new
File
(
path
+
subpath
);
if
(!
filesPath
.
exists
())
{
filesPath
.
mkdir
();
}
BufferedOutputStream
out
=
null
;
type
=
filepath
.
indexOf
(
"."
)
!=
-
1
?
filepath
.
substring
(
filepath
.
lastIndexOf
(
"."
)
+
1
,
filepath
.
length
())
:
null
;
try
{
out
=
new
BufferedOutputStream
(
new
FileOutputStream
(
new
File
(
filepath
)));
out
.
write
(
file
.
getBytes
());
msgs
.
add
(
newname
);
}
catch
(
Exception
e
)
{
// 没有上传成功
e
.
printStackTrace
();
}
finally
{
out
.
flush
();
out
.
close
();
}
}
return
msgs
;
}
}
\ No newline at end of file
app/nyx_app_key/api/src/main/java/cn/com/fun/nyxkey/api/web/controller/FileController.java
0 → 100644
查看文件 @
215eece7
package
cn
.
com
.
fun
.
nyxkey
.
api
.
web
.
controller
;
import
cn.com.fun.nyxkey.api.common.JSONResult
;
import
cn.com.fun.nyxkey.api.utils.UploadFile
;
import
io.swagger.annotations.ApiImplicitParams
;
import
io.swagger.annotations.ApiOperation
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
static
cn
.
com
.
fun
.
nyxkey
.
api
.
common
.
ExceptionMsg
.
FileEmpty
;
/**
* Created by jscat on 2020-11-21.
*/
@RestController
@RequestMapping
(
"/api"
)
public
class
FileController
{
//上传文件的路径
@Value
(
"${upload.image}"
)
public
String
imagePath
;
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
PayApiController
.
class
);
/**
* @Description 文件上传
*
* 流程:
* 1) 图片一张check
* 2) 通过的直接上传
* 3) 返回
*
* postman测试
* single: https://blog.csdn.net/maowendi/article/details/80537304/
* multi : https://blog.csdn.net/weixin_45739720/article/details/103724064
* 1. POST
* 2. Headers: Key:Content-Type/Value:multipart/form-data
* 3. body:
* single: Key: file/选择文件file/Value:mid_123.jpg
* multi : Key:files/选择文件file/Value:mid_123.jpg
* Key:files/选择文件file/Value:微信截图_20201018200554.png
*
* @param
* @return Map
*/
@ApiOperation
(
value
=
"单张图片上传"
,
notes
=
"单张图片上传"
)
@ApiImplicitParams
({
})
@RequestMapping
(
value
=
"/nyx/upload/pic"
,
method
=
RequestMethod
.
POST
)
@ResponseBody
public
JSONResult
uploadPic
(
@RequestParam
(
value
=
"file"
,
required
=
false
,
defaultValue
=
"0"
)
MultipartFile
multipartFile
)
throws
Exception
{
List
<
MultipartFile
>
files
=
new
ArrayList
<
MultipartFile
>();
files
.
add
(
multipartFile
);
Date
date
=
new
Date
();
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyyMMdd"
);
String
subpath
=
sdf
.
format
(
date
)
+
"/"
;
List
<
String
>
paths
=
UploadFile
.
uploadFiles
(
files
,
imagePath
,
subpath
);
LOGGER
.
info
(
"=============/api/nyx/upload/pic:{}"
);
int
totalCount
=
paths
.
size
();
if
(
totalCount
>=
1
)
{
return
new
JSONResult
(
totalCount
,
paths
);
}
else
{
return
new
JSONResult
(
FileEmpty
);
}
}
@ApiOperation
(
value
=
"多张图片上传"
,
notes
=
"多张图片上传"
)
@ApiImplicitParams
({
})
@RequestMapping
(
value
=
"/nyx/upload/pics"
,
method
=
RequestMethod
.
POST
)
@ResponseBody
public
JSONResult
uploadPics
(
@RequestParam
(
value
=
"files"
,
required
=
false
,
defaultValue
=
"0"
)
List
<
MultipartFile
>
files
)
throws
Exception
{
Date
date
=
new
Date
();
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyyMMdd"
);
String
subpath
=
sdf
.
format
(
date
)
+
"/"
;
List
<
String
>
paths
=
UploadFile
.
uploadFiles
(
files
,
imagePath
,
subpath
);
LOGGER
.
info
(
"=============/api/nyx/upload/pics:{}"
);
int
totalCount
=
paths
.
size
();
if
(
totalCount
>=
1
)
{
return
new
JSONResult
(
totalCount
,
paths
);
}
else
{
return
new
JSONResult
(
FileEmpty
);
}
}
}
app/nyx_app_key/api/src/main/resources/config/application-dev.yml
查看文件 @
215eece7
...
@@ -78,4 +78,7 @@ aliyun:
...
@@ -78,4 +78,7 @@ aliyun:
dirLogo
:
logo-dir/
dirLogo
:
logo-dir/
weixin
:
weixin
:
notifyUrl
:
https://wx.hisuhong.com/api/nyx/wx/pay/notify
notifyUrl
:
https://wx.hisuhong.com/api/nyx/wx/pay/notify
\ No newline at end of file
upload
:
image
:
e:\dev\pic\
\ No newline at end of file
app/nyx_app_key/api/src/main/resources/config/application-production.yml
查看文件 @
215eece7
...
@@ -62,4 +62,7 @@ aliyun:
...
@@ -62,4 +62,7 @@ aliyun:
dirLogo
:
logo-dir/
dirLogo
:
logo-dir/
weixin
:
weixin
:
notifyUrl
:
https://fun.hisuhong.com/api/nyx/wx/pay/notify
notifyUrl
:
https://fun.hisuhong.com/api/nyx/wx/pay/notify
\ No newline at end of file
upload
:
image
:
e:\dev\pic\
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论