W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
在使用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鍵即可多選文件.
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: