zoukankan      html  css  js  c++  java
  • springboot 上传和下载案例

    1、SpringBoot 项目搭建过程略;

    2、上传下载Controller

    package com.spqin.updownload.controller;
    
    /**
     * 文件上传和下载
     * @author Spqin
     * @Date 2020/12/3 9:39
     */
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.multipart.MultipartHttpServletRequest;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.*;
    import java.util.List;
    
    @Controller
    @RequestMapping("/file")
    public class FileUpDownLoadController {
    
        /**
         * 上传文件存放路径
         */
        private String filePath = "D:/Test/upload/";
    
        /**
         * 上传,支持多文件上传
         * @param request
         * @return
         */
        @RequestMapping(value = "upload", method = RequestMethod.POST)
        @ResponseBody
        public String fileUpload(HttpServletRequest request) {
            List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
            MultipartFile file = null;
            BufferedOutputStream stream = null;
            for (int i = 0; i < files.size(); ++i) {
                file = files.get(i);
                if (!file.isEmpty()) {
                    try {
                        //test.txt
                        String uploadFilePath = file.getOriginalFilename();
                        //test
                        String uploadFileName = uploadFilePath.substring(0,uploadFilePath.indexOf('.'));
                        //txt
                        String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.indexOf('.') + 1, uploadFilePath.length());
                        stream = new BufferedOutputStream(new FileOutputStream(new File(
                                filePath +uploadFilePath)));
                        byte[] bytes = file.getBytes();
                        stream.write(bytes,0,bytes.length);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (stream != null) {
                                stream.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    System.out.println("上传文件为空");
                }
            }
            System.out.println("文件接受成功了");
            return "Success";
        }
    
    
        /**
         * 下载
         * @param res
         * @param fileName
         */
        @RequestMapping(value = "/download", method = RequestMethod.GET)
        public void fileDownload(HttpServletResponse res,String fileName) {
              res.setHeader("content-type", "application/octet-stream");
            res.setContentType("application/octet-stream");
            res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            byte[] buff = new byte[1024];
            BufferedInputStream bis = null;
            OutputStream os = null;
            try {
                os = res.getOutputStream();
                bis = new BufferedInputStream(new FileInputStream(new File(filePath
                        + fileName)));
                int i = bis.read(buff);
                while (i != -1) {
                    os.write(buff, 0, buff.length);
                    os.flush();
                    i = bis.read(buff);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println("success");
        }
    }

    3、浏览器上传下载测试

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form method="POST" enctype="multipart/form-data" 
        action="http://localhost:8080/updownload/file/upload">
        <p>文件1:<input type="file" name="file" /></p>
        <p>文件2:<input type="file" name="file" /></p>
        <p><input type="submit" value="上传" /></p>
    </form>
    <a href="http://localhost:8080/updownload/file/download?fileName=test.txt">下载</a>
    </body>
    </html>
  • 相关阅读:
    C#基础第五天
    基础学习14天 MD5加密
    C#基础第二天
    C#基础学习第一天
    Privacy Policy of ColorfulBroswer
    asp.net mvc 上传图片 摘自mvc 高级编程第311页
    多彩浏览器win10版 隐私声明
    uwp获取版本信息win10 VersionInfo
    uwp ,win10 post json
    windows phone 8.0 app 移植到windows10 app 页面类
  • 原文地址:https://www.cnblogs.com/spqin/p/14078150.html
Copyright © 2011-2022 走看看