文件上傳不顯示上傳選擇文本域

2019-05-30 20:55 更新

在使用SwaggerBootstrapUi的朋友經(jīng)常詢問的一個(gè)問題,為什么上傳參數(shù)file對象不顯示file文本域,而是普通文本,如下圖:

因?yàn)镾pringfox-Swagger針對不同的版本,某些版本也會(huì)出現(xiàn)此問題,為一勞永逸,SwaggerBootstrapUi特別指定需要強(qiáng)指定dataType類型為MultipartFile

代碼示例(UploadController.java):

@ApiOperation(value = "文件素材上傳接口")
@ApiImplicitParams({@ApiImplicitParam(name = "file[]", value = "文件流對象,接收數(shù)組格式", required = true,dataType = "MultipartFile",allowMultiple = true),
                    @ApiImplicitParam(name = "title", value = "title", required = true)}
                  )
@RequestMapping(value="/uploadMaterial",method = RequestMethod.POST)
@ResponseBody
public RestMessage uploadMaterial(@RequestParam(value="file[]",required = true) MultipartFile[] files,@RequestParam(value = "title") String title, HttpServletRequest request) throws IOException {
    //int mul=1*1024*1024;
    String realPath=request.getSession().getServletContext().getRealPath("/upload");
    File realFile=new File(realPath);
    if (!realFile.exists()){
        realFile.mkdirs();
    }
    List<Map> uploadFiles= Lists.newArrayList();
    System.out.println("進(jìn)入圖片上傳接口:"+files.length +"張");
    for (MultipartFile file : files) {
        File targetFile=new File(realFile,file.getOriginalFilename());
        FileOutputStream fileOutputStream=null;
        InputStream ins=null;
        try{
            fileOutputStream=new FileOutputStream(targetFile);
            int i=-1;
            byte[] bytes=new byte[1024*1024];
            ins=file.getInputStream();
            while ((i=ins.read(bytes))!=-1){
                fileOutputStream.write(bytes,0,i);
            }
        }catch (IOException e){
        }finally {
            closeQuilty(ins);
            closeQuilty(fileOutputStream);
        }
        Map fileInfo= Maps.newHashMap();
        fileInfo.put("id", UUID.randomUUID().toString());
        fileInfo.put("url",targetFile.getPath());
        fileInfo.put("original_name",targetFile.getName());
        uploadFiles.add(fileInfo);
    }
    RestMessage rm=new RestMessage();
    rm.setData(uploadFiles);
    return rm;
}

關(guān)于多文件上傳,設(shè)置allowMultiple=true即可,在UI界面端,按住Ctrl鍵即可多選文件.

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)