SpringBoot入门系列篇(十一):实现文件上传

前情提要
现在大多数的web开发基本都会用到文件上传这一个功能,文件上传分为单文件上传和多文件上传,下面就一一讲解一下通过SpringBoot框架对两种上传的实现

SpringBoot实现单文件上传
首先建立一个html界面,包含一个form文件上传表单,代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
    <title>文件上传界面</title>
</head>
<body>
    <form enctype="multipart/form-data" method="post" action="/upload">
        选择文件:<input type="file" name="files" />
        <br />
        <button type="submit">上传</button>
        <span>&nbsp;&nbsp;</span>
        <button type="reset">重选</button>
    </form>
</body>
</html>
然后编写/pload的请求处理控制类,实现代码如下,在代码中进行详细注释:
package org.framework.demo.section1;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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 javax.servlet.http.HttpServletResponse;
import java.awt.image.MultiPixelPackedSampleModel;
import java.io.*;
import java.util.List;

/**
 * SpringBoot进行单文件上传
 * @author chengxi
 */
@Controller
public class FileUploadController {

    @RequestMapping("/upload")
    //将返回的结果直接写入到响应输出流中用于返回个客户端,而不会返回视图
    @ResponseBody
    public String upload(HttpServletResponse resp,
                         @RequestParam("file")MultipartFile file) throws IOException {

        resp.setCharacterEncoding("utf-8");
        if(file.isEmpty()){
            return "file is null文件为空";
        }

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(
                new File(file.getOriginalFilename())));
        bos.write(file.getBytes());
        bos.flush();
        bos.close();
        return "文件上传成功 file upload success";
    }
}
然后编写启动类,启动类的代码很简单,仅仅一个SpringApplication.run即可,这里就不贴出来了,自己进行测试即可

SpringBoot实现多文件上传
多文件上传相比单文件上传来说要麻烦一点,但是其实原理也就是进行遍历多个单文件上传,其处理方法代码如下:
package org.framework.demo.section1;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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 javax.servlet.http.HttpServletResponse;
import java.awt.image.MultiPixelPackedSampleModel;
import java.io.*;
import java.util.List;

/**
 * SpringBoot进行单文件上传
 * @author chengxi
 */
@Controller
public class FileUploadController {
    //多文件上传
    @RequestMapping("/uploads")
    @ResponseBody
    public String uploads(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        resp.setCharacterEncoding("utf-8");
        List<MultipartFile> files = ((MultipartHttpServletRequest)req).getFiles("files");

        if(files.size() < 1)
            return "没选中任何上传文件";

        for(MultipartFile file: files){
            byte[] bytes = file.getBytes();
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(
                    new File(file.getOriginalFilename())));
            out.write(bytes);
            out.flush();
            out.close();
        }
        return "文件批量上传成功";
    }
}
文件上传的一些属性配置
文件默认上传的大小上限是10MB,也可以通过启动类对文件上传的一些属性进行相应的自定义配置,代码如下:
/**
     * 进行文件上传的相应配置
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement(){

        MultipartConfigFactory factory = new MultipartConfigFactory();
        //设置上传的文件大小上限,如果超出限制,就会抛出异常信息
        factory.setMaxFileSize("128KB");
        //设置一次总上传数据的大小,用于多文件上传设置
        factory.setMaxRequestSize("256KB");
        return factory.createMultipartConfig();
    }
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值