技术中心

这里象征着我们的态度和能力

>J2ME post方式上传文件
发布者:中国IT实验室    信息来源:中国IT实验室    发布时间:2012-01-01      浏览次数:6627
分享到:

新浪微博

腾讯微博

QQ空间

豆瓣网

QQ好友

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

  public void temp() {

  String name = po.getName(); // file name

  String str = po.getStr()+"/plugins/j2me"; // url

  byte[] raw = snap.getData(); // file data

  String cmd = "temppic";

  String auth = po.getUserId();

  String albumId = po.getUserId();

  String type = po.getType();

  String remarks = po.getDemo();

  String[] props = { "cmd","auth","albumId", "public", "remarks" }; //property name

  String[] values = { cmd, auth, albumId, type, remarks }; // property value

  try {

  String BOUNDARY = "---------7d4a6d158c9"; // separate line

  String MULTIPART_FORM_DATA = "multipart/form-data";

  StringBuffer sb = new StringBuffer();

  for (int i = 0; i < props.length; i++) { // send each property

  sb = sb.append("--");

  sb = sb.append(BOUNDARY);

  sb = sb.append(" ");

  sb = sb.append("Content-Disposition: form-data; name=""+ props[i] + "" ");

  sb = sb.append(URLEncoder.encode(values[i],"UTF-8"));

  sb = sb.append(" ");

  }

  sb = sb.append("--");

  sb = sb.append(BOUNDARY);

  sb = sb.append(" ");

  sb = sb.append("Content-Disposition: form-data;name="file";filename=""+ name + ".jpg" ");

  sb = sb.append("Content-Type: application/octet-stream ");

  byte[] data = sb.toString().getBytes();

  byte[] end_data = (" --" + BOUNDARY + "-- ").getBytes();

  HttpConnection hc = (HttpConnection) Connector.open(str,Connector.READ_WRITE); // set HTTP head:

  hc.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);

  hc.setRequestProperty("Content-Length", String.valueOf(data.length+ raw.length + end_data.length)); //

  //        hc.setRequestProperty("Connection", "keep-alive");

  hc.setRequestMethod(HttpConnection.POST); // out: OutputStream                                            //

  output =hc.openOutputStream();

  output.write(data);

  output.write(raw, 0, raw.length);

  output.write(end_data);

  int cah = hc.getResponseCode();

  if (cah != HttpConnection.HTTP_OK) {

  System.out.println(cah);

  return;

  }

  output.close();

  //receive(hc);

  hc.close();

  } catch (IOException e) {

  } catch (SecurityException e) {

  } catch (NullPointerException e) {

  }

  }

4000-880-989
(24小时热线)
联系客服
微信公众号

官方公众号

小程序

©2008-2022 CORPORATION ALL Rights Reserved. 昆明奥远科技有限公司版权所有 滇ICP备09003328号-1 滇公网安备 53011102000818号
昆明那家网络公司好,新媒体运营,网站优化,网络推广,网站建设,网页设计,网站设计,网站推广,云南网站公司,昆明新媒体公司,云南网红主播,昆明SEO公司,昆明网站建设,昆明网络推广,昆明网站优化,昆明网站推广,红河网站建设,大理网络公司,曲靖网络公司,丽江网站设计,昭通网络公司,保山大数据服务,智慧高速建设,智慧校园服务,云南IDC服务商,网络安全测评,等保测评,网站关键词排名优化服务,服务客户尽超2000余家,一切尽在奥远科技,服务电话:13888956730