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 ...
随机推荐
- ES6 import、export的写法
大家都知道来到ES6版本,ES就原生支持JS Module的概念. import和export的写有哪些呢,我们看看 import: import from 和 var 变量一样,也会存在提升,这意味 ...
- 7.27T2
不可做题 sol:首先有个很显然的性质就是答案一定是在叶子上最优,然后画画图发现就是从最底层看,如果一条链就看做一个点,向上的第一颗非链的节点,它的儿子数-1就会对答案贡献,所有这样的累加起来就是答案 ...
- POI的XWPFTable的方法总结
1. void addNewCol(): 为该表中的每一行添加一个新列 2. void addRow(XWPFTableRow row): 向表中添加新行 3. boolean addRow(XWP ...
- ubuntu hadoop安装
参考: https://wangchangchung.github.io/2017/09/28/Ubuntu-16-04%E4%B8%8A%E5%AE%89%E8%A3%85Hadoop%E5%B9% ...
- 爬虫之代理和cookie的处理
代理操作 代理的目的 为解决ip被封的情况 什么是代理 代理服务器:fiddler 为什么使用代理可以改变请求的ip 本机的请求会先发送给代理服务器,代理服务器会接受本机发送过来的请求(当前请求对应的 ...
- .NET笔记题库(一)
1 (1)面向对象的语言具有__继承性_性._封装性_性._多态性 性. (2)能用foreach遍历访问的对象需要实现 _ IEnumerable 接口或声明_ GetEnumerator 方法的类 ...
- oracle启动过程2
5个目标点(知识点)环境说明,连接实例,hash运算dbs目录文件解释参数文件解释启动过程三阶段实战演练 本次课程目标是讲解oracle实例的启动过程首先了解一下本次实验环境 之前已经创建好了一 ...
- kubectl 之 patch 命令
patch命令 kubectl patch — Update field(s) of a resource using strategic merge patch Synopsis kubectl p ...
- 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_17.RabbitMQ研究-与springboot整合-消费者代码
创建消费者的类 使用@Component把这个类标记成一个Bean 把生产者里面创建的配置文件类复制过来 在原始的消费的方法上面 ,我们是可以拿到channel通道的 message.getBody就 ...
- RabbitMQ运转流程
生产者发送消息的过程 生产者连接到RabbitMQ Broker(相当于是一个RabbitMQ服务器),建立一个连接(Connection),开启一个信道(Channel). 生产者声明一个交换器(E ...