服务器之家

服务器之家 > 正文

Spring boot实现文件上传功能

时间:2021-05-11 15:28     来源/作者:卡通人物nnn

本文实例为大家分享了spring boot实现文件上传的具体代码,供大家参考,具体内容如下

1. 创建一个maven的web工程,然后配置pom.xml文件,增加依赖:

?
1
2
3
4
5
<dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-web</artifactid>
 <version>1.0.2.release</version>
</dependency>

2. 在webapp目录下的index.jsp文件中输入一个表单:

?
1
2
3
4
5
6
7
8
9
<html>
<body>
<form method="post" enctype="multipart/form-data" action="/upload">
 file to upload: <input type="file" name="file"><br />
 name: <input type="text" name="name"><br /> <br />
 <input type="submit" value="upload">
  press here to upload the file!
</form>
</body>

这个表单就是我们模拟的上传页面

3. 编写处理这个表单的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
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileoutputstream;
 
import org.springframework.stereotype.controller;
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.responsebody;
import org.springframework.web.multipart.multipartfile;
 
@controller
public class fileuploadcontroller {
 
 @requestmapping(value="/upload", method=requestmethod.get)
 public @responsebody string provideuploadinfo() {
  return "you can upload a file by posting to this same url.";
 }
 
 @requestmapping(value="/upload", method=requestmethod.post)
 public @responsebody string handlefileupload(@requestparam("name") string name,
   @requestparam("file") multipartfile file){
  if (!file.isempty()) {
   try {
    byte[] bytes = file.getbytes();
    bufferedoutputstream stream =
      new bufferedoutputstream(new fileoutputstream(new file(name + "-uploaded")));
    stream.write(bytes);
    stream.close();
    return "you successfully uploaded " + name + " into " + name + "-uploaded !";
   } catch (exception e) {
    return "you failed to upload " + name + " => " + e.getmessage();
   }
  } else {
   return "you failed to upload " + name + " because the file was empty.";
  }
 }
 
}

4. 然后我们对上传的文件做一些限制,同时编写main方法来启动这个web :

?
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
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.enableautoconfiguration;
import org.springframework.boot.context.embedded.multipartconfigfactory;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;
 
import javax.servlet.multipartconfigelement;
 
@configuration
@componentscan
@enableautoconfiguration
public class application {
 
 @bean
 public multipartconfigelement multipartconfigelement() {
  multipartconfigfactory factory = new multipartconfigfactory();
  factory.setmaxfilesize("128kb");
  factory.setmaxrequestsize("128kb");
  return factory.createmultipartconfig();
 }
 
 public static void main(string[] args) {
  springapplication.run(application.class, args);
 }
}

5. 然后访问http://localhost:8080/upload就可以看到页面了。

上面的例子是实现的是单个文件上传的功能,假定我们现在要实现文件批量上传的功能的话,我们只需要简单的修改一下上面的代码就行,考虑到篇幅的问题,下面只是贴出和上面不同的代码,没有贴出的说明和上面一样。:

1.新增batchupload.jsp文件

?
1
2
3
4
5
6
7
8
9
10
<html>
<body>
<form method="post" enctype="multipart/form-data"
  action="/batch/upload">
 file to upload: <input type="file" name="file"><br />
 file to upload: <input type="file" name="file"><br />
 <input type="submit" value="upload"> press here to upload the file!
</form>
</body>
</html>

2. 新增batchfileuploadcontroller.java文件:

?
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
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.responsebody;
import org.springframework.web.multipart.multipartfile;
import org.springframework.web.multipart.multiparthttpservletrequest;
 
import javax.servlet.http.httpservletrequest;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileoutputstream;
import java.util.list;
 
/**
 * created by wenchao.ren on 2014/4/26.
 */
 
@controller
public class batchfileuploadcontroller {
 
 @requestmapping(value="/batch/upload", method= requestmethod.post)
 public @responsebody
 string handlefileupload(httpservletrequest request){
  list<multipartfile> files = ((multiparthttpservletrequest)request).getfiles("file");
  for (int i =0; i< files.size(); ++i) {
   multipartfile file = files.get(i);
   string name = file.getname();
   if (!file.isempty()) {
    try {
     byte[] bytes = file.getbytes();
     bufferedoutputstream stream =
       new bufferedoutputstream(new fileoutputstream(new file(name + i)));
     stream.write(bytes);
     stream.close();
    } catch (exception e) {
     return "you failed to upload " + name + " => " + e.getmessage();
    }
   } else {
    return "you failed to upload " + name + " because the file was empty.";
   }
  }
  return "upload successful";
 }
}

这样一个简单的批量上传文件的功能就ok了,是不是很简单啊。

注意:上面的代码只是为了演示而已,所以编码风格上采取了随性的方式,不建议大家模仿。

参考资料:multipartresolver实现文件上传功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

1. multipartresolver也可以实现文件上传功能。参考文章:原文链接:https://blog.csdn.net/woshizhangliang999/article/details/46711747

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部