java map转Multipart/form

网友投稿 281 2022-12-07


java map转Multipart/form

我就废话不多说了,大家还是直接看代码吧!

public static String mapToTxt(Map fieldMap, Map 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 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 entity:fileMap.entrySet()) {

String name = "Content-Disposition: form-data; name=\""+fileName+"\"; filename=\""+entity.gethttp://Value().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 Test(HttpServletRequest request,HttpServletRequest response) throws Exception {

Map returnMap = new HashMap();

String a=request.getParameter("a");//取出form-data中a的值

String b=request.getParBxEdsZcfLameter("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 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();

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:postman中POST请求时参数包含参数list设置方式
下一篇:java模拟发送form
相关文章

 发表评论

暂时没有评论,来抢沙发吧~