zoukankan      html  css  js  c++  java
  • springboot简易上传下载

    1.导入上传下载依赖:

         <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3.2</version>
            </dependency>
    
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.4</version>
            </dependency>
        <!-- 添加thymeleaf -->
         <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-thymeleaf</artifactId>
         </dependency>

    2.上传:

    1)前端页面:

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script>
            function exproExcel() {
                
            }
        </script>
    </head>
    <body>
        <h1>Spring Boot</h1>
        <a href="/Expro/excel">导出</a>
        <p th:text="${hello}"></p>
        <p>文件上传</p>
        <form action="/upload/file" method="post" enctype="multipart/form-data">
            上传:<input type="file" name="upfile"/>
            <button type="submit">提交</button>
        </form>
    
    </body>
    </html>

    2)编写跳到上传页面接口:

    @Controller
    public class HelloController {
    
        @RequestMapping("/hello")
        public String helloIndex(HashMap<String, Object> map){
    
            map.put("hello","Hello SpringBoot!");
            return "/index";
        }
    }

    3)编写接收上传文件接口:

    @Controller
    @RequestMapping("/upload")
    public class UploadFileController {
    
        @RequestMapping(value = "/file")
        public @ResponseBody String uploadFile(@RequestParam("upfile") MultipartFile file, HttpServletRequest request){
    
            String message ="";
    
            try {
    
                if(!file.isEmpty()){
    
                    FileOutputStream outputStream = new FileOutputStream("F:\XIAOYAO"+"\"+file.getOriginalFilename());
    
                    outputStream.write(file.getBytes());
                    outputStream.flush();
                    outputStream.close();
    
                    message="上传成功!";
    
    
                }
    
    
    
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                message="上传失败!";
            } catch (IOException e) {
                e.printStackTrace();
                message="上传失败!";
            }
    
                return message;
        }
    }

    3.下载:

    1)编写下载接口:

    @RestController
    @RequestMapping("/Expro")
    public class ExprotExcelController {
    
        @RequestMapping("/excel")
        public void exproExcel(HttpServletRequest request, HttpServletResponse response) throws Exception{
    
            String path =  ClassLoader.getSystemResource("").toURI().getPath(); //获取类加载地址
    
            System.out.println(path);
    
            File file = new File(path+"excelTempalte/模板.xlsx");
            FileInputStream fileInputStream = new FileInputStream(file); //读取文件
    
            response.setHeader("Content-disposition", "attachment;filename=test.xlsx"); //设置响应头和文件名字
            OutputStream outputStream = response.getOutputStream();
    
            //创建缓存区
            byte [] buffe = new byte[1024];
    
            int len =0;
    
            while ((len =fileInputStream.read(buffe))>0){
                outputStream.write(buffe,0,len);
            }
            
    
            fileInputStream.close();
            outputStream.flush();
            outputStream.close();
        }
    }
  • 相关阅读:
    python 基础(三) list 的用法
    python 基础(二) string 的用法
    python 基础(四)购物车(list类型练习)
    python 基础(一)猜年龄游戏
    centOS 7 环境搭建之安装 python 3
    centOS 7 环境搭建之安装 JDK 8
    源代码管理相关命令(Git常用命令、Nuget常用命令、CMD常用命令)
    .Net Core 3.0 关于Windows Form和WPF的全面支持
    .Net Core 常用开发工具(IDE和运行时、Visual Studio插件、Visual Studio Code插件)
    .Net Core 精选公众号集合(保持更新)
  • 原文地址:https://www.cnblogs.com/chenziyu/p/9257755.html
Copyright © 2011-2022 走看看