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

Spring实现文件上传功能

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

Spring实现文件上传功能

本篇文章,我们要来做一个Spring的文件上传功能:

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



  org.springframework.boot

  spring-boot-starter-web

  1.0.2.RELEASE

 

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









 

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

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

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 :

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文件









 

2. 新增BatchFileUploadController.java文件:

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;

 



 

@Controller

public class BatchFileUploadController {

 

  @RequestMapping(value="/batch/upload", method= RequestMethod.POST)

  public @ResponseBody

  String handleFileUpload(HttpServletRequest request){

    List 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了,是不是很简单啊。 

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

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

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

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

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