J2ME文件上传实例(FileConnector链接文件系统,非RMS)

来源:计算机等级考试    发布时间:2012-08-29    计算机等级考试视频    评论

  代码如下:
  客户端:
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.io.ByteArrayOutputStream;
  import javax.microedition.io.*;
  import javax.microedition.midlet.MIDlet;
  import javax.microedition.midlet.MIDletStateChangeException;
  import javax.microedition.io.file.*;
  public class HttpConnect extends MIDlet {
  public HttpConnect() {
  // TODO Auto-generated constructor stub
  }
  protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub
  }
  protected void pauseApp() {
  // TODO Auto-generated method stub
  }
  protected void startApp() throws MIDletStateChangeException{
  String url = "http://192.168.0.114:8080/EngineService/sync";
  try{
  byte[] data = null;
  if( System.getProperty("microedition.io.file.FileConnection.version" ) != null ){
  try {
  FileConnection fc = (FileConnection)Connector.open("file:///root1/test.png", Connector.READ_WRITE);
  //fc.create();
  InputStream in = fc.openInputStream();
  fc.close();

  编辑特别推荐:

  全国计算机等级考试(等考)指定教材

  全国计算机等级考试学习视频

  全国计算机等级考试网上辅导招生

  全国计算机等级考试时间及科目

  考试大教育全国计算机等级考试在线测试平台

  全国计算机等级考试资料下载

  全国计算机等级考试论坛

  如何应付二级考试中的公共基础知识

  全国计算机等级考试上机考试应试技巧

  ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
  byte[] tmp = new byte[4096];
  int n;
  while ((n = in.read(tmp)) != -1) {
  out.write(tmp, 0, n);
  out.flush();
  }
  in.close();
  out.close();
  data = out.toByteArray();
  }catch (Exception e) {
  e.printStackTrace();
  }
  }
  HttpConnection sc = (HttpConnection)Connector.open( url, Connector.READ_WRITE, true );
  sc.setRequestMethod( HttpConnection.POST );
  sc.setRequestProperty("Content-Type", "application/octet-stream");
  sc.setRequestProperty("Content-Length", String.valueOf(data.length));
  OutputStream output = sc.openOutputStream();
  output.write(data);
  output.flush();
  output.close();
  }catch(Exception e){
  System.out.println("Error:"+e);
  }
  }
  }
  服务器端:
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException
  {
  InputStream in = req.getInputStream();
  byte[] tmp = new byte[4096];
  int size=0;
  File f = new File("c://",System.currentTimeMillis()+".png");
  DataOutputStream o = new DataOutputStream(new FileOutputStream(f));
  int len=0;
  while((len = in.read(tmp))!= -1){
  o.write(tmp,0,len);
  o.flush();
  size+=len;
  }
  o.close();
  }

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答