J2ME 文件上传 实例(FileConnector链接文件系统,非RMS)
2009-07-15 11:03
246 查看
客户端:
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();
}
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文件系统运用(五) 从本地上传文件至服务器(支持同时上传多个文件)
- flume实例二、监听目录日志上传到HDFS文件系统
- J2me文件上传实例(Use Fileconnector not rms)转来的
- j2me开发的程序,关闭后rms,没有保存,文件系统找不到文件
- 利用resteasy框架构建rest webservice----第三波:实现文件上传---非form提交方式(实例、教程)
- php实例多文件上传实例代码
- unix文件系统中的软链接、硬链接
- hadoop上传文件功能实例代码
- 18、SSM框架-SpringMVC 实例文件上传(5)
- SSIS的文件系统任务实例(zz)
- jquery实例文件无刷新上传代码
- Java中用Socket实现HTTP文件上传实例
- 使用百度webuploader插件进行多文件类型分片上传实例
- (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
- 使用FileZilla向linux系统上传文件
- asp.net 文件上传实例汇总
- SSM框架-SpringMVC 实例文件上传下载
- JQuery插件ajaxfileupload.js异步上传文件实例
- unix文件系统:链接与文件
- okhttp上传文件实例