新手汗颜发题, 请高手们援手

zhangyuan 2005-05-31 01:42:54
写一个j2me程序, 读一个apache上的文件夹, 当然, apache会把它关联到一个虚文件夹, 比如http://127.0.0.1/test/ .
现在要求这个程序读取服务器所产生的html内容, 遍历这些内容产生一个列表让用户来选择. 当用户选一项之后, 程序探测这一项相关的URL, 如果它类型是text(比如,text/html,text/xml)程序就下载这些文本内容在屏上显示, 如果项的内容是二进制的, 那么程序显示文件类型和大小.
...全文
270 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
sea2000cn 2005-06-08
  • 打赏
  • 举报
回复
按照你的思路,数据流需要你自己来解析
Truly 2005-06-07
  • 打赏
  • 举报
回复
点管理结帖
Truly 2005-06-07
  • 打赏
  • 举报
回复
jf
zhangyuan 2005-06-06
  • 打赏
  • 举报
回复
怎么给分呢?
大家接分吧, 回够50人结帖.
zhangyuan 2005-06-06
  • 打赏
  • 举报
回复
我自己写完了,没有人可以让依靠,把程序帖出来:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import java.util.*;

public class HTTPTest
extends MIDlet
implements CommandListener, Runnable {
private Display display;
private Command back = new Command("Back", Command.BACK, 0);

private String dircontent = "";

private Vector URLVec = new Vector();
private String URL = "";
public HTTPTest() {
display = Display.getDisplay(this);
}

protected void startApp() {
MainForm();
}

public void MainForm() {
List myList = new List("主菜单", Choice.IMPLICIT);
myList.append("http://127.0.0.1/", null);
myList.setCommandListener(this);
display.setCurrent(myList);
}

public void HTTPGet(String urlStrGet) throws IOException {
HttpConnection hc = null; // HttpConnection 连接
InputStreamReader readerIS = null; // 读入数据
StringBuffer buf = new StringBuffer();
String urltype = "";
String urlname = "";
String content = "";
boolean RIS = false;
try {
// 打开连接
hc = (HttpConnection) Connector.open(urlStrGet);
// 设置请求方法
hc.setRequestMethod(HttpConnection.GET);
hc.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0");
urltype = hc.getType();
urlname = hc.getFile();
if (urltype.indexOf("text") != -1) { // 打开的是不是TEXT类型?
readerIS = new InputStreamReader(hc.openDataInputStream());
RIS = true;
int ch;
// 接收服务器数据
while ( (ch = readerIS.read()) != -1) {
buf.append( (char) ch);
}
content = "";
content = content + buf.toString();


if (urlname.endsWith("/") && content.indexOf("<h1>Index of")!= -1) { // 打开的是不是个文件夹?
dircontent = content;
List myList = new List("All Files", Choice.IMPLICIT);
Command back = new Command("Back", Command.SCREEN, 2);
myList.addCommand(back);
myList.setCommandListener(this);
// 跳过标题行
int bgnpos = 0;
bgnpos = content.indexOf("<img src=\"/icons/blank.gif\"");
bgnpos = bgnpos + 4;
bgnpos = content.indexOf("<img", (bgnpos + 4));
content = content.substring(bgnpos);
if ( (bgnpos = content.indexOf("<img src=\"/icons/back.gif\"")) != -1 )
{
bgnpos = bgnpos + 4;
bgnpos = content.indexOf("<img", (bgnpos + 4));
content = content.substring(bgnpos);
}
String file = ""; //文件夹中的文件名

// 开始找文件名
while (true) {
bgnpos = content.indexOf("<a href=\"");
if (bgnpos == -1) {
break;
}
int endpos = content.indexOf("\">", bgnpos);
file = content.substring(bgnpos + 9, endpos);
content = content.substring(endpos);
myList.append(file,null);
}

display.setCurrent(myList);
}
else { // 不是文件夹, 返回其内容
Form f = new Form("Text Detail");
f.addCommand(back);
f.setCommandListener(this);
f.append(content);
display.setCurrent(f);
}
}
else { // 是二进制文件
Form f = new Form("File Detail");
String filename = (String) URLVec.lastElement();
int posbgn = dircontent.indexOf(filename);
posbgn =posbgn + filename.length() * 2 + 34; // dirty bloody method. don't care the magic numbers
int posend = dircontent.indexOf("<", posbgn);
String size = dircontent.substring(posbgn, posend);
f.append("File Name:\n");
f.append(filename + "\n");
f.append("Size:\n");
f.append(size + "\n");
display.setCurrent(f);
f.addCommand(back);
f.setCommandListener(this);
dircontent = "";
}
}
finally {
if (RIS)
readerIS.close();
hc.close();
}
}

protected void pauseApp() {
}

protected void destroyApp(boolean boolean0) {
}

public void commandAction(Command c, Displayable s) {
if (c == List.SELECT_COMMAND) {
List tmp = (List) s;
URLVec.addElement(new String(tmp.getString(tmp.getSelectedIndex())));
}
if (c.getLabel().equals("Back")) {
int tmp = URLVec.size();
URLVec.removeElementAt(tmp-1);
}
int i;
for ( URL="", i = 0; i < URLVec.size(); i++) {
URL = URL + (String) URLVec.elementAt(i);
}
if ( URL.equals("") )
MainForm();
else{
Thread t = new Thread(this);
t.start();
}

}
public void run() {
try {
HTTPGet(URL);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
zhangyuan 2005-06-01
  • 打赏
  • 举报
回复
小弟查了一下, MIDP里有6个包,没有这个URLContext类!!!!!
zhangyuan 2005-06-01
  • 打赏
  • 举报
回复
我怎么找不到捏?在JBUILDER的帮助中都没有找到URLContext, 小弟以前用C刚刚开始用JAVA, 对它的架构不了解啊,只知道语法.请再提示一下...
zhangyuan 2005-06-01
  • 打赏
  • 举报
回复
我的代码更新了, 已经实现了文件名的查找,准备再把这些名放到URL上面去重新连接,看它的属性.哪个哥给点指导啊, 初学者在黑暗中摸索啊, 这是我的第一个java程序啊,一小时才憋出来几行代码啊.



import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import java.util.*;

public class HTTPTest extends MIDlet implements CommandListener, Runnable
{
private Display display;
private Command back = new Command("Back", Command.BACK, 0);
private String urlStrGet = "http://127.0.0.1";
private String content ="";
Vector fileVec = new Vector();


public HTTPTest()
{
display = Display.getDisplay(this);
}

protected void startApp()
{

MainForm();
}
public void MainForm()
{
List myList = new List("主菜单", Choice.IMPLICIT);
myList.append(urlStrGet,null);
myList.setCommandListener(this);
display.setCurrent(myList);
}

public void HTTPGet(String urlStrGet) throws IOException
{
HttpConnection hc = null; // HttpConnection 连接
InputStreamReader readerIS = null; // 读入数据
StringBuffer buf = new StringBuffer();
String file = "";
try
{
// 打开连接
hc = (HttpConnection) Connector.open(urlStrGet);
// 设置请求方法
hc.setRequestMethod(HttpConnection.GET);
hc.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0");
readerIS = new InputStreamReader(hc.openDataInputStream());
int ch;
// 接收服务器数据
while ( (ch = readerIS.read()) != -1) {
buf.append( (char) ch);
}
content = content + buf.toString();


// 跳过标题行
int bgnpos = 0;
bgnpos = content.indexOf("<img");
bgnpos = bgnpos + 4;
bgnpos = content.indexOf("<img", (bgnpos + 4));
content = content.substring(bgnpos);

// 开始找文件名
while (true)
{
bgnpos = content.indexOf("<a href=\"");
if (bgnpos == -1)
break;
int endpos = content.indexOf("\">", bgnpos);
file = content.substring(bgnpos + 9, endpos);
content = content.substring(endpos);
fileVec.addElement(new String(file)); // 将文件名放在容器里
}
}
finally
{
readerIS.close();
hc.close();
}

}

protected void pauseApp()
{
}
protected void destroyApp(boolean boolean0)
{
}

public void commandAction(Command command, Displayable displayable)
{
if (command == List.SELECT_COMMAND) {
Thread t = new Thread(this);
t.start();
}
}

public void run()
{
try
{
HTTPGet(urlStrGet);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
cqgaoke 2005-06-01
  • 打赏
  • 举报
回复
up
airhand 2005-05-31
  • 打赏
  • 举报
回复
用URLContext类的查看属性方法放回文件属性,里面包括ContentType,大小等信息,根据信息判断后下载。
zhangyuan 2005-05-31
  • 打赏
  • 举报
回复
是不是我要先把文件名解析出来,然后一个一个往服务器上连?
网络咖啡 2005-05-31
  • 打赏
  • 举报
回复
连上服务器,根据头信息ContentType的值来判断文件的类型,然后使用流来读取具体的数据
zhangyuan 2005-05-31
  • 打赏
  • 举报
回复
我只是刚刚连上服务器, 剩下事怎么做啊? 给点解决方案吧....
zhangyuan 2005-05-31
  • 打赏
  • 举报
回复
100分啊, 没有人来取啊, 说个思路也行!!!
cutelion 2005-05-31
  • 打赏
  • 举报
回复
J2ME 正在学习中......
zhangyuan 2005-05-31
  • 打赏
  • 举报
回复
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;

public class HTTPTest extends MIDlet implements CommandListener, Runnable
{
private Display display;
private Command back = new Command("Back", Command.BACK, 0);
private String urlStrGet = "http://127.0.0.1/";
public HTTPTest()
{
display = Display.getDisplay(this);
}

protected void startApp()
{
MainForm();
}
public void MainForm()
{
List myList = new List("主菜单", Choice.IMPLICIT);
myList.append("GET",null);
myList.setCommandListener(this);
display.setCurrent(myList);
}
public void HTTPGet( String urlStrGet ) throws IOException
{
HttpConnection hc = null; // HttpConnection 连接
InputStream is = null; // 读入数据
StringBuffer buf = new StringBuffer(); //保存服务器响应数据
// TextBox t = null;
String content ="";
Form f = new Form("HTTP TEST");
try
{
// 打开连接
hc = (HttpConnection)Connector.open(urlStrGet);
// 设置请求方法
hc.setRequestMethod(HttpConnection.GET);
hc.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
is = hc.openDataInputStream();

int ch;
// 接收服务器数据
while((ch = is.read()) != -1)
{
content = content + (char)ch;
//buf.append((char) ch);
}
// t = new TextBox("Get Test Page", buf.toString(), 1024, 0);

f.append(content);

//t.addCommand(back);
//t.setCommandListener(this);
}
finally {
is.close();
hc.close();
}
display.setCurrent(f);
}
protected void pauseApp()
{
}
protected void destroyApp(boolean boolean0)
{
}

public void commandAction(Command command, Displayable displayable)
{
if (command == List.SELECT_COMMAND) {
Thread t = new Thread(this);
t.start();
}
}

public void run()
{
try
{
HTTPGet(urlStrGet);
}catch(Exception e)
{
e.printStackTrace();
}
}


这是我的代码, 但是html内容的分析放在哪里好呢?
zhangyuan 2005-05-31
  • 打赏
  • 举报
回复
能不能给个代码让我分析一下啊?
zhangyuan 2005-05-31
  • 打赏
  • 举报
回复
如果服务器端不写程序呢?只在客户端实现.
我读到内容之后, 在窗户端分析HTML内容如何?
cuilichen 2005-05-31
  • 打赏
  • 举报
回复
这个需求不是很难。
在服务器端使用Servlet响应用户的连接,分析命令,进行文件分析,并且返回信息给用户就可以了。

重点是手机和服务器之间的通讯,服务器端的东西都是常规的。
贝壳鱼 2005-05-31
  • 打赏
  • 举报
回复
J2ME+J2SE+J2EE

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧