新手入门------SpringBoot文件上传实现

       本次的内容是实现文件上传,小案例要达到的效果是在浏览器选择好文件,然后上传,最后返回结果(失败或成功),分为单文件和多文件上传。


一.案例准备

       首先还是进行springboot项目的创建,这个就不进行演示了。

       本次需要添加的依赖是spring web 和thymeleaf。如果已经有项目但是没有选择这两个依赖,可以点击pom.xml,在中手动添加如下依赖:

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

本次项目结构如下:

新手入门------SpringBoot文件上传实现_第1张图片

新手入门------SpringBoot文件上传实现_第2张图片

没有templates文件夹的自行创建文件夹即可

二.案例实现

       2.1单文件上传

       分析:首先我们需要的是一个上传文件的界面,输入访问路径后要展示页面,然后选择好文件,点击上传之后就会跳入新页面并且执行文件上传,最后将处理的结果显示在页面。

html页面名称为“uploadfile.html”,内容如下:




    
    
    

    
    文件上传


这里面要注意的是methodenctype文件上传的方法必须为postenctype也是固定为“multipart/form-data”。action为提交后要访问的url地址,与你自己在controller的定义有关。

       在controller层我们可以定义一个Map来处理信息,如下:

 // 处理上传文件结果
        private Map dealResultMap(boolean res, String msg)
        {
            Map result = new HashMap();
            result.put("success", res);
            result.put("msg", msg);
            return result;
        }

将文件上传结果传入后返回结果。

接下来就是确定访问的页面,如下图:

新手入门------SpringBoot文件上传实现_第3张图片

现在运行后就可以访问到文件上传页面了:

新手入门------SpringBoot文件上传实现_第4张图片

  

然后就是上传功能的实现,如下图:

新手入门------SpringBoot文件上传实现_第5张图片

       文件的名字就按照当前时间来命名,定义格式为“yyyyMMddHHmmssSSSS”(年月日时分秒)。

       获取文件后缀时调用了substring()来截取,先获取文件的原名称,然后找到最后一个 “  .  ”根据这个来截取文件的后缀。

       文件路径则是自己直接定义,可以自行先创建文件夹,也可以通过if语句来判断是否存在,如果不存在调用makirs()来创建文件夹(如上图)。

       文件的上传调用了FileCopyUtils.copy(),该方法的作用是将给定输入文件的内容复制到给定输出文件。因此我们需要传入的是上传的文件和要上传到的地方,如上图。最后结果返回我们之前定义的方法。

运行结果如下:

新手入门------SpringBoot文件上传实现_第6张图片

新手入门------SpringBoot文件上传实现_第7张图片

新手入门------SpringBoot文件上传实现_第8张图片

新手入门------SpringBoot文件上传实现_第9张图片

可以看到已经上传成功了。

       2.2多文件上传

       多文件上传与单文件上传没有太多变化,只是由单个文件变成了多个,因此我们传入的是一个集合,通过遍历来达到所有都上传的目的。

新手入门------SpringBoot文件上传实现_第10张图片

html页面代码如下:




    
    
    

    
    文件上传


运行结果如下:

新手入门------SpringBoot文件上传实现_第11张图片

新手入门------SpringBoot文件上传实现_第12张图片

三.controller层代码

       多文件上传代码如下,单文件自行更改即可。 

import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class FileController {

        @GetMapping("/upload/page")
        public String uploadPage() {
            return "uploadfile";
        }
        @PostMapping("/upload/multipart")
        @ResponseBody
        public Map upload(@RequestParam("photos") MultipartFile[] photos){

            String path = "d:/uploaded/";//保存路径
            for(MultipartFile photo:photos){
                if(!photo.isEmpty()){
                    String filename = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());

                    String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf("."));
                    if (!suffix.equals(".jpg")) {
                        return dealResultMap(false, "上传失败");
                    }

                    try {
                        File file = new File(path);
                        if(!file.exists()){
                            file.mkdirs();
                        }
                        FileCopyUtils.copy(photo.getInputStream(), new FileOutputStream(path + filename + suffix));
                    } catch (IOException e) {

                        e.printStackTrace();
                        return dealResultMap(false, "上传失败");
                    }
                }
            }
            return dealResultMap(true, "上传成功");
        }
        // 处理上传文件结果
        private Map dealResultMap(boolean res, String msg)
        {
            Map result = new HashMap();
            result.put("success", res);
            result.put("msg", msg);
            return result;
        }
    }

       

你可能感兴趣的:(入门SpringBoot,spring,boot,java,idea)