栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring boot实现文件上传实例(多文件上传)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring boot实现文件上传实例(多文件上传)

文件上传主要分以下几个步骤:

(1)新建maven java project;

(2)在pom.xml加入相应依赖;

(3)新建一个表单页面(这里使用thymleaf);

(4)编写controller;

(5)测试;

(6)对上传的文件做一些限制;

(7)多文件上传实现

(1)新建maven Java project

新建一个名称为spring-boot-fileupload maven Java项目;

(2)在pom.xml加入相应依赖;

加入相应的maven依赖,具体看以下解释:

 
 4.0.0 
 com.example 
 spring-boot-fileupload 
 0.0.1-SNAPSHOT 
  
  
   
    org.springframework.boot 
    spring-boot-starter-parent 
    1.4.0.RELEASE 
   
 
  
     
     
      org.springframework.boot 
      spring-boot-starter-web 
     
 
     
     
     org.springframework.boot 
     spring-boot-starter-thymeleaf 
     
  
 
   
     
       
       
maven-compiler-plugin 
 
  1.7 
  1.7 
 
       
     
   
 

(3)新建一个表单页面(这里使用thymleaf)

在src/main/resouces新建templates,在templates下新建一个file.html:

 
 
 
Hello World! 
 
 
   
 
 

(4)编写controller;

编写controller进行测试,这里主要实现两个方法:其一就是提供访问的/file路径;其二就是提供post上传的/upload方法,具体看代码实现:

@Controller 
public class FileUploadController { 
  // 访问路径为:http://127.0.0.1:8080/file 
  @RequestMapping("/file") 
  public String file() { 
    return "/file"; 
  } 
 
   
  @RequestMapping("/upload") 
  @ResponseBody 
  public String handleFileUpload(@RequestParam("file") MultipartFile file) { 
    if (!file.isEmpty()) { 
      try { 
  
 
 BufferedOutputStream out = new BufferedOutputStream( 
     new FileOutputStream(new File( 
  file.getOriginalFilename()))); 
 out.write(file.getBytes()); 
 out.flush(); 
 out.close(); 
 
      } catch (FileNotFoundException e) { 
 e.printStackTrace(); 
 return "上传失败," + e.getMessage(); 
      } catch (IOException e) { 
 e.printStackTrace(); 
 return "上传失败," + e.getMessage(); 
      } 
 
      return "上传成功"; 
 
    } else { 
      return "上传失败,因为文件是空的."; 
    } 
  } 
   
  // 访问路径为:http://127.0.0.1:8080/file 
    @RequestMapping("/mutifile") 
    public String mutifile() { 
      return "/mutifile"; 
    } 

(5)编写Application.java然后测试

Application没什么代码,就是Spring Boot的启动配置,具体如下:

package com.example.controller; 
 
import javax.servlet.MultipartConfigElement; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
//import org.springframework.boot.context.embedded.MultipartConfigFactory; 
import org.springframework.boot.web.servlet.MultipartConfigFactory; 
import org.springframework.context.annotation.Bean; 
 
@SpringBootApplication 
public class Application { 
  public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
  } 
   
} 

然后你就可以访问:http://127.0.0.1:8080/file 进行测试了,文件上传的路径是在工程的跟路径下,请刷新查看,其它的请查看代码中的注释进行自行思考

(6)对上传的文件做一些限制; 

对文件做一些限制是有必要的,在Application.java进行编码配置:。 

@Bean  
  public MultipartConfigElement multipartConfigElement() {  
      MultipartConfigFactory factory = new MultipartConfigFactory(); 
      //// 设置文件大小限制 ,超了,页面会抛出异常信息,这时候就需要进行异常信息的处理了; 
      factory.setMaxFileSize("128KB"); //KB,MB 
      /// 设置总上传数据总大小 
      factory.setMaxRequestSize("256KB");  
      //Sets the directory location where files will be stored. 
      //factory.setLocation("路径地址"); 
      return factory.createMultipartConfig();  
  } 

(7)多文件上传实现

多文件对于前段页面比较简单,具体代码实现:

在src/resouces/templates/mutifile.html

 
 
   
    Hello World! 
   
   
     
   
  

FileUploadController中新增两个方法:

// 访问路径为:http://127.0.0.1:8080/mutifile 
    @RequestMapping("/mutifile") 
    public String mutifile() { 
      return "/mutifile"; 
    } 
 
   
 
  @RequestMapping(value = "/batch/upload", method = RequestMethod.POST) 
  @ResponseBody 
  public String handleFileUpload(HttpServletRequest request) { 
    List files = ((MultipartHttpServletRequest) request) 
 .getFiles("file"); 
    MultipartFile file = null; 
    BufferedOutputStream stream = null; 
    for (int i = 0; i < files.size(); ++i) { 
      file = files.get(i); 
      if (!file.isEmpty()) { 
 try { 
   byte[] bytes = file.getBytes(); 
   stream = new BufferedOutputStream(new FileOutputStream( 
new File(file.getOriginalFilename()))); 
   stream.write(bytes); 
   stream.close(); 
 
 } catch (Exception e) { 
   stream = null; 
   return "You failed to upload " + i + " => " 
+ e.getMessage(); 
 
 } 
      } else { 
 return "You failed to upload " + i 
     + " because the file was empty."; 
      } 
 
    } 
    return "upload successful"; 
 
  } 

访问路径:http://127.0.0.1:8080/mutifile 进行测试。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/146364.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号