关于nutz跨服务器上传文件
Posted 疯子加天才
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于nutz跨服务器上传文件相关的知识,希望对你有一定的参考价值。
是这样的,项目在一台服务器,文件要存储到另外一台服务器。
项目中功能使用form表单上传
@At
@AdaptBy(type = UploadAdaptor.class, args = { "${app.root}/WEB-INF/tmp" })
public String add(@Param("upload_file")TempFile f, HttpServletRequest request)
module里已经接收到f,怎么将f传到另外一台服务器,nutz有这样的上传封装吗?求指点
21 回复
public static String fileUploadReturnPath(TempFile file, String model)
{
try
{
Request req = Request.create("http://localhost:8083/uploadFile/image", METHOD.POST);
req.getParams().put("file", file.getFile());
Response resp = new FilePostSender(req).send();
if (!resp.isOK()) {
System.out.println("11");
} else {
System.out.println("22");
}
System.out.println(resp.getContent());
return null;
}
catch (Exception e)
{
logger.info("【文件上传失败】", e);
}
return null;
}
大神,帮忙看看,system的是11....
@yong9664 那你不是应该去看880
@wendal 8083那机器的日志吗
@wendal 没有日志...
@wendal tomcat
@wendal 完整上传代码,麻烦帮看下,项目服务器和文件报错服务器不在一起....
jsp:
<form action="<%=path%>/manager/add" name="myForm" id="myForm" method="post" enctype="multipart/form-data">
<input type="file" name="upload_file" id="upload_file" />
</form>
module:
@At
@AdaptBy(type = UploadAdaptor.class, args = { "${app.root}/WEB-INF/tmp" })
public String add(@Param("upload_file")TempFile f, HttpServletRequest request)
{
fileUploadReturnPath(f, "image");
}
上传到另外一台服务器:
public static String fileUploadReturnPath(TempFile file, String model)
{
try
{
Request req = Request.create("http://192.168.1.103:8083/uploadFile/"+ model, METHOD.POST);//文件存储服务器ip和tomcat端口
req.getParams().put("file", file.getFile());
Response resp = new FilePostSender(req).send();
if (!resp.isOK()) {
System.out.println("11");
} else {
System.out.println("22");
}
System.out.println(resp.getContent());
return null;
}
catch (Exception e)
{
logger.info("【文件上传失败】", e);
}
return null;
}
system打印11,代表失败了吧?
@wendal 哦哦,我试试改为debug看看日志
@wendal 日志么看出什么东西....
[2016-04-08 13:55:26,266] org.nutz.mvc.impl.UrlMappingImpl:92 DEBUG - Found mapping for [POST] path=/manager/add : MultimediaModule.add(MultimediaModule.java:126)
[2016-04-08 13:55:26,266] org.nutz.ioc.impl.NutIoc:153 DEBUG - Get ‘multimediaModule‘<class com.xll.module.manager.MultimediaModule>
[2016-04-08 13:55:26,267] org.nutz.mvc.upload.UploadAdaptor:187 DEBUG - Select html4 Form upload parser --> /nysq/manager/add
[2016-04-08 13:55:26,269] org.nutz.mvc.upload.FastUploading:39 DEBUG - FastUpload : /manager/add
[2016-04-08 13:55:26,271] org.nutz.mvc.upload.FastUploading:54 DEBUG - info created
[2016-04-08 13:55:26,272] org.nutz.mvc.upload.FastUploading:60 DEBUG - Params map created - 0 params
[2016-04-08 13:55:26,274] org.nutz.mvc.upload.FastUploading:77 DEBUG - boundary:
------WebKitFormBoundary0DAjRIVAe1vCfi82
[2016-04-08 13:55:26,278] org.nutz.mvc.upload.FastUploading:100 DEBUG - skip first boundary
[2016-04-08 13:55:26,278] org.nutz.mvc.upload.FastUploading:111 DEBUG - Reading...
[2016-04-08 13:55:26,281] org.nutz.mvc.upload.FastUploading:133 DEBUG - Upload File info: FilePath=[null],fieldName=[title]
[2016-04-08 13:55:26,281] org.nutz.mvc.upload.FastUploading:214 DEBUG - Found a param, name=[title] value=[aaa]
[2016-04-08 13:55:26,281] org.nutz.mvc.upload.FastUploading:133 DEBUG - Upload File info: FilePath=[null],fieldName=[description]
[2016-04-08 13:55:26,282] org.nutz.mvc.upload.FastUploading:214 DEBUG - Found a param, name=[description] value=[]
[2016-04-08 13:55:26,282] org.nutz.mvc.upload.FastUploading:133 DEBUG - Upload File info: FilePath=[苹果.jpg],fieldName=[multimedia_file]
[2016-04-08 13:55:26,282] org.nutz.mvc.upload.FastUploading:137 DEBUG - Upload Info: name=苹果.jpg,content_type=image/jpeg
[2016-04-08 13:55:26,291] org.nutz.mvc.upload.FastUploading:230 DEBUG - ...Done 3584 bytes readed
这是点击提交请求后的所有日志。
@wendal 名字“multimedia_file”没错的,我给module更改了下,方便你看的o(∩_∩)o
@yong9664 这属于自以为聪明,找打
以上是关于关于nutz跨服务器上传文件的主要内容,如果未能解决你的问题,请参考以下文章