@ApiOperation(value="头像上传" , notes="用户进行头像上传操作") @ApiImplicitParams({ @ApiImplicitParam(value="用户id" , name="userId" , paramType="query" , dataType="String" , required=true) }) @PostMapping(value="/uploadFaceImage" , headers="content-type=multipart/form-data") public JsonResult uploadFaceImage(String userId, @RequestParam("file") MultipartFile[] file){ //文件存放路径c:/imooc_video_dev String fileSpacePath="C:/imooc_video_dev"; //数据库存放路径 String fileDBPath="/"+userId+"/face"; //字节输入缓冲流 BufferedInputStream bufferedInputStream=null;// InputStream inputStream=null; //字节输出缓冲流 BufferedOutputStream bufferedOutputStream=null;// OutputStream outputStream=null; //判断上传文件是否为空 System.out.println(file.length); try { if(file !=null && file.length>0){ //获取文件名称 String fileName=file[0].getOriginalFilename(); //判断文件名是否为空 if (StringUtils.isNotBlank(fileName)) { //文件最终保存路径 String finalName=fileSpacePath+fileDBPath+"/"+fileName; //数据库存放路径 fileDBPath +=("/"+fileName); //新建一个file对象 File outFile=new File(finalName); //判断文件夹是否存在,当文件夹不存在时,创建文件夹 if (outFile.getParentFile()!=null || !outFile.getParentFile().isDirectory()) { outFile.getParentFile().mkdirs(); } //将上传的文件转换成File File inputFile=new File(file.toString()); bufferedInputStream =new BufferedInputStream( new FileInputStream(inputFile)); bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(outFile)); IOUtils.copy(bufferedInputStream, bufferedOutputStream); } }else{ return JsonResult.errorMsg("文件无法上传"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { bufferedInputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { bufferedOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return JsonResult.ok(); }}