- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Spring boot 实现单个或批量文件上传功能由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
一:添加依赖:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- thymeleaf模板插件 -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-thymeleaf</artifactid>
</dependency>
<!-- jsp依赖 -->
<dependency>
<groupid>javax.servlet</groupid>
<artifactid>jstl</artifactid>
</dependency>
<dependency>
<groupid>org.apache.tomcat.embed</groupid>
<artifactid>tomcat-embed-jasper</artifactid>
<!--<scope>provided</scope>-->
</dependency>
|
二:application.xml配置文件路径:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#配置上传文件地址
image.location.path=f:/image/
#配置文件大小限制
spring.http.multipart.maxfilesize=100mb
spring.http.multipart.maxrequestsize=100mb
#静态页面的访问配置
spring.thymeleaf.cache=
false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=
true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=utf-
8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=html5
|
三:编写静态页面(src/main/resources下建文件夹static(static存放静态文件,比如 css、js、image…)和templates(存放静态页面)两个是同级目录),先在templates 中新建一个 uploadimg.html.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<!doctype html>
<html>
<head>
<title>uploadimg.html</title>
<meta name=
"keywords"
content=
"keyword1,keyword2,keyword3"
></meta>
<meta name=
"description"
content=
"this is my page"
></meta>
<meta name=
"content-type"
content=
"text/html; charset=utf-8"
></meta>
<!--<link rel=
"stylesheet"
type=
"text/css"
href=
"./styles.css"
rel=
"external nofollow"
>-->
</head>
<body>
<form enctype=
"multipart/form-data"
method=
"post"
action=
"/dc/fileupload"
>
图片<input type=
"file"
name=
"file"
/>
<input type=
"submit"
value=
"上传"
/>
</form>
</body>
</html>
|
四:编写controller层:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
package
com.hot.analysis.controller.file;
import
java.io.bufferedinputstream;
import
java.io.file;
import
java.io.fileinputstream;
import
java.io.ioexception;
import
java.io.outputstream;
import
java.util.date;
import
java.util.random;
import
javax.servlet.http.httpservletrequest;
import
javax.servlet.http.httpservletresponse;
import
org.springframework.beans.factory.annotation.value;
import
org.springframework.web.bind.annotation.postmapping;
import
org.springframework.web.bind.annotation.requestmapping;
import
org.springframework.web.bind.annotation.responsebody;
import
org.springframework.web.bind.annotation.restcontroller;
import
org.springframework.web.multipart.multipartfile;
import
org.springframework.web.servlet.modelandview;
import
com.hot.analysis.exception.myexception;
@restcontroller
public
class
fileuploadcontroller {
//获取配置文件的路径
@value
(
"${image.location.path}"
)
private
string resourcedir;
/**
* 实现文件上传
* */
@requestmapping
(value =
"/index"
)
public
modelandview toindex() {
modelandview mv =
new
modelandview(
"uploadimg"
);
return
mv;
}
//单个文件上传
@requestmapping
(
"/dc/fileupload"
)
@responsebody
public
string fileupload( multipartfile file){
// 获取上传文件路径
string uploadpath = file.getoriginalfilename();
// 获取上传文件的后缀
string filesuffix = uploadpath.substring(uploadpath.lastindexof(
"."
) +
1
, uploadpath.length());
if
(filesuffix.equals(
"apk"
)) {
uploadpath = resourcedir;
}
else
{
// 上传目录地址
// string uploadpath="e:/hot-manage/image/";//windows路径
uploadpath =resourcedir;
// liux路劲
}
// 上传文件名
string filename =
new
date().gettime() +
new
random().nextint(
100
) +
"."
+ filesuffix;
file savefile =
new
file(uploadpath + filename);
if
(!savefile.getparentfile().exists()) {
savefile.getparentfile().mkdirs();
}
try
{
file.transferto(savefile);
}
catch
(illegalstateexception e) {
e.printstacktrace();
}
catch
(ioexception e) {
e.printstacktrace();
}
if
(filesuffix.equals(
"apk"
)) {
return
"/apk/"
+ filename;
}
else
{
return
"/image/"
+ filename;
}
}
// 批量上传
@postmapping
(
"/dc/morefileupload"
)
public
string bacthfileupload(multipartfile[] file)
throws
myexception {
stringbuffer buffer =
new
stringbuffer();
for
(multipartfile multipartfile : file) {
string str = fileupload(multipartfile);
buffer.append(str);
buffer.append(
","
);
}
string all = buffer.substring(
0
, buffer.length() -
1
);
return
all;
}
// 删除文件
@postmapping
(
"/dc/deletefile"
)
public
string delfile(string path) {
string resultinfo =
null
;
int
lastindexof = path.lastindexof(
"/"
);
string sb = path.substring(lastindexof +
1
, path.length());
sb =
"f:/image/"
+ sb;
file file =
new
file(sb);
if
(file.exists()) {
if
(file.delete()) {
resultinfo =
"1-删除成功"
;
}
else
{
resultinfo =
"0-删除失败"
;
}
}
else
{
resultinfo =
"文件不存在!"
;
}
return
resultinfo;
}
//文件下载相关代码
@requestmapping
(
"/download"
)
public
string downloadfile(httpservletrequest request, httpservletresponse response) {
string filename =
"aim_test.txt"
;
// 设置文件名,根据业务需要替换成要下载的文件名
if
(filename !=
null
) {
//设置文件路径
string realpath =
"d://aim//"
;
file file =
new
file(realpath , filename);
if
(file.exists()) {
response.setcontenttype(
"application/force-download"
);
// 设置强制下载不打开
response.addheader(
"content-disposition"
,
"attachment;filename="
+ filename);
// 设置文件名
byte
[] buffer =
new
byte
[
1024
];
fileinputstream fis =
null
;
bufferedinputstream bis =
null
;
try
{
fis =
new
fileinputstream(file);
bis =
new
bufferedinputstream(fis);
outputstream os = response.getoutputstream();
int
i = bis.read(buffer);
while
(i != -
1
) {
os.write(buffer,
0
, i);
i = bis.read(buffer);
}
system.out.println(
"success"
);
}
catch
(exception e) {
e.printstacktrace();
}
finally
{
if
(bis !=
null
) {
try
{
bis.close();
}
catch
(ioexception e) {
e.printstacktrace();
}
}
if
(fis !=
null
) {
try
{
fis.close();
}
catch
(ioexception e) {
e.printstacktrace();
}
}
}
}
}
return
null
;
}
}
|
测试:
成功返回路径:
查看文件夹:
总结 。
以上所述是小编给大家介绍的spring boot 实现单个或批量文件上传功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。
原文链接:https://blog.csdn.net/qq_33355858/article/details/81747101 。
最后此篇关于Spring boot 实现单个或批量文件上传功能的文章就讲到这里了,如果你想了解更多关于Spring boot 实现单个或批量文件上传功能的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
有人可以解释一下 spring-boot-parent 和 spring-boot-starter-parent 之间的区别吗,正如我在下面附加的 GIT HUB 代码链接之一中看到的,他们为 spr
我有与 jersey 框架集成的 Spring Boot 应用程序。 现在,当我尝试运行该应用程序时,它只是停留在 Spring 启动徽标上,之后没有任何 react 。 我也尝试添加 -X ,但徽标
我指的是 Spring Boot 关于 的文档自动配置 和 执行器 模块: 自动配置: Spring Boot AutoConfiguration attempts to automatically
我正在尝试将 apache log4j 集成到我的 Spring boot 应用程序中。这是我的 build.gradle 文件: build.gradle buildscript { rep
使用 Spring Boot Maven 插件的以下命令在生产中启动 Spring Boot 应用程序是否是一个好主意或实践? mvn spring-boot:run 最佳答案 不,这是个坏主意。 您
据我所知,spring boot 和 spring session 为我们提供了一站式自动配置,但是当我的应用程序使用 session redis 和应用程序缓存 redis 时,不是同一个 redi
我希望使用Spring Boot创建一个新的Web应用程序。不幸的是,我的服务器在技术堆栈方面相当有限。它安装了Java 5。 谁能告诉我spring boot是否可以在Java 1.5上运行以及什么
我有3个实体 CarWash(设置Wash) Wash(car_wash_id FK到CarWash) WashComment(wash_id FK到Wash) 有什么办法可以写这个查询 @Qu
我一直在关注this文章。 我正在尝试在Spring-boot应用程序中优雅地处理gRPC错误,的主要目标是能够在gRPC客户端中获取错误状态。 在上面的文章之后,我坚持为异常添加拦截器。如何在Spr
我有一个要使用的自定义log4j布局插件。在IntelliJ中运行或与./gradlew bootRun一起运行时,插件可以正常工作。不使用./gradlew bootJar构建启动jar。 启用-D
我想在给定范围 (5001-5100) 的随机端口上启动 Spring Cloud 应用程序(Spring Boot 1.5.14,Spring Cloud Edgware.SR4)。我知道我们可以使
任何人都可以向我展示或指出不使用 spring boot gradle 插件的 spring boot gradle 项目。 我正在寻找类似不使用 gradle 插件的 spring boot sta
我当时尝试包含上述依赖项之一,但找不到任何区别: spring boot starter web:我可以看到 Flux 和 Mono 类并制作一个响应式(Reactive)休息 Controller
我们一直在为我们的应用程序使用 Springboot 1.X。 现在准备开始一些新的应用程序,想知道我们是应该使用 SpringBoot2.0 还是坚持使用 SpringBoot 1.X? 对一种方式
我希望记录应用程序正在加载 application-profile.propeties 或 application.yml。怎么做。在哪种方法中,我可以听取它并检测它是成功加载还是失败。 最佳答案 您
当我在 pom.xml 中添加简单的 spring-boot-starter-data-jpa 依赖项时,在 pom.xml 文件中出现错误。如果我删除该依赖项,则不会再有错误。我不确定为什么会发生这
我希望记录应用程序正在加载 application-profile.propeties 或 application.yml。怎么做。在哪种方法中,我可以听取它并检测它是成功加载还是失败。 最佳答案 您
我在网上看了很多关于 spring-boot-devtools 的文章和问题,但仍然无法弄清楚为什么它对我不起作用。每次运行我的应用程序时,我都会得到以下信息: 17:54:28.057 [main]
我正在尝试将现有的 Spring 应用程序移植到 Spring Boot。我不使用 spring-boot-starter-data-solr 启动器,但是我的类路径上有 apache solrj (
(这主要是一个历史问题。Pivotal 建议所有论坛讨论都在 StackOverflow 上进行,这就是我在这里问它的原因。) Spring Boot 项目用来证明将应用程序的类和依赖项从可执行 ja
我是一名优秀的程序员,十分优秀!