NutzCN Logo
问答 关于UploadAdaptor的问题
发布于 3345天前 作者 xliaoong 2918 次浏览 复制 上一个帖子 下一个帖子
标签:

@POST
@Ok("json")
@At("/uploadimage")
@AdaptBy(type=UploadAdaptor.class, args={"${app.root}/WEB-INF/tmp/", "8192", "utf-8", "100", "10240000"})
public Map<String, String> uploadAvatar(@Param("file")TempFile tf, AdaptorErrorContext err) {
Map<String, String> params = new HashMap<>();
......

以上是我的方法实现,目前的问题是文件可以完整接收到 ${app.root}/WEB-INF/tmp/ 之下。比对过md5,没问题可以断定adapt的接收文件这一环是ok的,但是等回调到uploadAvatar,参数TempFile tf 在判断是否为null的时候,出现了该参数为null的情况,请问是什么原因导致的。在线等!比较捉急,谢谢大神!

19 回复

看日志, 里面有显示解析出的文件参数的名称是啥, 一般就是写错了

@wendal 我打开debug也没看到文件参数名称啊?@Param("file")TempFile tf 这个参数在传递的时候应该传递null进来了。可是为啥会穿入null呢?

肯定有的, 给你一段实例log

2015-10-27 11:39:31,137 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:92) DEBUG - Found mapping for [POST] path=/yvr/upload : YvrModule.upload(...)
2015-10-27 11:39:31,140 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:145) DEBUG - Get 'yvrModule'<class net.wendal.nutzbook.module.yvr.YvrModule>
2015-10-27 11:39:31,145 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:39) DEBUG - FastUpload : /yvr/upload
2015-10-27 11:39:31,148 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:54) DEBUG - info created
2015-10-27 11:39:31,148 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:60) DEBUG - Params map created - 1 params
2015-10-27 11:39:31,151 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:77) DEBUG - boundary:
------WebKitFormBoundarykdFdeuzQTQB0vH9c
2015-10-27 11:39:31,156 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:100) DEBUG - skip first boundary
2015-10-27 11:39:31,156 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:111) DEBUG - Reading...
2015-10-27 11:39:31,158 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) DEBUG - Upload File info: FilePath=[null],fieldName=[id]
2015-10-27 11:39:31,158 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) DEBUG - Found a param, name=[id] value=[WU_FILE_0]
2015-10-27 11:39:31,159 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) DEBUG - Upload File info: FilePath=[null],fieldName=[name]
2015-10-27 11:39:31,159 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) DEBUG - Found a param, name=[name] value=[Screenshot_2015-09-27-18-46-00.png]
2015-10-27 11:39:31,160 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) DEBUG - Upload File info: FilePath=[null],fieldName=[type]
2015-10-27 11:39:31,160 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) DEBUG - Found a param, name=[type] value=[image/png]
2015-10-27 11:39:31,161 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) DEBUG - Upload File info: FilePath=[null],fieldName=[lastModifiedDate]
2015-10-27 11:39:31,161 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) DEBUG - Found a param, name=[lastModifiedDate] value=[Mon Sep 28 2015 09:46:43 GMT+0800 (中国标准时间)]
2015-10-27 11:39:31,162 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) DEBUG - Upload File info: FilePath=[null],fieldName=[size]
2015-10-27 11:39:31,162 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:214) DEBUG - Found a param, name=[size] value=[71240]
2015-10-27 11:39:31,166 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) DEBUG - Upload File info: FilePath=[Screenshot_2015-09-27-18-46-00.png],fieldName=[file]
2015-10-27 11:39:31,166 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:137) DEBUG - Upload Info: name=Screenshot_2015-09-27-18-46-00.png,content_type=image/png
2015-10-27 11:39:31,178 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:230) DEBUG - ...Done 72023 bytes readed
2015-10-27 11:39:31,185 net.wendal.nutzbook.mvc.LogTimeProcessor.process(LogTimeProcessor.java:24) DEBUG - [POST]URI=/yvr/upload 200 45ms

上面的日志里面的这句话

2015-10-27 11:39:31,166 org.nutz.mvc.upload.FastUploading.parse(FastUploading.java:133) DEBUG - Upload File info: FilePath=[Screenshot_2015-09-27-18-46-00.png],fieldName=[file]

关键是最后面的 fieldName=xxxx, 取决与表单里面name的值

@wendal 麻烦您仔细看一下我的问题:文件我已经接收完毕了,我去目录 ${app.root}/WEB-INF/tmp/也看到了,但是回调进函数:uploadAvatar 的时候 @Param("file")TempFile tf 这个参数是null。我是纳闷为啥这里的参数是null,理论上来说不应该是null

贴出部分日志:[很明显已经接收done了]
2015-10-27 11:39:48 org.nutz.mvc.upload.FastUploading:
Upload File info: FilePath=[MG_0499.JPG],fieldName=[fileMG_0499.JPG]
2015-10-27 11:39:48 org.nutz.mvc.upload.FastUploading:
Upload Info: name=MG_0499.JPG,content_type=application/octet-stream
2015-10-27 11:39:48 org.nutz.mvc.upload.FastUploading:
...Done 7707061 bytes readed

@wendal
2015-10-27 11:39:48 org.nutz.mvc.impl.UrlMappingImpl:
Found mapping for [POST] path=/uploadimage : ImageReceiver.uploadAvatar(...)
2015-10-27 11:39:48 org.nutz.mvc.upload.UploadAdaptor:
Select Html4 Form upload parser --> /uploadimage
2015-10-27 11:39:48 org.nutz.mvc.upload.FastUploading:
FastUpload : /uploadimage
2015-10-27 11:39:48 org.nutz.mvc.upload.FastUploading:
info created
2015-10-27 11:39:48 org.nutz.mvc.upload.FastUploading:
Params map created - 1 params
2015-10-27 11:39:48 org.nutz.mvc.upload.FastUploading:
boundary:
-----------CCHTTPAPIFormBoundaryEEXX5448
2015-10-27 11:39:48 org.nutz.mvc.upload.FastUploading:
skip first boundary
2015-10-27 11:39:48 org.nutz.mvc.upload.FastUploading:
Reading...
2015-10-27 11:39:48 org.nutz.mvc.upload.FastUploading:
Upload File info: FilePath=[MG_0499.JPG],fieldName=[fileMG_0499.JPG]
2015-10-27 11:39:48 org.nutz.mvc.upload.FastUploading:
Upload Info: name=MG_0499.JPG,content_type=application/octet-stream
2015-10-27 11:39:48 org.nutz.mvc.upload.FastUploading:
...Done 7707061 bytes readed

目测是表单里面的input并没有加name呢

不然不应该出现这个吧

Upload File info: FilePath=[MG_0499.JPG],fieldName=[fileMG_0499.JPG]

并映射到一个叫"fileMG_0499.JPG"的属性

@wendal 然而之前是可以的,我这边做了一下重构之后发现有问题了

@wendal 函数名称,参数都没有变化,唯一变化的就是从a.java里头剪切到了b.java里头

跟我强调"XXX没变",这对我没意义,对解决这个问题也没有意义

解决这个问题还有另外一个思路, 单独写个html, 里面就一个表单, 上传, 对比差异

@wendal 我根本不是用html上传的,使用HttpURLConnection这种方式传输的

@xliaoong 所以, 里面格式不对咯, 传File的是, 直接弄了文件名的节奏, 而不是传"file"

还有一种方案, 如果不能改客户端

声明一个参数

public ....... upload(@Param(".."Map<String, Object> map, ........)) {
 // 然后遍历map里面的元素,找出类型为TempFile的value,就可以了
}

@wendal 最终搞定了,真是一番波折啊!可能还是没能完全领会设计想法,源码看的不够!多谢您的耐心解决

@wendal
日志:
Upload File info: FilePath=[MG_0499.JPG],fieldName=[fileMG_0499.JPG]

修改后:
public Object uploadAvatar(@Param("fileMG_0499.JPG") TempFile tf, AdaptorErrorContext err) {

原来写法:
public Map<String, String> uploadAvatar(@Param("file")TempFile tf, AdaptorErrorContext err) {

ps:感觉挺无语的 哈哈哈

@xliaoong ... 你改成这样,选另外一个文件就挂了吧

@wendal 木关系,起码我找到问题了,还好我这边需求暂时不会有批量上传的情况。到时候在考虑解决方案。

添加回复
请先登陆
回到顶部