在实践NutzBook 这个项目时,做到上传头像的入口方法。出现没有权限创建文件夹。
我在项目中WEB-INF对应的地方创建好了,没用。
求职
```java
@AdaptBy(type = UploadAdaptor.class, args = {"${app.root}/WEB-INF/tmp/user_avatar", "8192", "utf-8", "20000", "102400"})
@POST
@Ok(">>:/user/profile")
@At("/avatar")
public void uploadAvatar(@Param("file") TempFile tf, @Attr(scope = Scope.SESSION, value = "me") int userId, AdaptorErrorContext err) {
String msg = null;
if (err != null && err.getAdaptorErr() != null) {
msg = "文件大小不符合规定";
} else if (tf == null) {
msg = "空文件";
} else {
UserProfile profile = get(userId);
try {
BufferedImage image = Images.read(tf.getFile());
image = Images.zoomScale(image, 128, 128, Color.WHITE);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Images.writeJpeg(image, out, 0.8f);
profile.setAvatar(out.toByteArray());
dao.update(profile, "^avatar$");
} catch (DaoException e) {
log.info("System Error", e);
msg = "系统错误";
} catch (Throwable e) {
msg = "图片格式错误";
}
}
if (msg != null)
Mvcs.getHttpSession().setAttribute("upload-error-msg",msg);
}
java
2018-06-06 10:11:32,799 org.nutz.filepool.NutFilePool.(NutFilePool.java:25) INFO - Init file-pool by: D:/apps/Tomcat8/webapps/ROOT/WEB-INF/tmp/user_avatar", "8192", "utf-8", "20000", "102400 [2000]
2018-06-06 10:11:32,803 org.nutz.lang.Files.createDirIfNoExists(Files.java:423) WARN - create filepool dir(D:\apps\Tomcat8\webapps\ROOT\WEB-INF\tmp\user_avatar", "8192", "utf-8", "20000", "102400) fail!!
2018-06-06 10:11:32,805 org.nutz.mvc.impl.NutActionChainMaker.eval(NutActionChainMaker.java:51) DEBUG - Eval FAIL!! : public void com.jaden.nutzbook.module.UserProfileModule.uploadAvatar(org.nutz.mvc.upload.TempFile,int,org.nutz.mvc.impl.AdaptorErrorContext)
org.nutz.lang.born.BorningException: Fail to born or cast to 'org.nutz.mvc.upload.UploadAdaptor'
by args: [@(D:/apps/Tomcat8/webapps/ROOT/WEB-INF/tmp/user_avatar", "8192", "utf-8", "20000", "102400)]
because:java.lang.RuntimeException: 'D:/apps/Tomcat8/webapps/ROOT/WEB-INF/tmp/user_avatar", "8192", "utf-8", "20000", "102400' should be a directory or don't have permission to create it!
at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:25)
at org.nutz.lang.born.BornContext.doBorn(BornContext.java:60)
at org.nutz.lang.Mirror.born(Mirror.java:1010)
at org.nutz.mvc.impl.Loadings.evalObj(Loadings.java:336)
at org.nutz.mvc.impl.processor.AbstractProcessor.evalObj(AbstractProcessor.java:48)
at org.nutz.mvc.impl.processor.AdaptorProcessor.evalHttpAdaptor(AdaptorProcessor.java:34)
at org.nutz.mvc.impl.processor.AdaptorProcessor.init(AdaptorProcessor.java:20)
at org.nutz.mvc.impl.NutActionChainMaker.eval(NutActionChainMaker.java:38)
at org.nutz.mvc.impl.UrlMappingImpl.add(UrlMappingImpl.java:54)
at org.nutz.mvc.impl.NutLoading.evalUrlMapping(NutLoading.java:205)
at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:108)
at org.nutz.mvc.ActionHandler.(ActionHandler.java:19)
```