博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot2 上传文件 上传多文件
阅读量:4358 次
发布时间:2019-06-07

本文共 2900 字,大约阅读时间需要 9 分钟。

项目结构:

 

 

1、单文件上传

upload.html

    
upload file

http://localhost:8080/uploadfile/upload

package com.archibladwitwicke.springboot2.chapter03.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import java.io.File;@Controller@RequestMapping("/uploadfile")public class UploadFileController {    @RequestMapping("/upload")    public String upload() {        return "/upload.html";    }    @PostMapping("/upload")    @ResponseBody    public String upload(MultipartFile file) {        if (file.isEmpty()) {            return "file is null";        }        String originFileName = file.getOriginalFilename();        String destFileLocation = "C:\\Users\\Administrator\\Desktop\\" + originFileName;        File destFile = new File(destFileLocation);        try {            file.transferTo(destFile);            return "upload ok! upload file location: " + destFileLocation;        } catch (Exception ex) {            return "upload false! reason: " + ex.getMessage();        }    }    @RequestMapping("/uploads")    public String uploads() {        return "/multiupload.html";    }}

2、多文件上传

multiupload.html

    
upload file

http://localhost:8080/uploadfile/uploads

package com.archibladwitwicke.springboot2.chapter03.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;import java.io.File;@Controller@RequestMapping("/uploadfile")public class UploadFileController {    @RequestMapping("/uploads")    public String uploads() {        return "/multiupload.html";    }    @PostMapping("/uploads")    @ResponseBody    public String uploads(MultipartFile[] files) {        String result = null;        if (files.length == 0) {            return "file is null";        }        for (MultipartFile file : files) {            String originFileName = file.getOriginalFilename();            String destFileLocation = "C:\\Users\\Administrator\\Desktop\\" + originFileName;            File destFile = new File(destFileLocation);            try {                file.transferTo(destFile);                result += "upload ok! upload file location: " + destFileLocation;            } catch (Exception ex) {                result = "upload false! reason: " + ex.getMessage();            }        }        return result;    }}

 

转载于:https://www.cnblogs.com/hfultrastrong/p/8583090.html

你可能感兴趣的文章
数据字典的转换
查看>>
二维数组按照指定的字段排序的函数
查看>>
在IAR下通过Jlink将程序直接下载到Flash指定地址
查看>>
POJ2560-雀斑(Freckles)【图论,并查集,最小生成树,KURUSKAL】
查看>>
[Angular] Tree shakable provider
查看>>
[Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript
查看>>
[Angular 2] Select From Multiple Nested Angular 2 Elements
查看>>
C# 中的委托和事件[转帖]
查看>>
图的遍历(bfs+dfs)模板
查看>>
angular service 进行组件通信
查看>>
linux安装Mac的默认Monaco字体
查看>>
java语言的特点
查看>>
关于动态添加iview admin路由以及刷新侧边栏
查看>>
ApplicationInsights的探测器尝鲜
查看>>
java 解析Json格式数据
查看>>
unix中的线程池技术详解
查看>>
CSS简介
查看>>
常用三大软件评价1
查看>>
MVC各层介绍使用---初步理解
查看>>
单例对象的创建与销毁
查看>>