java map转multipart/form-kb88凯时官网登录

时间:2020-05-06
阅读:
免费资源网,https://freexyz.cn/

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

public static string maptotxt(map fieldmap, map filemap,string filename) throws exception{
		random random = new random();
		int j;
		string getline = "rn";
		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.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 test(httpservletrequest request,httpservletrequest response) throws exception {
     map returnmap = new hashmap();
    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 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();
    }
免费资源网,https://freexyz.cn/
返回顶部
顶部
网站地图