美文网首页
day08 文件上传 — 应用服务器

day08 文件上传 — 应用服务器

作者: 山下_26 | 来源:发表于2019-03-25 18:43 被阅读0次
  • 新建一个模块,在创建时勾选web和Thymeleaf依赖
  • 新建一个upload.java文件
package com.springboot.upload.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 上传文件控制器
 * 直接上传到服务器
 * @author 26
 * 2019.3.25
 */

@Controller
public class UploadController {

    //遇到http://localhost:8080,则跳转到upload.html页面
    @GetMapping("/")
    public String index(){
        return "upload";
    }

    @PostMapping("/upload")
    public String fileUpload(@RequestParam("file")MultipartFile srcFile,
                             RedirectAttributes redirectAttributes){

        //生成系统时间
        SimpleDateFormat sf=new SimpleDateFormat("yyyyMMddHHmmss");
        String date=sf.format(new Date());

        //前端没有选择文件,srcFile为空
        if (srcFile.isEmpty()){
            redirectAttributes.addFlashAttribute("message", "请选择一个文件");
            return "redirect:upload_status";
        }
        //选择了文件,开始进行上传操作
        try {
            //构建上传目标路径
            File destFile=new File(ResourceUtils.getURL("classpath:").getPath());
            if (!destFile.exists()){
                destFile=new File("");
            }
            //输出目标文件的绝对路径
            System.out.println("file path:"+destFile.getAbsolutePath());
            //使用系统时间文件夹拼接目录
            File upload=new File(destFile.getAbsolutePath(),date);
            //若目标文件夹不存在,则创建一个
            if (!upload.exists()){
                upload.mkdirs();
            }
            //生成UUID码
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            // 获得文件原始名称
            String fileName = srcFile.getOriginalFilename();
            // 获得文件后缀名称
            String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
            // 生成最新的uuid文件名称
            String newFileName = uuid + "."+ suffixName;
            System.out.println("完整的上传路径:"+upload.getAbsolutePath()+"/"+newFileName);
            //根据srcFile的大小,准备一个字节数组
            byte[] bytes=srcFile.getBytes();
            //通过项目路径,拼接上传路径
            Path path=Paths.get(upload.getAbsolutePath()+"/"+newFileName);
            //最重要的一步,将源文件写入目标地址
            Files.write(path,bytes);
            //将文件上传成功的信息写入messages
            redirectAttributes.addFlashAttribute("message", "文件上传成功!"+newFileName);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:upload_status";
    }

    //匹配upload_status页面
    @GetMapping("/upload_status")
    public String uploadStatusPage(){
        return "upload_status";
    }
}
  • 在resources的templates下新建upload.html文件
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Spring Boot文件上传页面</title>
</head>
<body>
<form method="post" action="/upload" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="上传">

</form>

</body>
</html>
  • 同目录中新建upload_status.html文件
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>文件上传状态显示</title>
</head>
<body>
<h2>Spring Boot的文件上传状态</h2>
<div th:if="${message}"/>
<h2 th:text="${message}"/>
</body>
</html>
  • 运行Application文件,打开浏览器localhost:8080

相关文章

  • day08 文件上传 — 应用服务器

    新建一个模块,在创建时勾选web和Thymeleaf依赖 新建一个upload.java文件 在resources...

  • 2019-04-01

    Spring Boot文件上传 文件上传是项目开发中的常见操作。一般分为如下几种解决方案 直接上传到应用服务器 上...

  • 文件的上传

    基于SpringBoot的文件上传 上传方式: 1.直接上传到应用服务器 2.上传到OSS(阿里云、七牛云) 3....

  • 基于springboot的文件上传

    文件上传方式 直接上传到应用服务器 上传到oss(阿里云,七牛云) 前端将图片转成Base64编码上传 上传服务器...

  • 笔记3

    1、基于SpringBoot的文件上传 直接上传到应用服务器 上传到OSS(阿里云、七牛云) 前段将图片转成Bas...

  • SpringBoot文件上传

    基于Spring Boot的文件上传上传方式:1.直接上传到应用服务器2.上传到OSS(内容存储服务器,如:阿里云...

  • 4 SpringBoot文件上传

    基于Spring Boot的文件上传上传方式:1.直接上传到应用服务器2.上传到OSS(内容存储服务器,如:阿里云...

  • 4、基于SpringBoot的文件上传——前后端不分离

    基于SpringBoot的文件上传方式有:1、直接上传到应用服务器2、上传到OSS(阿里云、七牛云)3、前端将图片...

  • 5、基于SpringBoot的文件上传

    文件上传的三种方法 直接上传到应用服务器 上传到OSS(阿里云 七牛云) 前端将图片转成Base64编码上传 Sp...

  • dayFive 基于SpringBoot的文件上传

    文件上传的三种方法 直接上传到应用服务器 上传到OSS(阿里云 七牛云) 前端将图片转成Base64编码上传 Sp...

网友评论

      本文标题:day08 文件上传 — 应用服务器

      本文链接:https://www.haomeiwen.com/subject/aaibvqtx.html