图片上传测试

@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

你可能感兴趣的:(综合)