欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java map转Multipart/form-data类型body实例

程序员文章站 2023-02-17 16:51:28
我就废话不多说了,大家还是直接看代码吧!public static string maptotxt(map fieldmap, map

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

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类型body实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。