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);
}
}
}