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>
  • 相关阅读:
    【[Offer收割]编程练习赛12 B】一面砖墙
    【[Offer收割]编程练习赛12 A】歌德巴赫猜想
    【codeforces 779E】Bitwise Formula
    Java Web整合开发(85)
    数字
    T2602 最短路径问题 codevs
    P3378 堆【模板】 洛谷
    T1013 求先序排列 codevs
    P1717 钓鱼 洛谷
    P2085 最小函数值 洛谷
  • 原文地址:https://www.cnblogs.com/spqin/p/14078150.html
Copyright © 2011-2022 走看看