Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
D
data-server
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
licc
data-server
Commits
e86bf3db
Commit
e86bf3db
authored
Feb 27, 2021
by
m1991
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
资讯模块:用户 多图片上传功能实现完成
parent
2a195619
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
469 additions
and
119 deletions
+469
-119
FileException.java
...rc/main/java/cn/wisenergy/common/utils/FileException.java
+17
-0
FileUtils.java
...on/src/main/java/cn/wisenergy/common/utils/FileUtils.java
+47
-0
shopZx.java
...gy-model/src/main/java/cn/wisenergy/model/app/shopZx.java
+158
-3
UploadService.java
...src/main/java/cn/wisenergy/service/app/UploadService.java
+17
-6
UploadServiceImpl.java
...java/cn/wisenergy/service/app/impl/UploadServiceImpl.java
+150
-9
Application.java
...rgy-web-admin/src/main/java/cn/wisenergy/Application.java
+3
-0
IndexController.java
...n/wisenergy/web/admin/controller/app/IndexController.java
+17
-0
UploadController.java
.../wisenergy/web/admin/controller/app/UploadController.java
+39
-101
FileProperty.java
...n/src/main/java/cn/wisenergy/web/config/FileProperty.java
+21
-0
No files found.
wisenergy-common/src/main/java/cn/wisenergy/common/utils/FileException.java
0 → 100644
View file @
e86bf3db
package
cn
.
wisenergy
.
common
.
utils
;
/**
* Created by m1991 on 2021/2/26 23:51
* TODO 图片自定义错误返回值工具类
*/
public
class
FileException
extends
RuntimeException
{
public
FileException
(
String
message
)
{
super
(
message
);
}
public
FileException
(
String
message
,
Throwable
cause
)
{
super
(
message
,
cause
);
}
}
wisenergy-common/src/main/java/cn/wisenergy/common/utils/FileUtils.java
0 → 100644
View file @
e86bf3db
package
cn
.
wisenergy
.
common
.
utils
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.File
;
import
java.io.IOException
;
/**
* Created by m1991 on 2021/2/26 18:56
*/
public
class
FileUtils
{
/**
* @param file 文件
* @param path 文件存放路径
* @param fileName 保存的文件名
* @return
*/
public
static
boolean
upload
(
MultipartFile
file
,
String
path
,
String
fileName
)
{
//确定上传的文件名
String
realPath
=
path
+
"\\"
+
fileName
;
System
.
out
.
println
(
"上传文件:"
+
realPath
);
File
dest
=
new
File
(
realPath
);
//判断文件父目录是否存在
if
(!
dest
.
getParentFile
().
exists
())
{
dest
.
getParentFile
().
mkdir
();
}
try
{
//保存文件
file
.
transferTo
(
dest
);
return
true
;
}
catch
(
IllegalStateException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
return
false
;
}
catch
(
IOException
e
)
{
// TODO Auto-generated catch block
e
.
printStackTrace
();
return
false
;
}
}
}
wisenergy-model/src/main/java/cn/wisenergy/model/app/shopZx.java
View file @
e86bf3db
...
@@ -8,7 +8,6 @@ import io.swagger.annotations.ApiModelProperty;
...
@@ -8,7 +8,6 @@ import io.swagger.annotations.ApiModelProperty;
import
lombok.Data
;
import
lombok.Data
;
import
java.io.Serializable
;
import
java.io.Serializable
;
import
java.util.Date
;
/**
/**
...
@@ -68,10 +67,10 @@ public class shopZx extends Model<shopZx> implements Serializable{
...
@@ -68,10 +67,10 @@ public class shopZx extends Model<shopZx> implements Serializable{
* 资讯数据创建时间
* 资讯数据创建时间
*/
*/
@ApiModelProperty
(
name
=
"zxDate"
,
value
=
"资讯数据创建时间"
)
@ApiModelProperty
(
name
=
"zxDate"
,
value
=
"资讯数据创建时间"
)
private
Dat
e
zxDate
;
private
Dat
a
zxDate
;
/**
/**
* 图片存放地址
* 图片存放地址
imgUrl,zxAddress,zxField,zxName,userid,zxUrl
*/
*/
@ApiModelProperty
(
name
=
"imgUrl"
,
value
=
"图片存放地址"
)
@ApiModelProperty
(
name
=
"imgUrl"
,
value
=
"图片存放地址"
)
private
String
imgUrl
;
private
String
imgUrl
;
...
@@ -81,4 +80,160 @@ public class shopZx extends Model<shopZx> implements Serializable{
...
@@ -81,4 +80,160 @@ public class shopZx extends Model<shopZx> implements Serializable{
*/
*/
@ApiModelProperty
(
name
=
"zxAddress"
,
value
=
"用户发布地址"
)
@ApiModelProperty
(
name
=
"zxAddress"
,
value
=
"用户发布地址"
)
private
String
zxAddress
;
private
String
zxAddress
;
// /**
// * 资讯文件名称
// */
// @ApiModelProperty(name = "fileName", value = "资讯文件名称")
// private String fileName;
//
// /**
// * 资讯文件地址
// */
// @ApiModelProperty(name = "fileDownloadUri", value = "资讯文件地址")
// private String fileDownloadUri;
//
// /**
// * 资讯文件类型
// */
// @ApiModelProperty(name = "fileType", value = "资讯文件类型")
// private String fileType;
//
// /**
// * 资讯文件名称
// */
// @ApiModelProperty(name = "size", value = "资讯文件大小")
// private long size;
//
// public shopZx(String fileName, String fileDownloadUri, String fileType, long size) {
// this.fileName = fileName;
// this.fileDownloadUri = fileDownloadUri;
// this.fileType = fileType;
// this.size = size;
// }
public
String
getImgUrl
()
{
return
imgUrl
;
}
public
void
setImgUrl
(
String
imgUrl
)
{
this
.
imgUrl
=
imgUrl
;
}
public
String
getZxAddress
()
{
return
zxAddress
;
}
public
void
setZxAddress
(
String
zxAddress
)
{
this
.
zxAddress
=
zxAddress
;
}
// public String getFileName() {
// return fileName;
// }
//
// public void setFileName(String fileName) {
// this.fileName = fileName;
// }
//
// public String getFileDownloadUri() {
// return fileDownloadUri;
// }
// public void setFileDownloadUri(String fileDownloadUri) {
// this.fileDownloadUri = fileDownloadUri;
// }
//
// public String getFileType() {
// return fileType;
// }
//
// public void setFileType(String fileType) {
// this.fileType = fileType;
// }
//
// public long getSize() {
// return size;
// }
//
// public void setSize(long size) {
// this.size = size;
// }
public
Data
getZxDate
()
{
return
zxDate
;
}
public
void
setZxDate
(
Data
zxDate
)
{
this
.
zxDate
=
zxDate
;
}
public
Integer
getZxid
()
{
return
zxid
;
}
public
void
setZxid
(
Integer
zxid
)
{
this
.
zxid
=
zxid
;
}
public
String
getZxUrl
()
{
return
zxUrl
;
}
public
void
setZxUrl
(
String
zxUrl
)
{
this
.
zxUrl
=
zxUrl
;
}
public
String
getUserid
()
{
return
userid
;
}
public
void
setUserid
(
String
userid
)
{
this
.
userid
=
userid
;
}
public
Integer
getZxLikes
()
{
return
zxLikes
;
}
public
void
setZxLikes
(
Integer
zxLikes
)
{
this
.
zxLikes
=
zxLikes
;
}
public
String
getZxName
()
{
return
zxName
;
}
public
void
setZxName
(
String
zxName
)
{
this
.
zxName
=
zxName
;
}
public
Integer
getZxShenHe
()
{
return
zxShenHe
;
}
public
void
setZxShenHe
(
Integer
zxShenHe
)
{
this
.
zxShenHe
=
zxShenHe
;
}
public
String
getZxField
()
{
return
zxField
;
}
public
void
setZxField
(
String
zxField
)
{
this
.
zxField
=
zxField
;
}
@Override
public
String
toString
()
{
return
"shopzx{"
+
"zxid="
+
zxid
+
", zxUrl="
+
zxUrl
+
", userid="
+
userid
+
", zxLikes="
+
zxLikes
+
", zxName="
+
zxName
+
", zxShenHe="
+
zxShenHe
+
", zxField="
+
zxField
+
"}"
;
}
}
}
wisenergy-service/src/main/java/cn/wisenergy/service/app/UploadService.java
View file @
e86bf3db
package
cn
.
wisenergy
.
service
.
app
;
package
cn
.
wisenergy
.
service
.
app
;
import
cn.wisenergy.model.app.shopZx
;
import
cn.wisenergy.model.app.shopZx
;
import
org.springframework.core.io.Resource
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
...
@@ -9,28 +10,38 @@ import java.util.Map;
...
@@ -9,28 +10,38 @@ import java.util.Map;
/**
/**
* Created by m1991 on 2021/2/24 13:49
* Created by m1991 on 2021/2/24 13:49
* TODO 资讯Service
*/
*/
public
interface
UploadService
{
public
interface
UploadService
{
/**
/**
* 视频文件上传 zxUrl, userId,zxName,zxField,imgUrl
*
TODO
视频文件上传 zxUrl, userId,zxName,zxField,imgUrl
* @param request
* @param request
* @return
* @return
*/
*/
public
Map
<
String
,
Object
>
uploadVideo
(
MultipartFile
file
,
HttpServletRequest
request
,
String
userId
,
String
zxField
,
String
zxAddress
)
throws
Exception
;
Map
<
String
,
Object
>
uploadVideo
(
MultipartFile
file
,
HttpServletRequest
request
,
String
userId
,
String
zxField
,
String
zxAddress
)
throws
Exception
;
/**
/**
* 图片文件上传
*
TODO
图片文件上传
* @param request
* @param request
* @return
* @return
*/
*/
public
Map
<
String
,
Object
>
uploadImage
(
MultipartFile
file
,
HttpServletRequest
request
,
String
userId
,
String
zxField
,
String
zxAddress
)
throws
Exception
;
Map
<
String
,
Object
>
uploadImage
(
MultipartFile
file
,
HttpServletRequest
request
,
String
userId
,
String
zxField
,
String
zxAddress
)
throws
Exception
;
/**
/**
* 项目目录下的图片文件上传
*
TODO
项目目录下的图片文件上传
* @param request
* @param request
* @return
* @return
*/
*/
public
Map
<
String
,
Object
>
getImageUrl
(
HttpServletRequest
request
)
throws
Exception
;
Map
<
String
,
Object
>
getImageUrl
(
HttpServletRequest
request
)
throws
Exception
;
/**
* TODO 多文件上传
* @param file
* @return
*/
List
imageUpload
(
MultipartFile
[]
file
,
String
userId
,
String
zxField
,
String
zxAddress
);
String
storeFile
(
MultipartFile
file
);
Resource
loadFileAsResource
(
String
fileName
);
/**
/**
* 查询资讯所有信息(分页——10条信息一页)
* 查询资讯所有信息(分页——10条信息一页)
...
...
wisenergy-service/src/main/java/cn/wisenergy/service/app/impl/UploadServiceImpl.java
View file @
e86bf3db
package
cn
.
wisenergy
.
service
.
app
.
impl
;
package
cn
.
wisenergy
.
service
.
app
.
impl
;
import
cn.wisenergy.common.utils.FileException
;
import
cn.wisenergy.common.utils.FileUtils
;
import
cn.wisenergy.common.utils.FrameGrabberKit
;
import
cn.wisenergy.common.utils.FrameGrabberKit
;
import
cn.wisenergy.model.app.shopZx
;
import
com.alibaba.fastjson.JSON
;
import
cn.wisenergy.service.app.UploadService
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.UrlResource
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.*
;
import
java.io.IOException
;
import
java.net.MalformedURLException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
import
cn.wisenergy.service.app.UploadService
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.InputStream
;
import
java.util.*
;
import
java.util.*
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
cn.wisenergy.mapper.ShopZxMapper
;
import
cn.wisenergy.mapper.ShopZxMapper
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.transaction.annotation.Transactional
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.web.multipart.MultipartFile
;
/**
/**
* Created by m1991 on 2021/2/24 13:42
* Created by m1991 on 2021/2/24 13:42
...
@@ -169,11 +186,135 @@ public class UploadServiceImpl implements UploadService {
...
@@ -169,11 +186,135 @@ public class UploadServiceImpl implements UploadService {
result
.
put
(
"returnCode"
,
0
);
result
.
put
(
"returnCode"
,
0
);
return
result
;
return
result
;
}
}
//, String userId, String zxField, String zxAddress
@Override
public
List
imageUpload
(
MultipartFile
[]
files
,
String
userId
,
String
zxField
,
String
zxAddress
)
{
//获取上传图片数量,打印在控制台
System
.
out
.
println
(
"上传图片数量"
+
files
.
length
);
//创建集合
List
<
Map
<
String
,
Object
>>
root
=
new
ArrayList
<
Map
<
String
,
Object
>>();
String
imgUrl
=
null
;
String
zxName
=
null
;
String
fileName
=
null
;
String
zxUrl
=
null
;
// 要上传的目标文件存放的绝对路径
final
String
localPath
=
"d:\\IDEAProject\\imageupload\\target\\classes\\static\\img"
;
Map
<
String
,
Object
>
result
=
new
HashMap
<
String
,
Object
>();
//一个文件上传的结果
String
result_msg
=
""
;
//上传结果信息
//遍历图片数据
for
(
MultipartFile
file
:
files
)
{
// Map<String, Object> result = new HashMap<String, Object>();//一个文件上传的结果
if
(
file
.
isEmpty
())
{
System
.
out
.
println
(
"上传图片为空,请重新上传"
);
}
//判断上传文件格式
String
fileType
=
file
.
getContentType
();
if
(
fileType
.
equals
(
"image/jpeg"
)
||
fileType
.
equals
(
"image/png"
)
||
fileType
.
equals
(
"image/jpeg"
))
{
// 要上传的目标文件存放的绝对路径
//上传后保存的文件名(需要防止图片重名导致的文件覆盖)
//获取文件名
fileName
=
file
.
getOriginalFilename
();
//获取文件后缀名
String
suffixName
=
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
));
//重新生成文件名
fileName
=
UUID
.
randomUUID
()
+
suffixName
;
zxUrl
=
""
;
zxName
+=
fileName
+
","
;
imgUrl
+=
localPath
+
fileName
+
","
;
// shopZxMapper.zxadd(zxUrl,userId,zxName,zxField,imgUrl,zxAddress);
if
(
FileUtils
.
upload
(
file
,
localPath
,
fileName
))
{
//文件存放的相对路径(一般存放在数据库用于img标签的src)
String
relativePath
=
"img/"
+
fileName
;
result
.
put
(
"relativePath"
,
relativePath
);
//前端根据是否存在该字段来判断上传是否成功
result_msg
=
"图片上传成功"
;
result
.
put
(
"imgUrl"
,
imgUrl
);
}
else
{
result_msg
=
"图片上传失败"
;
}
}
else
{
result_msg
=
"图片格式不正确"
;
}
}
result
.
put
(
"result_msg"
,
result_msg
);
root
.
add
(
result
);
String
root_json
=
JSON
.
toJSONString
(
root
);
System
.
out
.
println
(
root_json
);
result
.
put
(
"returnCode"
,
0
);
System
.
out
.
println
(
zxName
);
System
.
out
.
println
(
imgUrl
);
shopZxMapper
.
zxadd
(
zxUrl
,
userId
,
zxName
,
zxField
,
imgUrl
,
zxAddress
);
return
root
;
}
//
/**
/**
//
* 获取资讯分页数据
* 获取资讯分页数据
//
* @return
* @return
//
*/
*/
// @Override
// @Override
/**
* 多文件上传 TODO
*/
private
Path
fileStorageLocation
;
// 文件在本地存储的地址
public
UploadServiceImpl
(
@Value
(
"${file.upload.path}"
)
String
path
)
{
this
.
fileStorageLocation
=
Paths
.
get
(
path
).
toAbsolutePath
().
normalize
();
try
{
Files
.
createDirectories
(
this
.
fileStorageLocation
);
}
catch
(
IOException
e
)
{
throw
new
FileException
(
"Could not create the directory"
,
e
);
}
}
/**
* 存储文件到系统
* @param file 文件
* @return 文件名
*/
public
String
storeFile
(
MultipartFile
file
)
{
// Normalize file name
String
fileName
=
StringUtils
.
cleanPath
(
file
.
getOriginalFilename
());
try
{
// Check if the file's name contains invalid characters
if
(
fileName
.
contains
(
".."
))
{
throw
new
FileException
(
"Sorry! Filename contains invalid path sequence "
+
fileName
);
}
// Copy file to the target location (Replacing existing file with the same name)
Path
targetLocation
=
this
.
fileStorageLocation
.
resolve
(
fileName
);
Files
.
copy
(
file
.
getInputStream
(),
targetLocation
,
StandardCopyOption
.
REPLACE_EXISTING
);
return
fileName
;
}
catch
(
IOException
ex
)
{
throw
new
FileException
(
"Could not store file "
+
fileName
+
". Please try again!"
,
ex
);
}
}
public
Resource
loadFileAsResource
(
String
fileName
)
{
try
{
Path
filePath
=
this
.
fileStorageLocation
.
resolve
(
fileName
).
normalize
();
Resource
resource
=
new
UrlResource
(
filePath
.
toUri
());
if
(
resource
.
exists
())
{
return
resource
;
}
else
{
throw
new
FileException
(
"File not found "
+
fileName
);
}
}
catch
(
MalformedURLException
ex
)
{
throw
new
FileException
(
"File not found "
+
fileName
,
ex
);
}
}
}
}
wisenergy-web-admin/src/main/java/cn/wisenergy/Application.java
View file @
e86bf3db
...
@@ -21,5 +21,8 @@ public class Application {
...
@@ -21,5 +21,8 @@ public class Application {
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
Application
.
class
,
args
);
SpringApplication
.
run
(
Application
.
class
,
args
);
}
}
}
}
wisenergy-web-admin/src/main/java/cn/wisenergy/web/admin/controller/app/IndexController.java
0 → 100644
View file @
e86bf3db
package
cn
.
wisenergy
.
web
.
admin
.
controller
.
app
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
/**
* Created by m1991 on 2021/2/27 0:43
*/
@Controller
public
class
IndexController
{
@GetMapping
(
"/"
)
public
String
index
()
{
return
"index"
;
}
}
wisenergy-web-admin/src/main/java/cn/wisenergy/web/admin/controller/app/UploadController.java
View file @
e86bf3db
package
cn
.
wisenergy
.
web
.
admin
.
controller
.
app
;
package
cn
.
wisenergy
.
web
.
admin
.
controller
.
app
;
import
cn.wisenergy.common.utils.FileUtils
;
import
cn.wisenergy.model.app.shopZx
;
import
cn.wisenergy.service.app.UploadService
;
import
cn.wisenergy.service.app.UploadService
;
import
io.swagger.annotations.*
;
import
com.alibaba.fastjson.JSON
;
import
lombok.extern.slf4j.Slf4j
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.io.Resource
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.servlet.support.ServletUriComponentsBuilder
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.
File
;
import
java.io.
IOException
;
import
java.
io.FileOutputStream
;
import
java.
util.ArrayList
;
import
org.springframework.stereotype.Controller
;
import
io.swagger.annotations.*
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
java.io.File
;
import
java.util.*
;
import
java.util.ArrayList
;
import
java.util.stream.Collectors
;
import
java.util.List
;
import
java.util.Map
;
import
static
cn
.
wisenergy
.
common
.
utils
.
exception
.
Result
.
RESULT_FLG
.
SUCCESS
;
/**
/**
* Created by m1991 on 2021/2/24 13:46
* Created by m1991 on 2021/2/24 13:46
...
@@ -73,106 +77,40 @@ public class UploadController {
...
@@ -73,106 +77,40 @@ public class UploadController {
* @return
* @return
* @throws Exception
* @throws Exception
*/
*/
@ApiOperation
(
value
=
"上传图片,返回路径给前台"
,
notes
=
"上传图片,返回路径给前台"
,
httpMethod
=
"POST"
)
@ApiOperation
(
value
=
"上传图片,返回路径给前台"
,
notes
=
"上传图片,返回路径给前台"
,
httpMethod
=
"POST"
,
produces
=
"application/json; charset=UTF-8"
)
@ApiImplicitParams
({
@ApiImplicitParams
({
@ApiImplicitParam
(
name
=
"zxField"
,
value
=
"资讯文字"
,
required
=
false
,
dataType
=
"String"
),
@ApiImplicitParam
(
name
=
"zxField"
,
value
=
"资讯文字"
,
required
=
false
,
dataType
=
"String"
),
@ApiImplicitParam
(
name
=
"userId"
,
value
=
"用户手机号"
,
required
=
true
,
dataType
=
"String"
)})
@ApiImplicitParam
(
name
=
"userId"
,
value
=
"用户手机号"
,
required
=
true
,
dataType
=
"String"
)})
@RequestMapping
(
value
=
"/uploadImage"
,
method
=
RequestMethod
.
POST
)
@RequestMapping
(
value
=
"/uploadImage"
,
method
=
RequestMethod
.
POST
)
public
Map
<
String
,
Object
>
uploadImage
(
MultipartFile
file
,
HttpServletRequest
request
,
String
userId
,
String
zxField
,
String
zxAddress
)
throws
Exception
{
public
Map
<
String
,
Object
>
uploadImage
(
MultipartFile
file
,
HttpServletRequest
request
,
String
userId
,
String
zxField
,
String
zxAddress
)
throws
Exception
{
return
uploadService
.
uploadImage
(
file
,
request
,
userId
,
zxField
,
zxAddress
);
}
@ApiOperation
(
value
=
"多图片,返回路径给前台"
,
notes
=
"上传图片,返回路径给前台"
,
httpMethod
=
"POST"
)
@RequestMapping
(
"/uploadImg"
)
@ResponseBody
public
void
uploadImg
(
MultipartFile
file
[],
String
areaName
)
throws
Exception
{
System
.
out
.
println
(
"得到的areaName:"
+
areaName
);
// 设置上传的路径是D盘下的picture
String
imgPath
=
"D:/picture/"
;
for
(
MultipartFile
f
:
file
)
{
// 图片的名字用毫秒数+图片原来的名字拼接
System
.
out
.
println
(
f
.
getSize
());
System
.
out
.
println
(
f
.
getBytes
());
String
imgName
=
System
.
currentTimeMillis
()
+
f
.
getOriginalFilename
();
//上传文件
uploadFileUtil
(
f
.
getBytes
(),
imgPath
,
imgName
);
}
return
uploadService
.
uploadImage
(
file
,
request
,
userId
,
zxField
,
zxAddress
);
}
}
/**
/**
* 上传文件的方法
* 多文件上传
* @param file:文件的字节
*
* @param imgPath:文件的路径
* @param files @RequestParam("file") /**
* @param imgName:文件的名字
* * 多文件上传接口
* @throws Exception
* * T0D0 value = "/fileUpload", produces = "application/json;charset=UTF-8
* *
* @return
*/
*/
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
UploadController
.
class
);
public
void
uploadFileUtil
(
byte
[]
file
,
String
imgPath
,
String
imgName
)
throws
Exception
{
@ApiOperation
(
value
=
"上传多图片,返回路径给前台"
,
notes
=
"上传图片,返回路径给前台"
,
httpMethod
=
"POST"
,
produces
=
"application/json; charset=UTF-8"
)
File
targetFile
=
new
File
(
imgPath
);
@ApiImplicitParams
({
if
(!
targetFile
.
exists
())
{
@ApiImplicitParam
(
name
=
"zxField"
,
value
=
"资讯文字"
,
required
=
false
,
dataType
=
"String"
),
targetFile
.
mkdirs
();
@ApiImplicitParam
(
name
=
"zxAddress"
,
value
=
"发表地址"
,
required
=
false
,
dataType
=
"String"
),
}
@ApiImplicitParam
(
name
=
"userId"
,
value
=
"用户手机号"
,
required
=
true
,
dataType
=
"String"
)})
FileOutputStream
out
=
new
FileOutputStream
(
imgPath
+
imgName
);
@RequestMapping
(
"/multipleImageUpload"
)
out
.
write
(
file
);
public
List
multipleImageUpload
(
MultipartFile
[]
files
,
String
userId
,
String
zxField
,
String
zxAddress
)
{
out
.
flush
();
return
uploadService
.
imageUpload
(
files
,
userId
,
zxField
,
zxAddress
);
out
.
close
();
}
@ApiOperation
(
value
=
"多图片,返回路径给前台"
,
notes
=
"上传图片,返回路径给前台"
,
httpMethod
=
"POST"
)
@RequestMapping
(
"/filesUpload"
)
//requestParam要写才知道是前台的那个数组
public
String
filesUpload
(
@RequestParam
(
"myfiles"
)
MultipartFile
[]
files
,
HttpServletRequest
request
)
{
List
<
String
>
list
=
new
ArrayList
<
String
>();
if
(
files
!=
null
&&
files
.
length
>
0
)
{
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++)
{
MultipartFile
file
=
files
[
i
];
// 保存文件
list
=
saveFile
(
request
,
file
,
list
);
}
}
//写着测试,删了就可以
// for (int i = 0; i < list.size(); i++) {
// System.out.println("集合里面的数据" + list.get(i));
// }
return
"index"
;
//跳转的页面
}
private
List
<
String
>
saveFile
(
HttpServletRequest
request
,
// ,
MultipartFile
file
,
List
<
String
>
list
)
{
// 判断文件是否为空
if
(!
file
.
isEmpty
())
{
try
{
// 保存的文件路径(如果用的是Tomcat服务器,文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\upload\\文件夹中
// )
String
filePath
=
request
.
getSession
().
getServletContext
()
.
getRealPath
(
"/"
)
+
"upload/"
+
file
.
getOriginalFilename
();
list
.
add
(
file
.
getOriginalFilename
());
File
saveDir
=
new
File
(
filePath
);
if
(!
saveDir
.
getParentFile
().
exists
())
saveDir
.
getParentFile
().
mkdirs
();
// 转存文件
file
.
transferTo
(
saveDir
);
return
list
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
return
list
;
}
// @ApiOperation(value = "资讯分页获取每页10条", notes = "资讯分页获取每页10条", httpMethod = "POST")
// @RequestMapping(value = "/findAll",method = RequestMethod.POST)
// public Object findAll(HttpServletRequest request){
//
// }
}
}
\ No newline at end of file
wisenergy-web-admin/src/main/java/cn/wisenergy/web/config/FileProperty.java
0 → 100644
View file @
e86bf3db
package
cn
.
wisenergy
.
web
.
config
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
@ConfigurationProperties
(
prefix
=
"file"
)
/**
* Created by m1991 on 2021/2/26 23:47
* TODO 文件配置
*/
public
class
FileProperty
{
private
String
filePath
;
public
String
getFilePath
()
{
return
filePath
;
}
public
void
setFilePath
(
String
filePath
)
{
this
.
filePath
=
filePath
;
}
}
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