发布于 578天前 作者 yong9664 770 次浏览 复制 上一个帖子 下一个帖子
标签: 无
是这样的,项目在一台服务器,文件要存储到另外一台服务器。
项目中功能使用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 回复
如何使用Sender.create(Request request).send()方法传文件
FilePostSender
前提是你存放文件的那个服务器要能接收 HTTP 形式的文件上传
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那机器的日志吗
@kerbores
能接收Http形式的文件上传,怎么设置啊?这个还真没注意...
@wendal 没有日志...
那8083是个啥程序
@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,代表失败了吧?
接收的那端总会有点日志的,log4j的话全配置为debug
@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
这是点击提交请求后的所有日志。
接收的名字应该是 @Param("multimedia_file") TempFile f
上面的日志是代表已经读取到文件了, 就看入口方法怎么接收了
但好奇怪为啥是 "multimedia_file" , 你代码里面写的是这个名字??? FilePostSender应该不会生成这个名字
@wendal 名字“multimedia_file”没错的,我给module更改了下,方便你看的o(∩_∩)o
@yong9664 这属于自以为聪明,找打
最终结果就目标服务根本没有对应的入口方法。