工程目录结构

完整代码:

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. gooreplacer 很好用

    国内上 StackOverflow, hackernews 之类的站点会慢. 因为页面里有链接指向 google 谷歌, 会被墙. 于是拖累了整个页面的显示. gooreplacer 可以把这些被墙连 ...

  2. 文献导读 - Machine Learning Identifies Stemness Features Associated with Oncogenic Dedifferentiation

    参考: Machine Learning Identifies Stemness Features Associated with Oncogenic Dedifferentiation 前所未有!1 ...

  3. 比较Class.getResource与Class.getClassLoader().getResource两种方式读取资源文件

    /** * @author zhangboqing * @date 2018/7/10 */ public class FileDemo { public static void main(Strin ...

  4. 流水的新技术,铁打的Linux

    关注嘉为科技,获取运维新知 这一年人工智能火了,凡是带电的专业都往AI上靠,实在靠不上的还可以看AlphaGo下棋,探讨AI能否取代人类.这种全民跟风,比前两年的“云计算”.“大数据”热度还高.就算你 ...

  5. python生成随机整数

    python生成随机不重复的整数,用random中的sample index = random.sample(range(0,10),10) 上面是生成不重复的10个从1~10的整数 python生成 ...

  6. PHP字符串函数小结

    1. strlen:获得字符串长度 2. substr:字符串截取函数 格式:string substr ( string $string , int $start [, int $length ] ...

  7. Lowest Common Ancestor of a Binary Search Tree(Java 递归与非递归)

    题目描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...

  8. link标签实现给网页标题前加一个小图标favicon.ico

    使用方法如下:1.<link rel="shortcut icon " type="images/x-icon" href="./favicon ...

  9. 自定义alert弹框,title不显示域名

    问题: 系统默认的alert弹框的title会默认显示网页域名 解决办法: (修改弹框样式) (function() { window.alert = function(name) { $(" ...

  10. 灵活运用 SQL SERVER FOR XML PATH 转

    灵活运用 SQL SERVER FOR XML PATH   FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些 ...