J2ME 文件下载实例 jar包为例
package com.wanchong;
/**
* 作者:万冲
* QQ:569845790
* Mail:wanchong998@qq.com
*/
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class DMIDlet extends MIDlet{
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 {
// TODO Auto-generated method stub
Display.getDisplay(this).setCurrent(new DCanvas());
}
}
package com.wanchong;
/**
* 作者:万冲
* QQ:569845790
* Mail:wanchong998@qq.com
*/
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class DCanvas extends Canvas implements Runnable{
Image image;
public DCanvas()
{
setFullScreenMode(true);
repaint();
//启动下载线程
new Thread(this).start();
}
String downFilecon=null;
protected void paint(Graphics g) {
// TODO Auto-generated method stub
g.setColor(0x0);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xff0000);
g.drawString("正在下载中....", 0, 0, 0);
if(downFilecon!=null){
g.setColor(0xff0000);
g.drawString(downFilecon, 50, 50, 0);
}
}
public void run() {
// TODO Auto-generated method stub
try {
connect();
} catch (Exception e) {
e.printStackTrace();
}
}
String url = "http://www.xxx.com/wandown/wan_down_test.jar";
public void connect() throws Exception
{
HttpConnection http = null;
InputStream is = null;
OutputStream os = null;
byte[] data = null;
try
{
http = getHttpConnection(url);
int code = http.getResponseCode();
// 返回200 表示连接成功
if (code == HttpConnection.HTTP_OK)
{
is = http.openInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch = -1;
while ((ch = is.read()) != -1) {
baos.write(ch);
//System.out.println(ch);
}
data = baos.toByteArray();
}
else
{
System.out.println("网络连接错误");
}
//下载图片数据完成后创建图片
if(data != null)
{
// image = Image.createImage(data, 0, data.length);
//repaint();//重绘
int t = this.saveFile("file://localhosst/root1/wanchong.jar",data, true);
downFilecon = "下载完成!";
}
}
finally
{
if(os != null)
{
os.close();
os = null;
}
if(is != null)
{
is.close();
is = null;
}
if(http != null)
{
http.close();
http = null;
}
}
}
/**
* 取得一个URL的连接
* @param url
* @return
* @throws Exception
*/
private HttpConnection getHttpConnection(String url) throws Exception
{
HttpConnection http = (HttpConnection) Connector.open(url);
http.setRequestProperty("Content-Type", "application/octet-stream");//get
http.setRequestMethod(HttpConnection.GET);
return http;
}
/**
* 保存文件
* @param path 文件路径
* @param fileData 文件数据
* @param overrite 是否要强制覆盖
* @return 1:成功写入文件 -1:文件已经存在没有重写 0:写入文件时出错
*
*/
public int saveFile(String path, byte[] fileData, boolean overrite) {
try {
FileConnection fc = (FileConnection) (Connector.open(path));
if (fc.exists()) {
if (overrite) {
fc.delete(); //如果文件已经存在,先删除
fc.create();
fc.setWritable(true);
OutputStream os = fc.openOutputStream();
os.write(fileData,0,fileData.length);
os.close();
return 1;
} else
return -1;
} else {
fc.create();
fc.setWritable(true);
OutputStream os = fc.openOutputStream();
os.write(fileData,0,fileData.length);
os.close();
return 1;
}
} catch (Exception e) {
System.out.println("saveFileErr:" + e.getMessage());
e.printStackTrace();
return 0;
}
}
/**
* 获取路径
* @return
*/
public String[] DeviceGetRoots() {
Enumeration enumRoot = FileSystemRegistry.listRoots();// 返回根目录组成的序列
Vector list = new Vector();
while (enumRoot.hasMoreElements()) {
list.addElement(enumRoot.nextElement());
}
String[] ret = null;
if (list.size() > 0) {
ret = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
ret[i] = (String) list.elementAt(i);
}
}
return ret;
}
}