springboot简易上传下载
1.导入上传下载依赖:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency> <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- 添加thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.上传:
1)前端页面:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function exproExcel() { }
</script>
</head>
<body>
<h1>Spring Boot</h1>
<a href="/Expro/excel">导出</a>
<p th:text="${hello}"></p>
<p>文件上传</p>
<form action="/upload/file" method="post" enctype="multipart/form-data">
上传:<input type="file" name="upfile"/>
<button type="submit">提交</button>
</form> </body>
</html>
2)编写跳到上传页面接口:
@Controller
public class HelloController { @RequestMapping("/hello")
public String helloIndex(HashMap<String, Object> map){ map.put("hello","Hello SpringBoot!");
return "/index";
}
}
3)编写接收上传文件接口:
@Controller
@RequestMapping("/upload")
public class UploadFileController { @RequestMapping(value = "/file")
public @ResponseBody String uploadFile(@RequestParam("upfile") MultipartFile file, HttpServletRequest request){ String message =""; try { if(!file.isEmpty()){ FileOutputStream outputStream = new FileOutputStream("F:\\XIAOYAO"+"\\"+file.getOriginalFilename()); outputStream.write(file.getBytes());
outputStream.flush();
outputStream.close(); message="上传成功!"; } } catch (UnsupportedEncodingException e) {
e.printStackTrace();
message="上传失败!";
} catch (IOException e) {
e.printStackTrace();
message="上传失败!";
} return message;
}
}
3.下载:
1)编写下载接口:
@RestController
@RequestMapping("/Expro")
public class ExprotExcelController { @RequestMapping("/excel")
public void exproExcel(HttpServletRequest request, HttpServletResponse response) throws Exception{ String path = ClassLoader.getSystemResource("").toURI().getPath(); //获取类加载地址 System.out.println(path); File file = new File(path+"excelTempalte/模板.xlsx");
FileInputStream fileInputStream = new FileInputStream(file); //读取文件 response.setHeader("Content-disposition", "attachment;filename=test.xlsx"); //设置响应头和文件名字
OutputStream outputStream = response.getOutputStream(); //创建缓存区
byte [] buffe = new byte[1024]; int len =0; while ((len =fileInputStream.read(buffe))>0){
outputStream.write(buffe,0,len);
} fileInputStream.close();
outputStream.flush();
outputStream.close();
}
}
springboot简易上传下载的更多相关文章
- SpringBoot实现上传下载(二)
这篇写下载. **1.实现思路** 上一篇的数据库设计中,我们有一个字段始终没有用到fileName,这是用来给Layer对象存储文件名的,以此来完成文件与对象的对应, 
FTP上传下载文件(函数简易版) # 服务端 import socket import json import hashlib import struct import os user_dic = { ...
- 仵航说 前后端分离,文件上传下载(springBoot+vue+elementUI)仵老大
1.介绍 本文主要是介绍前后端分离的上传下载,后端使用的是SpringBoot,持久层用的是mybatis-plus,前端用的Vue,UI用的elementUI,测试了一下,文本,图片,excel ...
随机推荐
- 2019.7.9 校内测试 T2 极值问题
这一次是交流测试?边交流边测试(滑稽 极值问题 乍一看这是一道数学题,因为1e9的数据让我暴力的心退却. 数学又不好,不会化简式子嘞,咋办? 不怕,咱会打表找规律.(考场上真的是打表找出了规律,打表打 ...
- Java 生成六位短信验证码
在<Java 生成三位随机数>中,简要介绍了使用Java生成三位随机数的方法,前几天在工作中遇到生成6位短信验证码的需求,验证码由6位随机数字构成,不包含字母.6位随机数通常用作短信验证码 ...
- Chisel-LLDB命令插件,让调试更Easy
http://blog.cnbluebox.com/blog/2015/03/05/chisel/ LLDB 是一个有着 REPL 的特性和 C++ ,Python 插件的开源调试器.LLDB 绑定在 ...
- JS模拟Touch事件
var ele = document.getElementsByClassName('target_node_class')[0] //may have x and y properties in s ...
- 解决python 保存json到文件时 中文显示16进制编码的问题
python 2.7 import codecs import json with codecs.open('Options.json', 'w', encoding='utf-8') as f: j ...
- tomcat配置使用log4j管理日志
从tomcat官网下载和tomcat对应的tomcat-juli.jar和tomcat-juli-adapters.jar,从log4j官网下载log4j的jar包(我用的是log4j-1.2.17. ...
- microsoft 官方学习资源
https://devblogs.microsoft.com/dotnet/ :_NET Blog https://docs.microsoft.com/zh-cn/learn/ :Microsof ...
- nginx使用场景
1. 对外开放本地封闭Server 本地server无法对外开放,nginx做反向代理,对外开发,使得外部可以访问封闭服务. upstream npm { server ; keepalive ; } ...
- VLC播放器web插件接口(Part2)
本文转自:http://www.educity.cn/wenda/124878.htmlVLC Activex控件(VideoLAN.VLCPlugin.1 VideoLAN.VLCPlugin.2) ...
- jquery mCustomScrollbar使用
$(".content .desc").mCustomScrollbar({ axis: "y", theme: 'dark', mouseWheel: { e ...