• 009 spring boot中文件的上传与下载


    一:任务

    1.任务

      文件的上传

      文件的下载

    二:文件的上传

    1.新建一个对象

      FileInfo.java

     1 package com.cao.dto;
     2 
     3 public class FileInfo {
     4     private String path;
     5     public FileInfo(String path) {
     6         this.path=path;
     7     }
     8     public String getPath() {
     9         return path;
    10     }
    11     public void setPath(String path) {
    12         this.path = path;
    13     }
    14     
    15 }

    2.新建控制器

     1 package com.cao.web.controller;
     2 
     3 import java.io.File;
     4 import java.io.IOException;
     5 import java.util.Date;
     6 
     7 import org.springframework.web.bind.annotation.PostMapping;
     8 import org.springframework.web.bind.annotation.RequestMapping;
     9 import org.springframework.web.bind.annotation.RestController;
    10 import org.springframework.web.multipart.MultipartFile;
    11 
    12 import com.cao.dto.FileInfo;
    13 
    14 @RestController
    15 @RequestMapping("/file")
    16 public class FileController {
    17     @PostMapping
    18     public FileInfo uploadFile(MultipartFile fileKey) throws Exception {
    19         System.out.println("fileName: "+fileKey.getName());
    20         System.out.println("OriginalFilename: "+fileKey.getOriginalFilename());
    21         System.out.println("size: "+fileKey.getSize());
    22         //将要存储在controller的目录下
    23         String folder="E:\workspace-sts-3.9.5.RELEASE\it-security-demo\src\main\java\com\cao\web\controller";
    24         File localFile=new File(folder,new Date().getTime()+".txt");
    25         fileKey.transferTo(localFile);
    26         //写入流中,这个主要用于写入其他的地方,例如服务器等,这里不写了
    27         //fileKey.getInputStream()
    28         return new FileInfo(localFile.getAbsolutePath());
    29     }
    30 }

    3.测试类

     1 /**
     2      * 测试文件的上传
     3      * @throws Exception 
     4      */
     5     @Test
     6     public void whenUploadSuccess() throws Exception {
     7         String result=mockMvc.perform(MockMvcRequestBuilders.fileUpload("/file")
     8                 .file(new MockMultipartFile("fileKey","test.txt","multipart/form-data","hello".getBytes("UTF-8"))))
     9                 .andExpect(MockMvcResultMatchers.status().isOk())
    10                 .andReturn().getResponse().getContentAsString();
    11         System.out.println("result="+result);
    12     }

    4.控制台

      

      存储到的现象

      

    三:文件的下载

    1.添加io操作的包

    1           <dependency>
    2             <groupId>commons-io</groupId>
    3             <artifactId>commons-io</artifactId>
    4           </dependency>

    2.文件下载的程序

     1 package com.cao.web.controller;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.IOException;
     6 import java.io.InputStream;
     7 import java.io.OutputStream;
     8 import java.util.Date;
     9 
    10 import javax.servlet.http.HttpServletRequest;
    11 import javax.servlet.http.HttpServletResponse;
    12 
    13 import org.apache.commons.io.IOUtils;
    14 import org.springframework.web.bind.annotation.GetMapping;
    15 import org.springframework.web.bind.annotation.PathVariable;
    16 import org.springframework.web.bind.annotation.PostMapping;
    17 import org.springframework.web.bind.annotation.RequestMapping;
    18 import org.springframework.web.bind.annotation.RestController;
    19 import org.springframework.web.multipart.MultipartFile;
    20 
    21 import com.cao.dto.FileInfo;
    22 
    23 @RestController
    24 @RequestMapping("/file")
    25 public class FileController {
    26     
    27     String folder="E:\workspace-sts-3.9.5.RELEASE\it-security-demo\src\main\java\com\cao\web\controller";
    28 
    29     /**
    30      * 文件的上传控制器
    31      * @param fileKey
    32      * @return
    33      * @throws Exception
    34      */
    35     @PostMapping
    36     public FileInfo uploadFile(MultipartFile fileKey) throws Exception {
    37         System.out.println("fileName: "+fileKey.getName());
    38         System.out.println("OriginalFilename: "+fileKey.getOriginalFilename());
    39         System.out.println("size: "+fileKey.getSize());
    40         //将要存储在controller的目录下
    41         File localFile=new File(folder,new Date().getTime()+".txt");
    42         fileKey.transferTo(localFile);
    43         //写入流中,这个主要用于写入其他的地方,例如服务器等,这里不写了
    44         //fileKey.getInputStream()
    45         return new FileInfo(localFile.getAbsolutePath());
    46     }
    47     
    48     /**
    49      * 文件的下载
    50      */
    51     @GetMapping("/{id}")
    52     public void downFile(@PathVariable String id, HttpServletRequest request,HttpServletResponse response) {
    53         try(
    54             InputStream inputStream=new FileInputStream(new File(folder,id+".txt"));
    55             OutputStream outputStream=response.getOutputStream();    
    56             ) 
    57         {
    58             response.setContentType("application/x-download");
    59             response.addHeader("Content-Disposition", "attachment;filename=test.txt");
    60             IOUtils.copy(inputStream, outputStream);
    61             outputStream.flush();
    62         } catch (Exception e) {
    63             // TODO: handle exception
    64         }
    65     }
    66     
    67     
    68 }

    3.在浏览器上访问

      

  • 相关阅读:
    android学习第一天
    定力
    C++ 虚基类表指针字节对齐
    c++内存对齐 转载
    #Pragma Pack(n)与内存分配
    c++ data语意学
    point类型·
    对象内存 (扩展 Data Structure Alignment)
    reinterpret_cast and const_cast
    static_cast AND dynamic_cast
  • 原文地址:https://www.cnblogs.com/juncaoit/p/9733763.html
Copyright © 2020-2023  润新知