📄 getfiles_form.java
字号:
package connectpacket;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.io.*;//yao yong dao httpconnection
import java.io.*;//yao yong dao inputstream outputstream
import javax.microedition.lcdui.*;
public class GetFiles_Form extends Form implements CommandListener {
private GetFiles_MIDlet gfm;
private Command exitCommand;
private HttpConnection http_con=null;
private InputStream is=null;
private OutputStream os=null;
private Image img1=null;
private Image img2=null;
public GetFiles_Form(String str_URL,GetFiles_MIDlet gfm) {
super("HttpGetFiles_Form");
// TODO 自动生成构造函数存根
this.gfm=gfm;
exitCommand=new Command("Exit",Command.EXIT,1);
this.addCommand(exitCommand);
this.setCommandListener(this);
try
{
if(http_con==null)
{
http_con=(HttpConnection)javax.microedition.io.Connector.open(str_URL);
http_con.setRequestMethod(HttpConnection.GET);
System.out.print("http_con::");
System.out.println(http_con);
//http_con.setRequestProperty(HttpConnection., arg1)
}
if(is==null||os==null)
{
is=http_con.openInputStream();
//int length=is.available();
//os=http_con.openOutputStream();
System.out.print("is::");
System.out.println(is);
System.out.print("is.length::");//is.length=buf.length
//System.out.println(length);//is.length=buf.length
}
int length=is.available();
byte[]buf=new byte[length];
is.read(buf,0,buf.length);
////当然我么也可以采取简洁转移法,就是假如我们无法得到该流的大小,
//那我们就先把该流写进一个字节数组输出流中,进而见解得到一个该输入流的大小,
//我们称这种见解方法为字节分流.....
/*int byte_code=0;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
while((byte_code=is.read())!=-1)
{
baos.write(byte_code);
//baos可以直接向字节数组输出流中写入一个整形或者字节数组
}
byte buf[]=baos.toByteArray();*/
System.out.print("buf.length::");//is.length=buf.length
System.out.println(buf.length);//is.length=buf.length
//ByteArrayInputStream bis=new ByteArrayInputStream(buf);
//DataInputStream dis=new DataInputStream(bis);
//System.out.println(dis.readUTF());
//is.read(buf,0, buf.length);
//既然可以从输入流中判断字节码的长度,在这里我们尝试以下,
//根据它的长度直接生成一个字节数组缓存区,来读去输入流中的字节码//
/*int length=is.available();
byte img_buf[]=new byte[1688];
is.read(img_buf,0,img_buf.length);
img2=Image.createImage(img_buf,0, img_buf.length);
this.append(img2);
//实验结果是不行的,但不知道为什么会这样?????????
*/
img1=Image.createImage(buf,0, buf.length);
this.append(img1);
//
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(is!=null)
{
is.close();
}
if(http_con!=null)
{
http_con.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public GetFiles_Form(String arg0, Item[] arg1) {
super(arg0, arg1);
// TODO 自动生成构造函数存根
}
public void commandAction(Command c, Displayable d) {
// TODO 自动生成方法存根
if(c==exitCommand)
{
gfm.quitApp();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -