⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connecthttp.java

📁 手机在线系统 采用Java 中的J2ME, JSP 跟MySql 运行环境是:jsdk1.4以上
💻 JAVA
字号:
/*
 * @(#)WebServer.java	1.11 01/08/23
 * Copyright (c) 2004-2005 wuhua of workroom Inc. All Rights Reserved.
 * @version 	1.0, 10/05/2004
 * @author 	饶荣庆
 * @author 	余煜辉
 */
/**
 *此common包是连网的共有包,为所有连网提供统一的接口
 */
package com.j2me.common;

import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

/*
 *此类是用来描述手机联网时的共有类,用来处理用户联网的统一类
 *此类实现的功能有:获得网站内容,与图片
 */
public class ConnectHttp
{
	public static boolean isDowloandOk; //判断下载好了没	
	public static long length;

	/*调用JSP方法,getURL为服务器网址, parameter为参数连接网络*/
	public static String invokeJSP(String getURL, String parameter)
		throws IOException
	{
		HttpConnection c = null;
		InputStream is = null;
		OutputStream os = null;
		StringBuffer b = new StringBuffer();
		String getContent = null;
		try
		{
			//建立连接
			c = (HttpConnection)Connector.open(getURL);
			//设置连接一些属性
			c.setRequestMethod(HttpConnection.POST);
			c.setRequestProperty("IF-Modified-Since", "29 Dec 2001 15:17:19 GMT");
			c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
			c.setRequestProperty("Content-Language", "en-US");

			//设置请求Content-Type属性,
			/*
			 *如果使用POST方法,一定要设置Content-Type请求属性为application/x-www-form-urlencoded,
			 *这样,在JSP/Servlet中才可以使用request.getParameter(String parameter)来获取客户端发送过来得参数值
			 */
			c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

			//获得内容长度
			//length = c.getLength();

			//建立输出流
			os = c.openDataOutputStream();
			os.write(parameter.getBytes());	//参数,并把参数转换为字节流,因为网络传输是用字节流的
			//os.flush();
			
			//建立输入流
			is = c.openDataInputStream();
			int ch;
			while ((ch = is.read()) != -1) 
			{
				//读取信息
				b.append((char) ch);  
				if (ch == -1)
				{
					isDowloandOk = true;
				}
			}

			//定义一个字符串getContent用于保存从网络上获取的内容
			//并调用类Unicode中的unicodeToString(String s)方法用于与处理JSP/Servlet中文乱吗问题
			getContent = Unicode.unicodeToString(b.toString());
			
		}
		catch(IOException ioe)
		{
			throw new IOException("Connect Error");
		}
		finally
		{
			if (is != null)
			{
				is.close();
			}
			if (c != null)
			{
				c.close();
			}
			if (os != null)
			{
				os.close();
			}	 
		}	
		return getContent;
	}

	/*调用JSP方法,getURL为服务器网址,没有参数的调用*/
	public static String invokeJSP(String getURL)
		throws IOException 
	{
		HttpConnection c = null;
		InputStream is = null;
		StringBuffer b = new StringBuffer();
		String getContent = null;
		try
		{
			//建立连接
			c = (HttpConnection)Connector.open(getURL);
			//设置连接一些属性
			c.setRequestMethod(HttpConnection.POST);
			c.setRequestProperty("IF-Modified-Since", "29 Dec 2001 15:17:19 GMT");
			c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
			c.setRequestProperty("Content-Language", "en-US");

			//设置请求Content-Type属性,
			/*
			 *如果使用POST方法,一定要设置Content-Type请求属性为application/x-www-form-urlencoded,
			 *这样,在JSP/Servlet中才可以使用request.getParameter(String parameter)来获取客户端发送过来得参数值
			 */
			c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

			//获得内容长度
			//length = c.getLength();
			
			//建立输入流
			is = c.openDataInputStream();
			int ch;
			while ((ch = is.read()) != -1) 
			{
				//读取信息
				b.append((char) ch);  
				if (ch == -1)
				{
					isDowloandOk = true;
				}
			}

			//定义一个字符串getContent用于保存从网络上获取的内容
			//并调用类Unicode中的unicodeToString(String s)方法用于与处理JSP/Servlet中文乱吗问题
			getContent = Unicode.unicodeToString(b.toString());
			
		}
		catch(IOException ioe)
		{
			throw new IOException("Connect Error");
		}
		finally
		{
			if (is != null)									  
			{
				is.close();
			}
			if (c != null)
			{
				c.close();
			}
		} 
		return getContent;
	}

	/*获得网络图片*/
	public static Image getJspImage(String getURL)
		throws IOException, NullPointerException, IllegalArgumentException
	{	
		HttpConnection c = null;
		InputStream is = null;	  
		ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
		Image image = null;
		byte[] imageData = null;
		try
		{
			c = (HttpConnection) Connector.open(getURL);            
			is = c.openInputStream();                
			               
			int ch = 0;              
			while ((ch = is.read()) != -1)               
			{                    
				baos.write(ch);  //读取网络图片            
			}  
		}
		catch(IOException ioe)
		{
			throw new IOException("Connect Error");
		}
		
		imageData = baos.toByteArray();    //把图片转换成字节
		try
		{
			image = Image.createImage(imageData, 0, imageData.length);     
		}
		catch(NullPointerException e)
		{
			throw new NullPointerException("NullPointerException");
		}
		catch(IllegalArgumentException e)
		{
			throw new IllegalArgumentException("IllegalArgumentException");
		}
		
		return image;
	}
	

	/*带参数的连网,没返回值的处理过程*/
	public static void executeJSP(String getURL, String parameter)
		throws IOException
	{
		HttpConnection c = null;
		OutputStream os = null;
		try
		{
			//建立连接
			c = (HttpConnection)Connector.open(getURL);
			//设置连接一些属性
			c.setRequestMethod(HttpConnection.POST);
			c.setRequestProperty("IF-Modified-Since", "29 Dec 2001 15:17:19 GMT");
			c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
			c.setRequestProperty("Content-Language", "en-US");

			//设置请求Content-Type属性,
			/*
			 *如果使用POST方法,一定要设置Content-Type请求属性为application/x-www-form-urlencoded,
			 *这样,在JSP/Servlet中才可以使用request.getParameter(String parameter)来获取客户端发送过来得参数值
			 */
			c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

			//获得内容长度
			//length = c.getLength();

			//建立输出流
			os = c.openDataOutputStream();
			os.write(parameter.getBytes());	//参数,并把参数转换为字节流,因为网络传输是用字节流的
			//os.flush();
		}
		catch(IOException ioe)
		{
			throw new IOException("Connect Error");
		}
		finally
		{
			if (os != null)									  
			{
				os.close();
			}
			if (c != null)
			{
				c.close();
			}
		}
	}
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -