AD

J2ME breakpoint download

package downfile;

import java.io. *;

import javax.microedition.io .*;
import javax.microedition.io.file .*;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

/ **
* <p> Title: </ p>
*
* <p> Description: </ p>
*
* <p> Copyright: Copyright (c) 2009 </ p>
*
* <p> Company: </ p>
*
* @ Author new soul
* @ Version 1.0
* /
public class Load extends Thread (
private String url; / / network file's address
private String path; / / save the path of the local
private long loadsize = 0; / / download file size of the
private long size = 0; / / downloaded file size
private MIDlet midlet;

/ **
* Download Thread
* @ Param urlstr String Network File Address
* @ Param pathstr String path for local storage
* @ Param mid MIDlet displays the current main class
* Cell phone address file: / / / E: / Computer Emulator address file: / / localhost/root1 /
* /
public Load (String urlstr, String pathstr, MIDlet mid) (
this.url = urlstr;
this.path = pathstr;
this.midlet = mid;
Thread thread = new Thread (this);
thread.start ();
)

public void run () (
try (
LoadFile (url, path);
Alert alert = new Alert ( "Download Alert", "download has been completed!", Null, AlertType.ALARM);
alert.setTimeout (3000);
Display.getDisplay (midlet). SetCurrent (alert);

) Catch (Exception ex) (
ex.printStackTrace ();
)
)

/ **
* Download the file breakpoints
* @ Param urladress String network file's address
* @ Param pathstr String file storage local address
* /
public void LoadFile (String urladress, String pathstr) (
HttpConnection conn = null;
FileConnection fc = null;
InputStream is = null;
OutputStream os = null;

/ / Interception of file names
String [] urlstr = Tool.Split (urladress, "/");
String filename = urlstr [urlstr.length - 1]. ToString ();

try (
conn = (HttpConnection) Connector.open (url.trim ());
conn.setRequestMethod (HttpConnection.POST);

/ / Cell phone address file: / / / E: / Computer Add file: / / localhost/root1 /
fc = (FileConnection) Connector.open (pathstr + filename, Connector.READ_WRITE);
if (fc.exists ())
this.size = fc.fileSize ();
conn.setRequestProperty ( "RANGE", "bytes =" + size + "-"); / / HTTP
setLoadsize (conn.getLength ());// download the file length of the

is = conn.openInputStream ();
int ch;
byte [] b = new byte [1024];

if (! fc.exists ()) (/ / file does not exist
fc.create ();
os = fc.openOutputStream ();
)

else (/ / the file already exists
fc.truncate (size);
os = fc.openOutputStream (size);
)

while ((ch = is.read (b))! = -1) (
os.write (b, 0, ch);
)

fc.close ();
os.close ();
is.close ();
conn.close ();
) Catch (Exception e) (
e.printStackTrace ();
)

)

public void setLoadsize (long loadsize) (
this.loadsize = loadsize;
)

public long getLoadsize () (
return loadsize;
)

public long getSize () (
return size;
)

)

This article comes from CSDN blog, reproduced please indicate source: **blog.csdn**/laigb/archive/2009/03/15/3992367.aspx
标签: lt, quot, import java, javax, public void, string url, file storage, interception, inputstream, file names, conn, string path, url path, midlet, string filename, cell phone, computer emulator, phone address
分类: Mobile
时间: 2010-03-29

相关文章

  1. http response Last-Modified and ETag

    Basics 1) What is "Last-Modified"? In the browser first requests a particular URL, the server side o ...
  2. To determine whether a page is updated

    Basics 1) What is "Last-Modified"? In the browser first request a particular URL, the server-side re ...
  3. usage of common commands linux article reprint 100

    Usually use linux, I have a habit of is to meet, more useful and easy to forget the order, into a text file, w ...
  4. linux common commands use text reproduced 100

    Usually use linux, I have a habit is to encounter, more useful and easy to forget the command into a text file ...
  5. Internet multi-threaded download Android breakpoint

    Internet multi-threaded download Android breakpoint 1. Create Android projects www.AFITY.com Project name: Mul ...
  6. FTP download breakpoint problem solving

    FtpClient fc = new FtpClient (strRemoteAddr, 21); / / Connection FTP fc.binary (); / / This is a very critical ...
  7. Java multi-threaded download breakpoint

    Download the file can use multiple threads to download files more quickly, multi-threaded download document wa ...
  8. Using Eclipse to debug J2ME program

    Eclipse is the development of J2ME program using one of the main development tool, while the capacity of the d ...
  9. Java Products and software download

    This column provides hundreds of Java products and software download site link and brief introduction develope ...
  10. HADOOP-HDFS DataNode transform into storage / download server

    Hadoop defaults to return to host the means to give the client response. The FSNamesystem end, achieved with j ...
  11. A variety of open source database optimized J2me

    J2ME own UI is not too beautiful, to use is not very concerned, in order to solve this problem, a lot of poten ...
  12. J2ME using the Socket access security through HTTPS access points cmwap

    The problem is in my J2ME version of XHTML browser upgrade when they were introduced, I have tried many ways, ...
  13. MTJ 1.0 WTK 2.5.2_01 unable to debug J2ME applications

    Original Published time :2009-08-20 Originally wanted to write a simple J2ME program, the results of happy, an ...
  14. J2ME experience summary of the Proguard confused overflow

    J2ME experience summary of the Proguard confused overflow by: hunhun1981 From: http://blog.csdn.net/hunhun1981 ...
  15. Eclipse + EclipseME + WTK to build J2ME development environment (notes)

    0, download unzip Eclipse 3.3 (already installed before) and install the Chinese language pack: 1) Eclipse: ht ...
  16. java.C # Some data download address many of the very wide

    Hotel Management System (absolute classic) http://www.ibeifeng.com/read-htm-tid-10774.html&u=64404 [C #] s ...
  17. Experience of J2ME confusion overflow Proguard

    J2ME experience summary of Proguard overflow of confusion: hunhun1981 From: http://blog.csdn.net/hunhun1981/ I ...
  18. J2ME cmwap access point by using the Socket Access Security HTTPS

    The problem is in my J2ME version of XHTML browser upgrade when they were introduced, I tried many ways, made ...
  19. Reposted elsewhere: J2ME basic knowledge of the characteristics of a

    1, J2ME Java basic knowledge required in Most people now are starting from scratch learning J2ME, J2ME's time ...