我使用fileinput插件上传文件,请问入口方法该怎么改?
前端配置参数如下:
var options ={
language: 'zh', //设置语言
allowedPreviewTypes : [ 'image' ],
allowedFileTypes: ['image','text','object','flash'],//默认是['image', 'html', 'text', 'video', 'audio', 'flash','object']
uploadUrl: "${base}/file/upload/file", //上传的地址
// allowedFileExtensions : ['jpg', 'png','gif'],//接收的文件后缀,
maxFileCount: 2000,
enctype: 'multipart/form-data',
showUpload: true, //是否显示上传按钮
showCaption: true,//是否显示标题
showRemove : true,
browseClass: "btn btn-primary", //按钮样式
previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",
};
入口方法如下:
@AdaptBy(type = UploadAdaptor.class)
@POST
@At
@Ok("json")
@RequiresAuthentication
public Object file(@Param("file") TempFile tf, HttpServletRequest req, AdaptorErrorContext err) {
try {
if (err != null && err.getAdaptorErr() != null) {
return NutMap.NEW().addv("code", 1).addv("msg", "文件不合法");
} else if (tf == null) {
return Result.error("空文件");
} else {
String p = Globals.AppRoot;
String f = Globals.AppUploadPath + "/file/" + DateUtil.format(new Date(), "yyyyMMdd") + "/" + R.UU32() + tf.getSubmittedFileName().substring(tf.getSubmittedFileName().indexOf("."));
Files.write(new File(p + f), tf.getInputStream());
return Result.success("上传成功", Globals.AppBase+f);
}
} catch (Exception e) {
return Result.error("系统错误");
} catch (Throwable e) {
return Result.error("图片格式错误");
}
}