SpringBoot-05-之上传文件

简介: 需要使用引擎模板thymeleaf,如果不清楚,可见04--SpringBoot之模板引擎--thymeleaf1.新建表单网页:templates/upfile.

需要使用引擎模板thymeleaf,如果不清楚,可见04--SpringBoot之模板引擎--thymeleaf

1.新建表单网页:templates/upfile.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/upload">
    文件上传<input type="file" name="file"/>
    <input type="submit" value="上传"/>
</form>
</body>
</html>
2.控制器:toly1994.com.toly01.controller.UpFileController
/**
 * 作者:张风捷特烈
 * 时间:2018/5/29:23:15
 * 邮箱:1981462002@qq.com
 * 说明:文件上传控制器
 */
@Controller
public class UpFileController {
    @GetMapping("/upload_html")
    public String uploadHtml() {
        return "upfile";
    }

    //处理文件上传
    @PostMapping(value = "/upload")
    public @ResponseBody
    String uploadImg(
            @RequestParam("file") MultipartFile file, HttpServletRequest request) {

        if (file.isEmpty()) {
            return "false";
        }
        String fileName = file.getOriginalFilename();//获取名字

        String path = "F:/test";
        File dest = new File(path + "/" + fileName);
        if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
            return "上传成功!";
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
            return "上传失败!";
        }
    }
}
upload.png
相关文章
|
3月前
|
Java
SpringBoot之文件上传(单文件与多文件上传的使用)
SpringBoot之文件上传(单文件与多文件上传的使用)
|
4月前
|
Java 关系型数据库 MySQL
文件在线压缩与解压|基于Springboot实现文件在线压缩与解压
文件在线压缩与解压|基于Springboot实现文件在线压缩与解压
|
2月前
|
JavaScript 前端开发 Java
springboot整合minio+vue实现大文件分片上传,断点续传(复制可用,包含minio工具类)
springboot整合minio+vue实现大文件分片上传,断点续传(复制可用,包含minio工具类)
364 0
|
26天前
|
SQL Java 数据库连接
springboot解析txt文件顺便加到数据库中(nohup文件)
springboot解析txt文件顺便加到数据库中(nohup文件)
108 1
|
27天前
|
存储 JavaScript 前端开发
Spring Boot + Vue: 实现文件导入导出功能
本文介绍了使用Spring Boot和Vue实现文件导入导出的步骤。在后端,Spring Boot通过`MultipartFile`接收上传文件,保存至服务器,并使用`ResponseEntity`提供文件下载。前端部分,Vue项目借助`axios`发送HTTP请求,实现文件选择、上传及下载功能。这种前后端分离的实现方式提高了应用的可维护性和可扩展性。
23 2
|
28天前
|
Java Nacos 数据安全/隐私保护
springboot使用configtree读取树形文件目录中的配置
springboot使用configtree读取树形文件目录中的配置
springboot使用configtree读取树形文件目录中的配置
|
28天前
|
Java Spring
springboot项目读取 resources 目录下的文件的9种方式(总结)
springboot项目读取 resources 目录下的文件的9种方式(总结)
79 1
|
2月前
|
Java Docker 容器
docker部署springboot指定yml文件
docker部署springboot指定yml文件
55 0
|
2月前
|
druid JavaScript Java
SpringBoot+Vue.js实现大文件分片上传、断点续传与极速秒传
SpringBoot+Vue.js实现大文件分片上传、断点续传与极速秒传
100 0
|
3月前
|
Java Maven
IDEA Maven SpringBoot配置POM文件
IDEA Maven SpringBoot配置POM文件
34 0