是这样的,项目在一台服务器,文件要存储到另外一台服务器。
项目中功能使用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 回复
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....
@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 日志么看出什么东西....
[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
这是点击提交请求后的所有日志。