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 Multi-University Training Contest 10
目录 Contest Info Solutions C - Valentine's Day D - Play Games with Rounddog E - Welcome Party G - Clo ...
- Codeforces 1272 A-E
Codeforces 1272 A-E A Three Friends 直接枚举所有情况,共\(3\times 3\times 3=27\)种. code #include<bits/stdc+ ...
- kubernetes部署nginx/tomcat
kubernetes集群已经部署好了,需要的话可以参考之前的文章https://www.cnblogs.com/winter1519/p/10015420.html [root@master tomc ...
- 工作流调度系统Azkaban的简介和使用
1 概述 1.1 为什么需要工作流调度系统 l 一个完整的数据分析系统通常都是由大量任务单元组成: shell脚本程序,java程序,mapreduce程序.hive脚本等 l 各任务单元之间存在时间 ...
- Flutter设置Container的最大最小宽高
Flutter中设置Container宽高可直接通过width和height属性来设置:如下 Container( width: 100, height: 100, color: Colors.red ...
- (main)贝叶斯统计 | 贝叶斯定理 | 贝叶斯推断 | 贝叶斯线性回归 | Bayes' Theorem
2019年08月31日更新 看了一篇发在NM上的文章才又明白了贝叶斯方法的重要性和普适性,结合目前最火的DL,会有意想不到的结果. 目前一些最直觉性的理解: 概率的核心就是可能性空间一定,三体世界不会 ...
- 重学C语言
重学C语言 #include <stdio.h> int main() { ; ; printf("I am %d year old.\n",age); printf( ...
- 慎用array_filter函数
array_filter (PHP 4 >= 4.0.6, PHP 5, PHP 7) array_filter - 用回调函数过滤数组中的单元 说明 array array_filter ( ...
- Netty解码器相关文章
最通用TCP黏包解决方案:LengthFieldBasedFrameDecoder和LengthFieldPrepender https://blog.csdn.net/u010853261/arti ...
- Swift 析构过程
在一个类的实例被释放之前,析构函数被立即调用.用关键字deinit来标示析构函数,类似于初始化函数用init来标示.析构函数只适用于类类型. 析构过程原理 Swift 会自动释放不再需要的实例以释放资 ...