我先用前端form上传表单数据(包含上传文件)到后台服务器,在后台服务器中可以提出TempFile
然后在后台程序中通过如下类上传图片到指定文件服务器
public static NutMap Upload(String token,String userId,String type, TempFile tempFile){
try {
Request req = Request.create("http://127.0.0.1:8182/upload/image/user/" + userId, Request.METHOD.POST);
File f = File.createTempFile("shuxin", "data");
Files.write(f, tempFile.getInputStream());
req.getParams().put(type, f);
req.getParams().put("token", token);
FilePostSender sender = new FilePostSender(req);
Response resp = sender.send();
String json = resp.getContent();
Result result = Json.fromJson(Result.class, json);
if (result.success()) {
List<NutMap> list = (List<NutMap>) result.getData();
if (list != null && list.size() > 0) {
return list.get(0);
} else {
throw Lang.makeThrow("未知错误");
}
} else {
throw Lang.makeThrow(result.getMsg());
}
}
catch(IOException ex){
throw Lang.makeThrow(ex.getMessage());
}
}
文件服务器(采用NB搭建)接收端口为:
文件服务器直接通过前端表单上传是没有问题的
@AdaptBy(type = UploadAdaptor.class, args = {"ioc:imageUpload"})
@At(value = {"/image/?/?"})
@Ok("json")
public Object images(String type, String name, @Param("image") TempFile[] files, @Param("..")NutMap data, HttpServletRequest req, AdaptorErrorContext err) {
try {
if (err != null && err.getAdaptorErr() != null) {
return Result.error("文件不合法:" + err.toString());
} else {
return Helper.upload(type,name,files,data,"image");
}
} catch (Throwable e) {
return Result.error(e.getMessage());
}
}
提交后,文件服务器报错如下:
[WARN ] 23:50:38.075 org.eclipse.jetty.server.HttpChannel.handleException(HttpChannel.java:590) - /upload/image/user/f92364ee3f8043299f0e9ee6f3b9a377
java.lang.NoClassDefFoundError: org/nutz/mvc/RequestPath
at org.nutz.mvc.Mvcs.getRequestPathObject(Mvcs.java:232)
at org.nutz.mvc.Mvcs.getRequestPathObject(Mvcs.java:222)
at org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:91)
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:28)
at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
at org.nutz.mvc.WhaleFilter.doFilter(WhaleFilter.java:171)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1602)
at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:214)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:540)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1588)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1345)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:480)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1557)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1247)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:502)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:364)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:305)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:683)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: org.nutz.mvc.RequestPath
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 34 more
请指教