CXF Attachment接受文件流 (解决)

CXF文件上传解决 (解决一下问题)

   org.apache.cxf.interceptor.Fault: Invalid URL encoding: not a valid digit (radix 16): -17

   java.lang.RuntimeException: Invalid URL encoding: not a valid digit (radix 16): -17 

(CXF+Spring 的配置这里就不叙述了)

代码如下

   前段代码

         


  后端接口:

  

@POST
	@Path("/uploadCrawler")
	@Consumes({ MediaType.MULTIPART_FORM_DATA })
	boolean uploadCrawler(@Multipart(value = "<span style="color:#FF0000;">files</span>") Attachment file,
			@FormParam("crawlerName") String crawlerName, @FormParam("remark") String remark);

遇到问题情况:

 前期由于不懂。input 文件域 的name属性是自己定义的,然后@Multipart(value = "files")也是自己定义的,然后就会出现以上问题,通过大牛的知道,对源码的调试,可以看出 input的name属性必须为 files,这样CXF才不会将其变为String,才不会出现以上问题。


源码一下:(org.apache.cxf.jaxrs.utils.FormUtils)

 private static final String MULTIPART_FORM_DATA_TYPE = "form-data";  
    private static final String MAX_FORM_PARAM_COUNT = "maxFormParameterCount";  
    private static final String CONTENT_DISPOSITION_FILES_PARAM = "files";  


 public static void populateMapFromMultipart(MultivaluedMap<String, String> params,
                                                MultipartBody body,
                                                Message m,
                                                boolean decode) {
        List<Attachment> atts = body.getAllAttachments();
        checkNumberOfParts(m, atts.size());
        for (Attachment a : atts) {
            ContentDisposition cd = a.getContentDisposition();
            if (cd != null && !MULTIPART_FORM_DATA_TYPE.equalsIgnoreCase(cd.getType())) {
                continue;
            }
            String cdName = cd == null ? null : cd.getParameter("name");
            String contentId = a.getContentId();
            String name = StringUtils.isEmpty(cdName) ? contentId : cdName.replace("\"", "").replace("'", "");
            if (StringUtils.isEmpty(name)) { 
                throw ExceptionUtils.toBadRequestException(null, null);
            }
            if (CONTENT_DISPOSITION_FILES_PARAM.equals(name)) {
                // this is a reserved name in Content-Disposition for parts containing files
                continue;
            }
            try {
                String value = IOUtils.toString(a.getDataHandler().getInputStream());
                params.add(HttpUtils.urlDecode(name),
                           decode ? HttpUtils.urlDecode(value) : value);
            } catch (IllegalArgumentException ex) {
                LOG.warning("Illegal URL-encoded characters, make sure that no "
                    + "@FormParam and @Multipart annotations are mixed up");
                throw ExceptionUtils.toInternalServerErrorException(ex, null);
            } catch (IOException ex) {
                throw ExceptionUtils.toBadRequestException(null, null);
            }
        }
    }


    

  


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值