zoukankan      html  css  js  c++  java
  • Spring Boot Controller单元测试

    一、创建Controller

    一个方法是用传统IO来下载文件,一个是NIO下载文件

    @Controller
    public class FileController {
    
        private Logger log = LoggerFactory.getLogger(FileController.class);
    
    
        @RequestMapping(value="/download/oldio}", method = RequestMethod.GET)
        public void download(HttpServletRequest request, HttpServletResponse response,String fileName) throws IOException{
             String folder = "C://Users/xxx/Downloads/0714";
             File file = new File(folder, fileName);
             if(file.exists()){
                 response.setContentType("MimeType");
                 response.addHeader("Content-Disposition","attachment;filename=" +fileName);
                 response.setContentLength((int)file.length());
    
                 //Java IO
                 InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
                 //Copy bytes from source to destination(outputstream in this example), closes both streams.
                 FileCopyUtils.copy(inputStream, response.getOutputStream());
    
                 log.info("download success! ---" + fileName);
    
    
    
             }else {
                 throw  new Error("file not exist");
             }
         }
    
    
        @RequestMapping(value="/download/nio}", method = RequestMethod.GET)
        public void downloadfornio(HttpServletRequest request, HttpServletResponse response, String fileName) throws IOException{
            String folder = "C://Users/xxx/Downloads/0714";
            File file = new File(folder, fileName);
            if(file.exists()){
                response.setContentType("MimeType");
                response.addHeader("Content-Disposition","attachment;filename=" +fileName);
                response.setContentLength((int)file.length());
    
                //128 * 1024 = 128K
                int bufferSize = 131072 * 6;
                FileInputStream fileInputStream = new FileInputStream(file);
                FileChannel fileChannel = fileInputStream.getChannel();
                // 6 * 128K = 768K = 786432
                ByteBuffer buffer = ByteBuffer.allocateDirect(786432);
                byte[] byteArr = new byte[bufferSize];
                int nRead, nGet;
    
                try {
                    while ((nRead = fileChannel.read(buffer)) != -1){
                        if(nRead == 0){
                            continue;
                        }
                        buffer.position(0);
                        buffer.limit(nRead);
                        while (buffer.hasRemaining()){
                            nGet = Math.min(buffer.remaining(), bufferSize);
                            // read bytes from disk
                            buffer.get(byteArr,0,nGet);
                            //write bytes to output
                            response.getOutputStream().write(byteArr);
                        }
                        buffer.clear();
    
    
                    }
                    log.info("download success! ---" + fileName);
                }catch (IOException e){
                    e.printStackTrace();
                }finally {
                    buffer.clear();
                    fileChannel.close();
                    fileInputStream.close();
                }
    
    
    
            }else {
                throw  new Error("file not exist");
            }
        }
    }
    

      

    二、创建单元测试

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class FileControllerTest {
    
        private MockMvc mockMvc;
    
        @Autowired
        private WebApplicationContext wac;
    
        private  FileController fc;
    
        @Before
        public  void  setup(){
            mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
            fc = this.wac.getBean(FileController.class);
        }
    
    
    
        @Test
        public void compareTime() throws Exception {
    
            String fileName = "11.tar.gz";
            MockHttpServletRequest request = new MockHttpServletRequest();
            MockHttpServletResponse response = new MockHttpServletResponse();
    
    
            long c = System.currentTimeMillis();
            fc.downloadfornio(request, response, fileName);
            long d = System.currentTimeMillis();
            System.out.println("nio download takes :" + (d - c) );
    
            long a = System.currentTimeMillis();
            fc.download(request, response,fileName);
            long b = System.currentTimeMillis();
            System.out.println("io download takes :" + (b - a) );
    
    
    
    
        }
    
    }
    

      输出结果

    nio download takes :144
    io download takes :164
    

      

  • 相关阅读:
    暑期大作战第三天
    暑期大作战 第二天
    暑假作战第一天
    JDK源码学习笔记——Object
    JVM堆 栈 方法区详解
    JVM入门——JVM内存结构
    Spring Boot 1.Hello World
    Flutter Widget不刷新问题
    Flutter 根界面退出的时候(即是应用退出),不会触发deactivate/dispose方法 / 监听返回按钮
    Android Studio 3.3.1 代码提示不区分大小写
  • 原文地址:https://www.cnblogs.com/linlf03/p/11376958.html
Copyright © 2011-2022 走看看