springboot之文件上传下载

在很多现实情况下,我们都会遇到过上传下载的问题,那么运用springboot框架如何实现上传下载呢,话不多说,直接上代码

html页面代码,分成了三部分单文件上传,多文件上传,下载

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>Insert title here</title>

</head>
<body>

    <form action="http://localhost:8080/test/onceupload" method="post"
        enctype="multipart/form-data">
        <p>单文件上传:</p>
        <br /> <input type="file" name="file1" /> <input type="submit"
            value="上传" />

    </form>



    <form action="http://localhost:8080/test/manyupload" method="post"
    enctype="multipart/form-data">
    <p>多文件上传</p><br/>
    
    <input type="file" name="file2"/>
    <input type="file" name="file2"/>
    <input type="file" name="file2"/>
    <input type="submit" value="上传多文件"/>
    </form>


    <a href="http://localhost:8080/test/downLoad">下载</a>

</body>
</html>

单文件上传

@RequestMapping(value = "/onceupload")
    public String uploadOnce(HttpServletRequest request, MultipartHttpServletRequest multiRequest) {

        // 获取上传文件的路径
        String uploadFilePath = multiRequest.getFile("file1").getOriginalFilename();
        System.out.println("uploadFilePath:" + uploadFilePath);

        // 截取上传文件的文件名
        String uploadFileName = uploadFilePath.substring(uploadFilePath.lastIndexOf('\') + 1,
                uploadFilePath.indexOf('.'));
        System.out.println("uploadFileName:" + uploadFileName);

        // 截取上传文件的后缀
        String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.indexOf('.') + 1, uploadFilePath.length());

        System.out.println("uploadFileSuffix:" + uploadFileSuffix);

        FileOutputStream fos = null;
        FileInputStream fis = null;

        String newPath = "E:\" + System.nanoTime() + uploadFileSuffix;
        try {
            fis = (FileInputStream) multiRequest.getFile("file1").getInputStream();

            fos = new FileOutputStream(new File(newPath));

            byte[] bytes = new byte[1024];

            int i = fis.read(bytes);
            while (i != -1) {
                fos.write(bytes, 0, bytes.length);
                fos.flush();
                i = fis.read(bytes);
            }

            return "admin/login";//这里根据自己实际情况返回路径
} catch (IOException e) { // TODO Auto-generated catch block  e.printStackTrace(); return "error"; } finally { // 关流 if (fis != null) { try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block  e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block  e.printStackTrace(); } } } }

多文件上传

@RequestMapping(value="manyupload")
    public String manyUpload(HttpServletRequest request, MultipartHttpServletRequest multiRequest) {

        List<MultipartFile> multipartFiles = multiRequest.getFiles("file2");

        MultipartFile file = null;
        BufferedOutputStream bos = null;
        for (int i = 0; i < multipartFiles.size(); ++i) {
            file = multipartFiles.get(i);
            if (!file.isEmpty()) {
                // 获取上传文件的路径
                String uploadFilePath = file.getOriginalFilename();
                System.out.println("uploadFilePath:" + uploadFilePath);
                // 截取上传文件的文件名
                String uploadFileName = uploadFilePath.substring(uploadFilePath.lastIndexOf('\') + 1,
                        uploadFilePath.indexOf('.'));
                System.out.println("uploadFileName:" + uploadFileName);

                // 截取上传文件的后缀
                String uploadFileSuffix = uploadFilePath.substring(uploadFilePath.indexOf('.') + 1,
                        uploadFilePath.length());

                System.out.println("uploadFileSuffix:" + uploadFileSuffix);

                String newPath = "E:\" + System.nanoTime() + uploadFileSuffix;
                
                try {
                    bos=new BufferedOutputStream(new FileOutputStream(new File(newPath)));
                    byte[]bytes=file.getBytes();
                    
                    bos.write(bytes, 0, bytes.length);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }catch(Exception e){
                    e.printStackTrace();
                }
                
            } else {
                System.out.println("上传的文件为空");
            }
            System.out.println("上传文件完成了");
            
        }

        return "admin/login";//这里根据自己实际情况返回路径
    }

下载

@RequestMapping("/downLoad")
    @ResponseBody
    public void downLoad(HttpServletResponse res){
        
        String fileName = "a.txt";
        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("f://"
              + 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");
    
        
        
    }
原文地址:https://www.cnblogs.com/1a2b/p/9009162.html