List<MultipartFile>序列化成String和将序列化成String的文件反序列化
作者:互联网
场景来源:无法给Dubbo接口传参数,List<MultipartFile> files不支持。
解决方法:Base64将文件序列化为String。Dubbo再解析成List。
List<MultipartFile>转成String:
public String uploadFj(List<MultipartFile> files){ try { if(files==null||files.size()==0){ throw new RuntimeException("附件为空"); } List<Map<String,Object>>list =new ArrayList<Map<String,Object>>(); for(MultipartFile f:files){ Map<String,Object>map=new HashMap<String,Object>(); String filename = f.getOriginalFilename(); byte[] bytes = f.getBytes(); map.put("fileName", filename); map.put("fileByte", Base64.encodeBase64String(bytes)); list.add(list.size(),map); } String jsonfiles = JSONObject.toJSONString(list); return jsonfiles; } catch (Exception e) { throw new RuntimeException(e.getMessage()); } }
从序列化后的String提取数据:
public void formatUpload01(String fid, String userid, String username, String businesstag, String fileobj){ try { List<Map> attachList=JSONObject.parseArray(fileobj,Map.class); if(attachList!=null && attachList.size()>0){ String fileid = null; for(Map m:attachList){ byte[] fileByte=Base64.decodeBase64(m.get("fileByte")==null?"":m.get("fileByte").toString()); String fileName=m.get("fileName").toString(); } } } catch (Exception e) { throw new RuntimeException(e.getMessage()); } }
标签:files,String,List,化成,attachList,序列,new,null 来源: https://www.cnblogs.com/whirlwind/p/12179860.html