扣丁学堂java培训简述java中Spring boot实现文件上传功能

2018-06-28 09:25:01 496浏览

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





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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>

2.在webapp目录下的index.jsp文件中输入一个表单:
<formmethod="POST"enctype="multipart/form-data"action="/upload"> </body>

<html>
<body>
<formmethod="POST"enctype="multipart/form-data"action="/upload">
Filetoupload:<inputtype="file"name="file"><br/>
Name:<inputtype="text"name="name"><br/><br/>
<inputtype="submit"value="Upload">
Pressheretouploadthefile!
</form>
</body>
这个表单就是我们模拟的上传页面

3.编写处理这个表单的Controller:


importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileOutputStream;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.RequestParam;
importorg.springframework.web.bind.annotation.ResponseBody;
importorg.springframework.web.multipart.MultipartFile;
@Controller
publicclassFileUploadController{
@RequestMapping(value="/upload",method=RequestMethod.GET)
public@ResponseBodyStringprovideUploadInfo(){
return"YoucanuploadafilebypostingtothissameURL.";
}
@RequestMapping(value="/upload",method=RequestMethod.POST)
public@ResponseBodyStringhandleFileUpload(@RequestParam("name")Stringname,
@RequestParam("file")MultipartFilefile){
if(!file.isEmpty()){
try{
byte[]bytes=file.getBytes();
BufferedOutputStreamstream=
newBufferedOutputStream(newFileOutputStream(newFile(name+"-uploaded")));
stream.write(bytes);
stream.close();
return"Yousuccessfullyuploaded"+name+"into"+name+"-uploaded!";
}catch(Exceptione){
return"Youfailedtoupload"+name+"=>"+e.getMessage();
}
}else{
return"Youfailedtoupload"+name+"becausethefilewasempty.";
}
}
}


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

importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;
importorg.springframework.boot.context.embedded.MultiPartConfigFactory;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.context.annotation.Configuration;
importjavax.servlet.MultipartConfigElement;
@Configuration
@ComponentScan
@EnableAutoConfiguration
publicclassApplication{
@Bean
publicMultipartConfigElementmultipartConfigElement(){
MultiPartConfigFactoryfactory=newMultiPartConfigFactory();
factory.setMaxFileSize("128KB");
factory.setMaxRequestSize("128KB");
returnfactory.createMultipartConfig();
}
publicstaticvoidmain(String[]args){
SpringApplication.run(Application.class,args);
}
}

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

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

1.新增batchUpload.jsp文件
<html>
<body>
<formmethod="POST"enctype="multipart/form-data"
action="/batch/upload">
Filetoupload:<inputtype="file"name="file"><br/>
Filetoupload:<inputtype="file"name="file"><br/>
<inputtype="submit"value="Upload">Pressheretouploadthefile!
</form>
</body>
</html>


2.新增BatchFileUploadController.java文件:

importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.ResponseBody;
importorg.springframework.web.multipart.MultipartFile;
importorg.springframework.web.multipart.MultipartHttpServletRequest;
importjavax.servlet.http.HttpServletRequest;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.util.List;
/**
*Createdbywenchao.renon2014/4/26.
*/
@Controller
publicclassBatchFileUploadController{
@RequestMapping(value="/batch/upload",method=RequestMethod.POST)
public@ResponseBody
StringhandleFileUpload(HttpServletRequestrequest){
List<MultipartFile>files=((MultipartHttpServletRequest)request).getFiles("file");
for(inti=0;i<files.size();++i){
MultipartFilefile=files.get(i);
Stringname=file.getName();
if(!file.isEmpty()){
try{
byte[]bytes=file.getBytes();
BufferedOutputStreamstream=
newBufferedOutputStream(newFileOutputStream(newFile(name+i)));
stream.write(bytes);
stream.close();
}catch(Exceptione){
return"Youfailedtoupload"+name+"=>"+e.getMessage();
}
}else{
return"Youfailedtoupload"+name+"becausethefilewasempty.";
}
}
return"uploadsuccessful";
}
}


这样一个简单的批量上传文件的功能就ok了,是不是很简单啊。以上就是扣丁学堂为大家带来的java中Spring boot实现文件上传功能,如果你对java感兴趣的话,请登录扣丁学堂官网,或者关注微信公众号,大量java在线视频教程等你来观看!

扣丁学堂微信公众号

【关注微信公众号获取更多学习资料】




查看更多关于“Java开发资讯的相关文章>>


标签: java java培训 java在线视频教程 java编程 java语言

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号