httpdownloader.java
来自「相册是J2me MIDLet 的一个demo程序」· Java 代码 · 共 94 行
JAVA
94 行
/*
*
* Filename: HttpDownloader.java
* - contains definition of the class HttpDownloader, which has a static method for downloading byte data
* from specified URL. The method is called from DownloadFromURLForm object.
*
* Application Name: Image Album Demo
* Version: 2.0
* Release Date: 27th September, 2002
* Author: Edmund Wong, Application Engineer, Metrowerks Hong Kong
*
*
* (c) Copyright. 2002. Metrowerks Corp. ALL RIGHTS RESERVED.
* This code is provided "AS IS" without warranty of any kind and subject to Metrowerks Sample Application
* End User License Agreement. NO EXTERNAL DISTRIBUTION OR COMMERCIALIZATION OF THE SOFTWARE IS PERMITTED
* EXCEPT BY WRITTEN AGREEMENT OF METROWERKS."
*/
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
/**
* Class HttpDownloader for downloading byte data from specified URL
*/
public class HttpDownloader {
/**
* HttpDownloader Constructor
*/
public HttpDownloader () {
}
/**
* Static method for downloading byte data from specified URL
*/
public static byte[] httpDownload (String url) throws IOException {
byte[] downloadedData = null; // byte data buffer
String responseMessage = null; // storing repsonse message from server
HttpConnection httpConnection = null;
DataInputStream dataInputStream = null;
try {
httpConnection = (HttpConnection)Connector.open(url);
//httpConnection.setRequestMethod(HttpConnection.GET);
// for testing
responseMessage = httpConnection.getResponseMessage();
System.out.println(httpConnection.getResponseCode());
System.out.println(responseMessage);
dataInputStream = new DataInputStream(httpConnection.openDataInputStream());
downloadedData = new byte[(int)httpConnection.getLength()];
dataInputStream.readFully(downloadedData);
}
finally { //close the dataInputStream and httpConnection
if (dataInputStream != null) dataInputStream.close();
if (httpConnection != null) httpConnection.close();
}
return downloadedData;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?