J2me文件上传实例(Use Fileconnector not rms)转来的
来源:互联网 发布:英文ubuntu 中文输入法 编辑:程序博客网 时间:2024/06/14 03:12
代码如下:
客户端:
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();
}
- J2me文件上传实例(Use Fileconnector not rms)转来的
- J2ME 文件上传 实例(FileConnector链接文件系统,非RMS)
- J2ME:rms的排序
- J2ME:rms的排序
- rms 文件上传
- J2ME FileConnector相关 暂不理解
- J2ME的RMS读写实现
- J2ME:rms的事件监听器
- J2ME:rms的条件过滤
- J2ME:rms的事件监听器
- j2me:rms的条件过滤
- J2ME中RMS的使用解析(转自cuilichen的专栏)
- J2ME RMS
- j2me上传文件的问题
- J2ME中RMS的使用解析
- J2ME中RMS的使用解析
- J2ME 程序中RMS的一些问题
- 学习j2me rms写的一点东西
- 学习C#基础知识(七)
- Ubuntu 10.04配置Apache+PHP+MySQL[转自:Ubuntu社区 作者:little樟]
- CopyOnWriteArraySet
- 英文中i.e.,e.g.,etc.的正确用法
- ArrayBlockingQueu
- J2me文件上传实例(Use Fileconnector not rms)转来的
- 一个Java程序员应该掌握的10项技能
- ArrayBlockingQueue
- 仿佛他刚从旧社会过来
- 使用线程创建的三种方法实现j2me联网(PS:我想做android!NND!)
- AtomicInteger
- SQL Select中的in和exists的区别
- PHP 控制电脑重启 关机
- 对EJB3 Stateless和Stateful的认识