画像アップロードテスト

2234 ワード

@Test
    public void uploadFilePublic1() throws Exception {
        File file = new File("D:/Document/BI/pic/aa201810232018.png");
        // 
        //String key = OssUtils.createFileKey(file.getName(), null);
        MockMultipartFile firstFile = new MockMultipartFile("multipartFiles", "aa201810232018.png",     // multipartFiles public ResponseBean uploadImg(@RequestParam("multipartFiles") MultipartFile[] multipartFiles){ 
                MediaType.MULTIPART_FORM_DATA_VALUE, new FileInputStream(file));

        mockMvc.perform(MockMvcRequestBuilders.fileUpload("/custom/uploadImg")
                .file(firstFile)
                        .param("key", "keys")
                )// 
                .andExpect(MockMvcResultMatchers.status().isOk());

    }
@ApiOperation(value = " ")
    @PostMapping(value = "/uploadImg")
    @ResponseBody
    public ResponseBean uploadImg(@RequestParam("multipartFiles") MultipartFile[] multipartFiles){
        ResponseBean responseBean = new ResponseBean<>();
        RsBody rsBody = new RsBody<>();
        List imgList = new ArrayList<>();

        for (MultipartFile multi : multipartFiles) {
            System.out.println("---");
            ImageVO imageVO = new ImageVO();
            String name = multi.getOriginalFilename();
            // 
            List suffixList = Arrays.asList("jpg", "png");
            String suffix = name.substring(name.lastIndexOf(".") + 1, name.length());
            if (suffixList.contains(suffix.trim().toLowerCase())) {
                String url = OSSClientUtils.uploadImg2Oss(multi);
                System.out.println(url);
                imageVO.setUrl(url);
                imgList.add(imageVO);
            } else {
                continue;
            }
        }
        System.out.println(imgList);
        rsBody.setData(imgList);
        responseBean.setResponseBody(rsBody);
        return responseBean;
    }

nginx技術学習https://www.itkc8.com