springboot简易上传下载
2024-09-07 00:21:33
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();
}
}
最新文章
- Array的个人总结
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q133-Q135)
- canvas转盘抽奖
- Ioc容器Autofac系列(1)-- 初窥
- Windows Server Backup 2008 R2 备份Hyper-V
- 让别人也可以访问你电脑上的ASP.NET MVC创建的网站
- Discuz资料整理
- C#解leetcode 152. Maximum Product Subarray
- 笔记:java并发实践2
- 2014第6周五JS调试
- Why stackedit
- HDU<;1372>;/bfs
- java开发3轮技术面+hr面 面经(MT)
- js发送post请求,实现下载文件
- [Swift]LeetCode636. 函数的独占时间 | Exclusive Time of Functions
- sql 2012 用户sa登陆不上
- C goto
- 数据库——MongoDB的安装
- qemu 对虚机的地址空间管理
- 理解 DocumentFragment
热门文章
- Codevs 1242 布局 2005年USACO(差分约束)
- P1968 美元汇率 怀疑智商超过海平面
- k-means和iosdata聚类算法在生活案例中的运用
- U盘exFAT格式转NTFS
- 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_3.RabbitMQ研究-工作原理
- 一百三十八:CMS系统之发布帖子前端js
- MyISAM与InnoDB之间的区别
- 建立第一个Django工程---linux中的python
- docker attach 和 exec 用法区别
- golang web框架设计4:日志设计