您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要为大家展示了java map如何转Multipart/form-data类型,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。
我就废话不多说了,大家还是直接看代码吧!
public static String mapToTxt(Map<String,String> fieldMap, Map<String,File> fileMap,String fileName) throws Exception{ Random random = new Random(); int j; String getLine = "\r\n"; String fileType = "Content-Type: application/octet-stream"; String doubleBar = "--"; biaoshi = "----WebKitFormBoundary"; StringBuffer sb = new StringBuffer(); for(int i = 0; i < 16;i++){ j = random.nextInt(MULTIPART_CHARS.length-2)+2; sb.append(MULTIPART_CHARS[j]); } biaoshi = biaoshi + sb.toString(); StringBuffer stringBuffer = new StringBuffer(); for (Map.Entry<String,String> entity:fieldMap.entrySet()) { String name = "Content-Disposition: form-data; name=\""+entity.getKey()+"\""; stringBuffer.append(doubleBar+biaoshi); stringBuffer.append(getLine); stringBuffer.append(name); stringBuffer.append(getLine); stringBuffer.append(getLine); stringBuffer.append(entity.getValue()); stringBuffer.append(getLine); } for (Map.Entry<String,File> entity:fileMap.entrySet()) { String name = "Content-Disposition: form-data; name=\""+fileName+"\"; filename=\""+entity.getValue().getName()+"\""; stringBuffer.append(doubleBar+biaoshi); stringBuffer.append(getLine); stringBuffer.append(name); stringBuffer.append(getLine); stringBuffer.append(fileType); stringBuffer.append(getLine); stringBuffer.append(getLine); File f = entity.getValue(); FileInputStream fileInputStream = new FileInputStream(f); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte by[] = new byte[1024]; int k = 0; while ((k=fileInputStream.read(by))!=-1){ byteArrayOutputStream.write(by,0,k); } by = byteArrayOutputStream.toByteArray(); for(int i = 0; i < by.length; i++){ stringBuffer.append(by[i]); } stringBuffer.append(getLine); } stringBuffer.append(doubleBar+biaoshi+doubleBar); return stringBuffer.toString(); }
补充知识:java 如何取出传参数格式为form-data中的值
public Map<String, Object> Test(HttpServletRequest request,HttpServletRequest response) throws Exception { Map<String, String> returnMap = new HashMap<String, String>(); String a=request.getParameter("a");//取出form-data中a的值 String b=request.getParameter("b");//取出form-data中a的值 //取出form-data中的二进制字段 MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest) request; MultipartFile multipartFile = multipartRequest.getFile("file");//file是form-data中二进制字段对应的name System.out.println(multipartFile.getSize()); Map<String, Object> resultMapsReturn = new HashMap<>(); String imagePath="C:\\Users\\win\\Desktop\\1.jpg"//把取出来的二进制保存图片到本地 if(multipartFile.getSize()<=0){ resultMapsReturn.put("resultcode", "0"); resultMapsReturn.put("msg", DisWebConst.ERROR_TITLE); }else{ InputStream is = multipartFile.getInputStream(); OutputStream out = new FileOutputStream(imagePath); IOUtils.copy(is, out); is.close(); out.close(); }
以上就是关于java map如何转Multipart/form-data类型的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。