📄 httpdownloader.java
字号:
/*
*
* 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -