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 ...
随机推荐
- [Luogu] 产生数
题面:https://www.luogu.org/problemnew/show/P1037 题解:https://www.zybuluo.com/wsndy-xx/note/1145473
- qtableview 表格风格设置
1.窗体无边框? tableView->setFrameShape(QFrame::NoFrame); 2.表格内容无边框? tableView->setShowGrid(false); ...
- 数据结构实验之二叉树四:(先序中序)还原二叉树 (SDUT 3343)
#include <bits/stdc++.h> using namespace std; struct node { char data; struct node *lc, *rc; } ...
- C语言学习笔记9-指针
1.指针基础 NULL为预处理器变量,是从C继承下来的,该变量在cstdlib头文件中定义 2.指针函数与函数指针 3.指针数组与数组指针 4.
- shell 统计行数,单词个数,字符个数
如果我们想知道1.txt中有多少行,多少个单词,多少个字符.我们可以使用wc命令.选项与参数-l:今列出行-w:今列出多少字(英文单词)-m:多少字符[zhang@localhost ~]$ cat ...
- java spring boot- freemarker 配置 yml使用流程
1.pom.xml 加入maven 依赖 <!-- 引入 freemarker 模板依赖 --><dependency> <groupId>org.springf ...
- hive 常用参数
hive.exec.max.created.files •说明:所有hive运行的map与reduce任务可以产生的文件的和 •默认值:100000 hive.exec.dynamic.partit ...
- instr动态模糊查询
String sqlSearchtext = ""; if(!"".equals(model.getXzqhdm())&&model.getXz ...
- django中安装pillow ValueError: zlib is required unless explicitly disabled using --disable-zlib, aborting
在windows系统上,使用 pip install pillow安装pillow时 报错 在使用 easy_install Pillow 方式安装成功,默认是最高版本 如果需要在安装时,指定安装版 ...
- [Phoenix] Mix 命令
mix phx.gen.html 命令生成模板: # 其中 name 和 age 是 schema 字段名称,后面跟的是类型 # 下面这样的写法,会生成 controller 和 service 层的 ...