我用J2ME实现了用手机控制电脑开关机很简单,在电脑上开一个WEB服务器,用HTTP协议使手机连接互联网访问电脑,WEB服务器解析手机命令就可以控制本机,服务器端用JSP/Servlet或ASP都可以。
主要代码可以给你
手机端
package controlitem;
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
public class ConnectionManager
{
private HttpConnection con;
private InputStream is;
private OutputStream os;
private String baseurl = "http://10.1.49.51:7001/Mobile_Server/servlet1?cmd=";
private String ua;
private String locale;
private String msg;
private String user;
private String password;
public ConnectionManager()
{
System.out.println("初始化ConnectionManager");////////////////////
StringBuffer sb = new StringBuffer(60);
sb.append("configuration/");
sb.append(System.getProperty("microedition.configuration"));
String prof = System.getProperty("microedition.profiles");
System.out.println("prof is " + prof);////////////////////
int i = 0,j = 0;
while((j = prof.indexOf(' ',i))!= -1)
{
sb.append(" Profiles/");
sb.append(prof.substring(i,j));
i = j + 1;
}
sb.append(" Profile/");
sb.append(prof.substring(i));
ua = sb.toString();
System.out.println("sb is" + sb + "ua is " + ua);////////////////////////
locale = System.getProperty("microedition.locale");
System.out.println("locale is" + locale);///////////////////////
if(locale == null)
{
locale = "en-UD";
}
System.out.println("初始化完毕");///////////////////////////
}
byte[] Progress()//处理连接,获得回复信息
{
byte[] data = null;
try
{
open();//打开连接
int n = (int)con.getLength();//响应信息的长度
if(n>0)
{
int bytesread = 0;
data = new byte[n];
for(int offset = 0;offset<n;offset += bytesread)
{
bytesread = is.read(data,offset,n-bytesread);
}
}
}
catch(IOException ioe)
{
System.out.println("error here!!! " + ioe.getMessage());
}
finally
{
try
{
if(con!= null)
{
con.close();
}
if(os != null)
{
os.close();
}
if(is != null)
{
is.close();
}
}
catch(IOException ioe)
{
System.out.print("error here @@@@@@@@@@");
}
return data;
}
}
private void open() throws IOException
{
int status = -1;
String url = baseurl;
String auth = null;
is = null;
os = null;
con = null;
while(con == null)//一直循环直到连接被建立
{
System.out.println("开始打开连接!url=" + url );////////////////////////
con = (HttpConnection)Connector.open(url);
con.setRequestMethod(HttpConnection.POST);
con.setRequestProperty("User-Agent",ua);
con.setRequestProperty("Accpet-Language",locale);
con.setRequestProperty("Content-Type","text/plain");
con.setRequestProperty("Accept","text/plain");
if(user != null&&password != null)
{
con.setRequestProperty("Authorization","Basic ");//此处应该加入64base的用户名和密码 +BasicAuth.encode(user,password)
}
os = con.openOutputStream();//打开输出流会清空所有的响应头
System.out.println("输出流已打开" );////////////////////
os.write(msg.getBytes());//把消息写入输出流
os.close();
os = null;
System.out.println("输出流已关闭");////////////////////
status = con.getResponseCode();//检查返回的状态码
System.out.println("status is " + status);/////////////////
switch(status)
{
case HttpConnection.HTTP_OK://成功
break;
case HttpConnection.HTTP_TEMP_REDIRECT:
case HttpConnection.HTTP_MOVED_TEMP:
case HttpConnection.HTTP_MOVED_PERM:
url = con.getHeaderField("location");//重定向,取得新的目标地址
con.close();
con = null;
break;
default://错误,抛出一个异常
con.close();
throw new IOException("Response status not OK:" + status);
}
}
is = con.openInputStream();//打开输入流。使调用者可以读取响应信息
}
void setMsg(String s)
{
msg = s;
}
void setUser(String s)
{
user = s;
}
void setPassword(String s)
{
password = s;
}
void appendUrl(String append)
{
baseurl += append;
}
}
服务器端(Servlet)
package mobile_server;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Servlet1 extends HttpServlet
{
String execCmd;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String cmd=request.getParameter("cmd");
if(cmd == null)
cmd = "nocmd";
if(cmd.equals("sendinfo"))
{
String ip = request.getParameter("ip");
String info = request.getParameter("info");
if(info == null||info.length()==0) info="";
execCmd = "net send " + ip + " " +info;
System.out.println("execCmd is " + execCmd);
Runtime time = Runtime.getRuntime();
time.exec(execCmd);
}
else if(cmd.equals("rebootComputer"))
{
execCmd = "tsshutdn /reboot";
Runtime time = Runtime.getRuntime();
time.exec(execCmd);
}
else if(cmd.equals("shutdownComputer"))
{
execCmd = "tsshutdn";
Runtime.getRuntime().exec(execCmd);
}
else
{
System.out.print("no cmd!");
}
}
}