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简易上传下载的更多相关文章

  1. SpringBoot实现上传下载(二)

    这篇写下载. **1.实现思路** 上一篇的数据库设计中,我们有一个字段始终没有用到fileName,这是用来给Layer对象存储文件名的,以此来完成文件与对象的对应, ![image.png](ht ...

  2. springboot文件上传下载简单使用

    springboot的文件上传比较简单 一.使用默认的Resolver:StandardServletMultipartResolver controller package com.mydemo.w ...

  3. SpringBoot文件上传下载

    项目中经常会有上传和下载的需求,这篇文章简述一下springboot项目中实现简单的上传和下载. 新建springboot项目,前台页面使用的thymeleaf模板,其余的没有特别的配置,pom代码如 ...

  4. springboot 文件上传下载

    关键点: 1,使用 POST 请求2,consumes=MediaType.MULTIPART_FROM_DATA_VALUE3,@RequestParm 里面的字符串和前端 input 控件的 na ...

  5. springboot文件上传下载,转载的

    Spring Boot入门——文件上传与下载 原文来自:https://www.cnblogs.com/studyDetail/articles/7003253.html 1.在pom.xml文件中添 ...

  6. SpringBoot 文件上传、下载、设置大小

    本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...

  7. springboot整合vue实现上传下载文件

    https://blog.csdn.net/yhhyhhyhhyhh/article/details/89888953 文章目录 springboot整合vue实现上传下载文件 1上传下载文件api文 ...

  8. FTP上传下载文件(函数简易版)

    FTP上传下载文件(函数简易版) # 服务端 import socket import json import hashlib import struct import os user_dic = { ...

  9. 仵航说 前后端分离,文件上传下载(springBoot+vue+elementUI)仵老大

    1.介绍 ​ 本文主要是介绍前后端分离的上传下载,后端使用的是SpringBoot,持久层用的是mybatis-plus,前端用的Vue,UI用的elementUI,测试了一下,文本,图片,excel ...

随机推荐

  1. 数据分析之numpy使用

    使用numpy生成数字 生成的类型是ndarray类型 t1 = np.array([1,2,3,4,5]) print(t1,type(t1)) # 类型为ndarray t2 = np.array ...

  2. java spring boot- freemarker 配置 yml使用流程

    1.pom.xml  加入maven 依赖 <!-- 引入 freemarker 模板依赖 --><dependency> <groupId>org.springf ...

  3. G-LAB四月份作业-数据可视化问题探讨

    G-LAB四月份作业-数据可视化问题探讨 引子: 数据平台项目建设正在按照公司的计划开展执行中,作为平台建设项目参与者之一,感觉目前我们现有的MIS报表平台数据也不可谓不丰富,但是不论从提供给用户的数 ...

  4. JS获取div高度的方法

    有时在写页面时,需要获取一个div的高度.怎么才能获取呢?哈哈,先上结论.有两种方法. offsetHeight .clientHeight getComputedStyle offsetHeight ...

  5. An error occurred while starting a transaction on the provider connection. See the inner exception for details.

    用EntityFramework循环操作数据时,报了如下错误 An error occurred while starting a transaction on the provider connec ...

  6. PHP魔术方法和魔法变量详解

    一.魔术常量 __LINE__ 文件中的当前行号.__FILE__ 文件的完整路径和文件名.如果用在被包含文件中,则返回被包含的文件名. 自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对 ...

  7. 性能分析 | 线上CPU100%排查

    不知道在大家面试中,有没有遇到这个问题: 生产服务器上部署了几个java程序,突然出现了CPU100%的异常告警,你如何定位出问题呢? 这个问题分为两版回答! 高调版 对不起,我是做研发的,这个问题在 ...

  8. Linunx创建软连接、删除软连接、修改软连接

    创建: ln -s [目标目录] [软链接地址] ln -s /usr/local/python3/bin/python3 /usr/bin/python3ln -s /usr/local/pytho ...

  9. WPF 设置TextBox为空时,背景为文字提示。

    <TextBox FontSize="> <TextBox.Resources> <VisualBrush x:Key="HelpBrush" ...

  10. osg help

    #ifdef _WIN32#include <Windows.h>#endif // _WIN32 #include <osgViewer/Viewer>#include &l ...