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
    

      

  • 相关阅读:
    Centos安装Nginx过程步骤详细解析
    查看是否已经安装nginx
    使用uwsgi --http :80 --wsgi-file test.py 在浏览器上无法访问
    uwsgi 常用参数
    Python生成requirements.txt方法
    快排
    绝对路径和相对路径
    perspective结合transform的3D效果
    mobile web 手机开发
    Date对象需要注意的点
  • 原文地址:https://www.cnblogs.com/linlf03/p/11376958.html
Copyright © 2011-2022 走看看