工程目录结构

完整代码:

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 上传、下载(四)的更多相关文章

  1. springboot上传下载文件

    在yml配置相关内容 spring: # mvc: throw-exception-if-no-handler-found: true #静态资源 static-path-pattern: /** r ...

  2. Spring Boot2(十四):单文件上传/下载,文件批量上传

    文件上传和下载在项目中经常用到,这里主要学习SpringBoot完成单个文件上传/下载,批量文件上传的场景应用.结合mysql数据库.jpa数据层操作.thymeleaf页面模板. 一.准备 添加ma ...

  3. SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)

    SpringBoot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,SpringBoot致力于在蓬勃发 ...

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

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

  5. springboot简易上传下载

    1.导入上传下载依赖: <dependency> <groupId>commons-fileupload</groupId> <artifactId>c ...

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

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

  7. salesforce 零基础学习(四十二)简单文件上传下载

    项目中,常常需要用到文件的上传和下载,上传和下载功能实际上是对Document对象进行insert和查询操作.本篇演示简单的文件上传和下载,理论上文件上传后应该将ID作为操作表的字段存储,这里只演示文 ...

  8. Spring MVC 使用介绍(十四)文件上传下载

    一.概述 文件上传时,http请求头Content-Type须为multipart/form-data,有两种实现方式: 1.基于FormData对象,该方式简单灵活 2.基于<form> ...

  9. Gin-Go学习笔记四:Gin-Web框架 文件的上传下载

    文件的上传和下载 1->文件的上传 文件的上传,采用的是uploadify.js这个插件. 本事例实现的是上传图片文件,其他的文件上传也一样. 2->文件的下载 文件的下载有两个实现的方式 ...

  10. SpringBoot的文件上传&下载

    前言:不多BB直接上代码 文件上传 pom依赖添加commons-io <!-- 上传/下载jar https://mvnrepository.com/artifact/commons-io/c ...

随机推荐

  1. cocos2dx 3.x 集成protobuf

    vs2013+cocos2dx 3.13.1 这篇博文是集成Lua版本的protobuf,集成C++版本的过程也可参考. 主要参考博文地址<cocos2dx 3.x C++搭建protobuf环 ...

  2. web前端名人的博客微博Githu

    尤雨溪 vuejs作者 王垠  http://www.yinwang.org/ 20位活跃在Github上的国内技术大牛  1. lifesinger(玉伯)  Github主页:        ht ...

  3. RPG游戏中如何判断敌人是否在玩家的攻击范围之内

    // 方式1:通过主角和场景中的所有敌人比较 private void AtkCondition1(float _range,float _angle) { // 搜索所有敌人列表(在动态创建敌人时生 ...

  4. 【文献04】无人驾驶高速AWID-AWIS车辆运动控制研究

    参考:阮久宏, 李贻斌, 荣学文, et al. 无人驾驶高速AWID-AWIS车辆运动控制研究[J]. 农业机械学报, 2009, 40(12):37-42. https://drive.wps.c ...

  5. Genome-wide gene-environment analyses of depression and reported lifetime traumatic experiences in UK Biobank

    Genome-wide gene-environment analyses of depression and reported lifetime traumatic experiences in U ...

  6. Centos6.8 smokeping安装

    yum -y install rrdtool perl-rrdtool curl perl-core bind bind-chroot bind-utils httpd popt popt-devel ...

  7. HTML 弹出遮罩层一(遮罩层和内容标签嵌套)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Vrrp和Hsrp的区别

    VRRP原理协议简述简单来说,VRRP是一种容错协议,它为具有组播或广播能力的局域网(如以太网)设计,它保证当局域网内主机的下一跳路由器出现故障时,可以及时的由另一台路由器来代替,从而保持通讯的连续性 ...

  9. python记录_day11 闭包 迭代器

    一.第一类对象: 函数名是一个变量,可以当普通变量使用,但它又是一个特殊的变量,与括号配合可以执行函数. 函数名的运用 1.单独打印是一个内存地址 2.可以给其他变量赋值 3.可以作为容器类变量的元素 ...

  10. VBA find查找行号和列号的方法

    ).Worksheets(1).Range("b:b").Find("*", , , , , xlPrevious).Row)'查找最大行号 ).Workshe ...