@wendal
package nuoshang.bluejay.modules;
import nuoshang.bluejay.base.Globals;
import nuoshang.bluejay.base.Result;
import nuoshang.bluejay.base.exception.AppRuntimeException;
import nuoshang.bluejay.bean.Image;
import nuoshang.bluejay.services.ImgService;
import nuoshang.bluejay.util.DateUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Files;
import org.nutz.lang.random.R;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.impl.AdaptorErrorContext;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;
import org.nutz.mvc.view.HttpStatusView;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
/**
* Created by Administrator on 2016/8/18.
*/
@IocBean
@At("/img")
@Fail("http:500")
public class ImgModule {
private static final Log log = Logs.get();
@Inject
protected ImgService imgService;
@At
@Ok("json")
public Object add(@Param("::img.") Image img){
NutMap re = new NutMap();
if(img == null){
return re.addv("ok", false).addv("msg", "参数错误").addv("result", "03");
}
try {
imgService.Save(img);
} catch (AppRuntimeException e) {
return re.addv("ok", false).addv("msg", e.getMessage()).addv("result", "02");
}
return re.addv("ok", true).addv("result", "01");
}
@At
@Ok("json")
public Object List(){
return null;
}
@At
@Ok("json")
@POST
@RequiresAuthentication
@AdaptBy(type = UploadAdaptor.class, args = { "ioc:myUpload" })
public Object uploadPhoto( @Param("userId") Integer uid,@Param("photo") 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 {
// path = Mvcs.getServletContext().getRealPath("/") + "/asserts/user/" + uid + ".jpg"; // 更常规的做法是通过配置文件来确定目标路径.
// Files.write(path, tf.getInputStream()); // 写入流.
String p = Globals.AppRoot;
String f = Globals.AppUploadPath + "/image/" + DateUtil.format(new Date(), "yyyyMMdd") + "/" + R.UU32() + tf.getSubmittedFileName().substring(tf.getSubmittedFileName().indexOf(".jpg"));
Files.write(new File(p + f), tf.getInputStream());
return Result.success("上传成功", f);
}
} catch (Exception e) {
return Result.error("系统错误");
} catch (Throwable e) {
return Result.error("图片格式错误");
}
}
@Ok("raw:jpg")
@At("/upload/?/?")
@Fail("http:404")
public Object image(String p, String p2) throws IOException {
if ((p + p2).contains(".."))
return HttpStatusView.HTTP_404;
File f = new File("/upload/image", p + "/" + p2 + ".jpg");
return f;
}
}
http://localhost:8080/img/upload/20160819/r0k8cilqk0iqsp0l58luqrfha3.jpg
入口方法没有调用到 这个方法没有执行 是为什么呢