SpringBoot 上传、下载(四)
工程目录结构

完整代码:
1、pom.xml
首先当然是添加依赖,用到thymeleaf模板渲染html页面
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion> <groupId>com.hello</groupId>
<artifactId>HelloBoot</artifactId>
<version>0.0.-SNAPSHOT</version>
<packaging>jar</packaging> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
</properties> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0..BUILD-SNAPSHOT</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies> <repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories> </project>
如果遇到<parent>标签报错,看看和上面写的有出入吗---
2、application.properties
只有一行,关闭thymeleaf缓存
spring.thymeleaf.cache=false
3、file.html 页面
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body> <h1 th:inlines="text">文件上传</h1>
<form action="fileUpload" method="post" enctype="multipart/form-data">
<p><input type="file" name="filename" /></p>
<p><input type="submit" value="提交" /></p> </form>
<a href="http://localhost:8080/fileDownload">下载</a> </body>
</html>
4、HelloController.java 控制类
package com.hello; import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; @Controller
public class HelloController { //映射视图
@RequestMapping("/file")
public String file() {
return "/file";
} //上传
@RequestMapping("fileUpload")
@ResponseBody
public String fileUpload(@RequestParam("filename") MultipartFile file) {
if(file.isEmpty()) {
return "false";
}
//将里面内容移动到目标文本文档
File dest = new File("D:/test.txt");
try {
file.transferTo(dest);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
//下载
@RequestMapping("fileDownload")
@ResponseBody
public String fileDownload(HttpServletResponse res) {
String fileName = "1.jpg";
res.setHeader("content-type", "application/octet-stream");
res.setContentType("application/octet-stream");
res.setHeader("Content-Disposition", "attachment;filename=" + fileName); byte[] buff = new byte[];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = res.getOutputStream();
//获取zhi'ding
bis = new BufferedInputStream(
new FileInputStream(new File("D:/1.jpg")));
int i = bis.read(buff);
while(i != -) {
//每次读取1024字节,就flush输出
os.write(buff, , buff.length);
os.flush();
//然后继续读取下一个1024字节
i = bis.read(buff);
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "success";
} }
(1)file()方法return返回的是视图“file.html”,使用@Controller修饰类即可
(2)上传业务方法fileUpload(),return返回的是个字符串——“false“或”success”,不是html视图页面,所以不用渲染视图,
这里使用@Controller修饰类 和 @ResponseBody修饰方法
5、启动类App.java
package com.hello; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App { public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
SpringBoot 上传、下载(四)的更多相关文章
- springboot上传下载文件
在yml配置相关内容 spring: # mvc: throw-exception-if-no-handler-found: true #静态资源 static-path-pattern: /** r ...
- Spring Boot2(十四):单文件上传/下载,文件批量上传
文件上传和下载在项目中经常用到,这里主要学习SpringBoot完成单个文件上传/下载,批量文件上传的场景应用.结合mysql数据库.jpa数据层操作.thymeleaf页面模板. 一.准备 添加ma ...
- SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)
SpringBoot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,SpringBoot致力于在蓬勃发 ...
- springboot整合vue实现上传下载文件
https://blog.csdn.net/yhhyhhyhhyhh/article/details/89888953 文章目录 springboot整合vue实现上传下载文件 1上传下载文件api文 ...
- springboot简易上传下载
1.导入上传下载依赖: <dependency> <groupId>commons-fileupload</groupId> <artifactId>c ...
- 仵航说 前后端分离,文件上传下载(springBoot+vue+elementUI)仵老大
1.介绍 本文主要是介绍前后端分离的上传下载,后端使用的是SpringBoot,持久层用的是mybatis-plus,前端用的Vue,UI用的elementUI,测试了一下,文本,图片,excel ...
- salesforce 零基础学习(四十二)简单文件上传下载
项目中,常常需要用到文件的上传和下载,上传和下载功能实际上是对Document对象进行insert和查询操作.本篇演示简单的文件上传和下载,理论上文件上传后应该将ID作为操作表的字段存储,这里只演示文 ...
- Spring MVC 使用介绍(十四)文件上传下载
一.概述 文件上传时,http请求头Content-Type须为multipart/form-data,有两种实现方式: 1.基于FormData对象,该方式简单灵活 2.基于<form> ...
- Gin-Go学习笔记四:Gin-Web框架 文件的上传下载
文件的上传和下载 1->文件的上传 文件的上传,采用的是uploadify.js这个插件. 本事例实现的是上传图片文件,其他的文件上传也一样. 2->文件的下载 文件的下载有两个实现的方式 ...
- SpringBoot的文件上传&下载
前言:不多BB直接上代码 文件上传 pom依赖添加commons-io <!-- 上传/下载jar https://mvnrepository.com/artifact/commons-io/c ...
随机推荐
- 类的调用1(被调用的MyFirstJava)
package com.mec.MyFirstJavaLife.text; public class MyFirstJava { /** * @param args */ private in ...
- 大数据 - spark-sql 常用命令
--spark启动 spark-sql --退出 spark-sql> quit; --退出spark-sql or spark-sql> exit; 1.查看已有的database sh ...
- vue-cli webpack打包不.map文件,iview 项目打包完,图标路径有问题
vue 项目打包出来有时候体积有点大,其实基本都是.map文件比较大,这些文件对项目没什么影响,可以直接在打包时候就不生成.map文件这样就不用每次删那么麻烦了, 做法: config中知道 inde ...
- 用 EasyUEFI 在 Win8/10 中硬盘安装 Ubuntu16.04图文教程
用 EasyUEFI 在 Win8/10 中硬盘安装 Ubuntu 作者:TeliuTe 来源:基础教程网 1.准备Ubuntu安装文件 1)下载带amd的64位 Ubuntu 桌面版光盘镜像文件,如 ...
- 判断一个点在多边形的内部C++
/* 原理: 将测试点的Y坐标与多边形的每一个点进行比较, ** 会得到测试点所在的行与多边形边的所有交点. ** 如果测试点的两边点的个数都是奇数个, ** 则该测试点在多边形内,否则在多边形外. ...
- 基于BindingSource的WinForm开发
BindingSource控件介绍 BindingSource控件介绍 BindingSource控件是.NET Framework 2.0提供的新控件之一.BindingSource控件与数据源建立 ...
- java.——最大子序列和(前提是:全部都是非负数)
直接上代码吧: 情况一:全部是非负数整数的时候,其实非负实数处理也一样. package Person; import java.util.Scanner; public class Main{ ...
- Hadoop – The Definitive Guide Examples,,IntelliJ
IntelliJ Project for Building Hadoop – The Definitive Guide Examples http://vichargrave.com/intellij ...
- linux+vs2013编译静态库和动态库
Linux下创建与使用静态库 Linux静态库命名规则 Linux静态库命名规范,必须是"lib[your_library_name].a":lib为前缀,中间是静态库名,扩展名为 ...
- spring boot 多数据源配置与使用
在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.properties文 ...