SpringMVC 接收方法的參數自定義轉換,下面例子實現客戶端發送圖片的Base64數據,服務器轉換爲自定義的對象。java
1. 首先,須要配置自定義的轉換器。web
<mvc:annotation-driven> <mvc:argument-resolvers> <bean class="com.mumu.support.spring.Base64ToFileConverter"></bean> </mvc:argument-resolvers> </mvc:annotation-driven>
2. 添加註解RequestBase64和實體類Base64File。並實現Base64ToFileConverter(實現org.springframework.web.method.support.HandlerMethodArgumentResolver接口)spring
1) 註解RequestBase64服務器
@Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RequestBase64 { }
2) 實體類Base64Filemvc
public class Base64File { private String imageType; private byte[] content; public Base64File() { } public Base64File(String imageType, byte[] content) { super(); this.imageType = imageType; this.content = content; } public boolean isEmpty() { return ArrayUtils.isEmpty(content); } public long getSize() { return content != null? content.length:0L; } public byte[] getBytes() throws IOException { return content; } public String getImageType() { return imageType; } public InputStream getInputStream() throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(content); return bais; } public void transferTo(File dest) throws IOException, IllegalStateException { if(isEmpty()){ return; } try(FileOutputStream fos = new FileOutputStream(dest);){ fos.write(content, 0, content.length); } } }
3) Base64ToFileConverterapp
public class Base64ToFileConverter implements HandlerMethodArgumentResolver{ private static Pattern pattern = Pattern.compile("data:([^;]*);base64,(.+)", Pattern.CASE_INSENSITIVE); public Base64ToFileConverter(){ } @Override public boolean supportsParameter(MethodParameter parameter) { return parameter.hasParameterAnnotation(RequestBase64.class); } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { String[] values = webRequest.getParameterValues(parameter.getParameterName()); if(ArrayUtils.isNotEmpty(values)){ if(parameter.getParameterType().isAssignableFrom(Base64File.class)){ return toContent(values[0]); }else if(parameter.getParameterType().isAssignableFrom(List.class)){ List<Base64File> list = new ArrayList<>(values.length); for(String value : values){ list.add(toContent(value)); } return list; }else if(parameter.getParameterType().isArray()){ Base64File[] bfs = new Base64File[values.length]; int index = 0; for(String value : values){ bfs[index++] = toContent(value); } return bfs; } } return null; } private Base64File toContent(String value){ sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder(); try { Matcher matcher = pattern.matcher(value); if(matcher.find()){ String imageType = matcher.group(1); String content = matcher.group(2); //Base64解碼 byte[] b = decoder.decodeBuffer(content); return new Base64File(imageType, b); } return null; }catch(Exception e){ throw new I18NIllegalArgumentException(e); } } public static void main(String[] args) { String value = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAeCAIAAACwp+nIAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADxSURBVEhL7ZU7DoMwEESXVNBRUsKtuAHcgmNAyS3gZtCRER5FkYw/66AUUV4BK2OeZmWxZMdxyN08eL+VH5Cu69o0TeYAj+Z55lYbHNQldV1zh4M8z7nVwpm06zpWDvZ9Z2VDeRzjOPK1E65aKKTLspRlSZ9I3/d8YKGQVlVFn8i2bVy9Ilb63rgnoyFW+orZti2X3ERJ32P6GzdESVUxQViqjQnCUm1MEJAmxAQBaUJMEJj8GEimQExMEFMH8c3TaZpYnTOJVQwm8CVpvQNf+0VRmPmm6h342h+GAVd86brecRKepMl898f3CX/p3Yg8AZMYt/pV+plfAAAAAElFTkSuQmCC"; Matcher matcher = pattern.matcher(value); if(matcher.find()){ String imageType = matcher.group(1); String content = matcher.group(2); System.out.println(imageType + " " + content); } } }
3. Controller中方法實現ide
@RequestMapping(value = "/testing", method = RequestMethod.POST) @ResponseBody public ResultVO testing(@RequestBase64 Base64File file, @RequestBase64 List<Base64File> file2) { try { System.out.println(Arrays.toString(file.getBytes())); System.out.println("============================="); file2.stream().forEach(new Consumer<Base64File>() { @Override public void accept(Base64File t) { try { System.out.println(Arrays.toString(t.getBytes())); } catch (IOException e) { e.printStackTrace(); } } }); file.transferTo(new File("d:/test.jpg")); } catch (IOException e) { e.printStackTrace(); } return ResultVO.successVO(); }
注意事項: 參數前面不能加@RequestParam,不然org.springframework.web.method.annotation.RequestParamMethodArgumentResolver會處理參數解析this