@At
@POST
@Ok("json")
@AdaptBy(type = UploadAdaptor.class, args = {"ioc:imageUpload"})
@RequiresAuthentication
@ApiOperation(value = "文件上传", notes = "文件上传", httpMethod = "POST")
@ApiParam(value = "图片接受参数", name = "tf")
public Object image(@Param("Filedata") TempFile tf, HttpServletRequest req, AdaptorErrorContext err) {
try {
if (err != null && err.getAdaptorErr() != null) {
return ApiResult.error("文件不合法,支持(gif|jpg|png)");
} else if (tf == null) {
return ApiResult.error("空文件");
} else {
String suffix = tf.getSubmittedFileName().substring(tf.getSubmittedFileName().indexOf("."));
String f = Globals.AppUploadPath + "image/" + DateUtil.format(new Date(), "yyyyMMdd") + "/" + R.UU32() +suffix ;
Files.write(new File(f), tf.getInputStream());
Document document = new Document() ;
document.setUrl("/platform/file/download/fileDownload?url="+f);
document.setPath(f);
document.setName(tf.getSubmittedFileName());
documentService.insert(document) ;
return ApiResult.success(document);
}
} catch (Exception e) {
return ApiResult.error("系统错误");
} catch (Throwable e) {
return ApiResult.error("图片格式错误");
}
}
上传文件格式及大小验证,err里面的errros是空的,只能拿到err.getAdaptorErr()里面的一个Unsupport file name 'test.pdf'