单、多文件上传:单文件上传使用upload.html ,多文件上传使用uploads.html

创建一个Springboot application, POM 中加入 spring-boot-starter-web 依赖

upload.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head> <body> <form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile" value="请选择文件" /> <input
type="submit" value="上传" />
</form> </body>
</html>

uploads.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head> <body> <form action="uploads" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFiles" value="请选择文件" multiple /> <input
type="submit" value="上传" />
</form> </body>
</html>

创建Controller 用于处理提交之后的Aciton

package com.app;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; @RestController
public class FileUploadController { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/"); @PostMapping("/upload")
public String upload(MultipartFile uploadFile, HttpServletRequest req) {
String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
String format = sdf.format(new Date());
File folder = new File(realPath + format);
System.out.println(folder.getAbsolutePath());
if(!folder.isDirectory()) {
folder.mkdirs();
}
String oldName = uploadFile.getOriginalFilename();
System.out.println("oldName" + oldName);
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
System.out.println("new Name" + newName);
try {
uploadFile.transferTo(new File(folder,newName));
String filePath= req.getScheme() +"://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName; return filePath;
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
} return "上传失败!";
}
@PostMapping("/uploads")
public String upload(MultipartFile[] uploadFiles, HttpServletRequest req) {
String realPath = req.getSession().getServletContext().getRealPath("/uploadFiles/");
String format = sdf.format(new Date());
File folder = new File(realPath + format);
System.out.println(folder.getAbsolutePath());
if(!folder.isDirectory()) {
folder.mkdirs();
}
StringBuffer result = new StringBuffer();
for(MultipartFile uploadFile: uploadFiles) {
String oldName = uploadFile.getOriginalFilename();
System.out.println("oldName" + oldName);
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
System.out.println("new Name" + newName);
try {
uploadFile.transferTo(new File(folder,newName));
String filePath= req.getScheme() +"://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFiles/" + format + newName;
result.append(filePath);
result.append("\r\n"); } catch (IllegalStateException | IOException e) {
e.printStackTrace();
return "上传失败!";
}
} return result.toString(); } }

SpringBoot 单文件和多文件上传的更多相关文章

  1. c# 模拟表单提交,post form 上传文件、大数据内容

    表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipart/form-data,而且要设定一个 boundary 参数,这个参数是由应用程序自行产生,它会用来识别每 ...

  2. django 基于form表单上传文件和基于ajax上传文件

    一.基于form表单上传文件 1.html里是有一个input type="file" 和 ‘submit’的标签 2.vies.py def fileupload(request ...

  3. c# 模拟表单提交,post form 上传文件、数据内容

    转自:https://www.cnblogs.com/DoNetCShap/p/10696277.html 表单提交协议规定:要先将 HTTP 要求的 Content-Type 设为 multipar ...

  4. PHP之文件的锁定、上传与下载

    小结文件的锁定机制.上传和下载 1.文件锁定 现在都在讲究什么分布式.并发等,实际上文件的操作也是并发的,在网络环境下,多个用户在同一时刻访问页面,对同一服务器上的同一文件进行着读取,如果,这个用户刚 ...

  5. SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html

    SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html 说明: 1.环境:SpringMVC+Spring+Tomcat7+JDK1.7 2.支持 zip和rar格式的压 ...

  6. android中的文件(图片)上传

    android中的文件(图片)上传其实没什么复杂的,主要是对 multipart/form-data 协议要有所了解. 关于 multipart/form-data 协议,在 RFC文档中有详细的描述 ...

  7. 求大师点化,寻求大文件(最大20G左右)上传方案

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  8. JS代码实用代码实例(输入框监听,点击显示点击其他地方消失,文件本地预览上传)

    前段时间写前端,遇到一些模块非常有用,总结以备后用 一.input框字数监听 <!DOCTYPE html> <html lang="en"> <he ...

  9. [html5+java]文件异步读取及上传核心代码

    html5+java 文件异步读取及上传关键代码段 功能: 1.多文件文件拖拽上传,file input 多文件选择 2.html5 File Api 异步FormData,blob上传,图片显示 3 ...

  10. 使用swfupload上传超过30M文件,使用FLASH上传组件

    原文:使用swfupload上传超过30M文件,使用FLASH上传组件 前一段时间会员的上传组件改用FLASH的swfupload来上传,既能很友好的显示上传进度,又能完全满足大文件的上传. 后来服务 ...

随机推荐

  1. Hive 教程(六)-Hive Cli

    hive 有两种启动方式,一种是 bin/hive,一种是 hiveserver2, bin/hive 是 hive 的 shell 模式,所有任务在 shell 中完成,shell 就相当于 hiv ...

  2. 109、Secret的使用场景 (Swarm16)

    参考https://www.cnblogs.com/CloudMan6/p/8082429.html   我们可以用secret管理任何敏感数据.这些敏感数据是容器在运行时需要的.同时我们又不想把这些 ...

  3. ECharts模拟百度迁徙实例

    本实例原始信息: 作者:anix 演示地址:Echarts模拟迁徙 源码地址:GitHub-ananix-qianxi 前言 "百度地图春节人口迁徙大数据"(简称"百度迁 ...

  4. 禁止antd Input.Password浏览器自动回传

    设置autoComplete为new-password

  5. ZeroMQ 三种模式python3实现

    ZeroMQ是一个消息队列网络库,实现网络常用技术封装.在C/S中实现了三种模式,这段时间用python简单实现了一下,感觉python虽然灵活.但是数据处理不如C++自由灵活. Request-Re ...

  6. VIM简单操作

    ngg就跳转到第n行行首,G就到结尾 0光标移到当前行行首 $光标移到当前行行末 fx搜索当前行中下一个出现x的地方 yy复制当前行 nyy复制当前行到n-1行 dd删除当前行 ndd删除当前行到n- ...

  7. IO模型(epoll)--详解-03

    写在前面 epoll是开发linux高性能服务器的必备技术至,epoll本质,是服务端程序员的必须掌握的知识. 七.epoll的原理和流程 本节会以示例和图表来讲解epoll的原理和流程. 创建epo ...

  8. SQL练习02

    编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+| Id | Salary |+----+--------+| 1 | 100 || ...

  9. Mongodb的聚合和管道

    MongoDB 聚合 MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果. aggregate() 方法 MongoDB中聚合的方法使用agg ...

  10. idea 下获取路径下文件的方法

    想读取resouce/temp下的test.txt文件.方式一:File sourceFile = ResourceUtils.getFile("classpath:temp/test.tx ...