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

📄 httpuploadcapturedbuffer.java

📁 我们提供的VideoIM手机自动拍照上传器J2ME版本[开源]就是这么一种概念: 一个可以下载到手机(例如Nokia7610已经确实可以下载安装并正常运行)的Java应用程序
💻 JAVA
字号:
/**
//VideoIM文档生成日期:2005.10.12
//
//(1)概述:
//类名称:HttpUploadCapturedBuffer
//类说明:
//	提供通过HttpConnection经由GPRS CMNET通道向远程服务器发送图像数据的功能
* 
//所在子系统:VideoIM
//
//系统总描述:
	    我们提供的VideoIM手机自动拍照上传器J2ME版本[开源]是
	    一个可以下载到手机(例如Nokia7610已经确实可以下载安装并正常运行)的应用程序,
	    用来自动驱动手机摄像头定时拍摄,并后台将JPEG图像(很小,大约几KB)上传到服务器上,
	    这样就可以帮助其他系统工作,比如PC机上的MSN Messenger可以和你的移动MSN Messenger
	    通过这种方式视频聊天,对方可以每隔十几秒钟看到你的手机所看到的画面了。

	 子系统描述:
		VideoIM的功能列表:
			1:我要MobileWebCam
				启动MobileWebCam
				停止MobileWebCam
			2:MobileWebCam设置
			3:关于我
			4:退出


//(2)历史记录:
//创建人: 郑昀(2005.10.12)
//联系我: Google Talk >> zhengyun@gmail.com
//Blogs:    http://blog.csdn.net/zhengyun_ustc/以及http://www.cnblogs.com/zhengyun_ustc

//(3)版权声明:
//由于我这个版本的VideoIM手机自动拍照上传器也是基于Mowecam的设计理念基础上改编而来的,
//所以决定遵照GPL协议的大意开放源代码,您可以自由传播和修改,在遵照GPL协议的约束条件的前提下。

//(4)相关资源:
1:《[J2ME]VideoIM手机自动拍照上传器开源说明》
2:《[J2ME]VideoIM手机自动拍照上传器设计说明》
3:下载源代码:

////////////////////////////////////////////////////////////////////*/
package com.ultrapower.tools;

import java.io.IOException;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import com.ultrapower.common.CommandResources;
import com.ultrapower.control.GUIController;
import com.ultrapower.model.VideoSettings;
import com.ultrapower.view.FormPostProgress;
import com.ultrapower.view.WaitFlash;

/**********************************************************
//HttpUploadCapturedBuffer
//
//Class Description:
//	提供通过HttpConnection经由GPRS CMNET通道向远程服务器发送图像数据的功能
//
//Author:
//zhengyun@ultrapower 2005.10.12
//
**********************************************************/
public class HttpUploadCapturedBuffer {
	
	private byte m_byteBuffer[];
	private String m_strCurrentMagic;
	private GUIController m_controller;
	private VideoSettings m_settings = VideoSettings.getInstance();
	
	private final int m_MAX_WRITE_DATA_LENGTH = 1700;
	
	public HttpUploadCapturedBuffer(byte abyte0[])
    {
		m_byteBuffer = abyte0;
		m_strCurrentMagic = String.valueOf(
				BuildLongFromString(
						m_settings.getPlayerName() + "thisisasalt1234"));
    }

    public HttpUploadCapturedBuffer(byte abImageData[],
			GUIController control)
    {
        this(abImageData);
		m_controller = control;
    }
	
	private String GetCID()
    {
        return "cid" + System.currentTimeMillis();
    }
	
	private long BuildLongFromString(String strMagic)
    {
        int j = 0;
        for(int k = 0; k < strMagic.length(); k++)
            if((k & 0x1) == 0)
                j ^= j << 7 ^ strMagic.charAt(k) ^ j >> 3;
            else
                j ^= ~(j << 11 ^ strMagic.charAt(k) ^ j >> 5);

        return (long)(j & 0x7fffffff);
    }
	
	private void postWithContentLength(HttpConnection httpconnection, int nContentLength)
    throws IOException
	{
	    httpconnection.setRequestMethod("POST");
		
		/*
			这种User-Agent不能发送给IIS服务器,否则得到400错误
	    	httpconnection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
	     * 
	     */
	    httpconnection.setRequestProperty("Content-Language", "zh-CN");
	    httpconnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
	    httpconnection.setRequestProperty("Content-Length", String.valueOf(nContentLength));
	}
	
	public final String upload()
    throws SecurityException
    {
		System.out.println("/** Enter HttpUploadCapturedBuffer::upload!");
	    String strError = "ERROR=";
		String sProgress = String.valueOf(
				CommandResources.getChars(CommandResources.TXT_WAITMESSAGE));
		String sProgressTitle = String.valueOf(
				CommandResources.getChars(CommandResources.TXT_WAITTITLE));
		
	    try
	    {
	        String strCID = GetCID();
	        StringBuffer sbQueryString;
	        (sbQueryString = new StringBuffer("?cid")).append('=');
			sbQueryString.append(strCID);
			sbQueryString.append('&');
			sbQueryString.append("user");
			sbQueryString.append('=');
			sbQueryString.append(m_strCurrentMagic);
			sbQueryString.append('&');
			sbQueryString.append("type");
			sbQueryString.append('=');
			sbQueryString.append(m_settings.getSnapshotImageType());
	        String strQueryString = sbQueryString.toString();
			
			String strPostURL = 
				String.valueOf(m_settings.getRemoteServerUploadPageURL())
				+ strQueryString;
			System.out.println("/** Connect to:" + strPostURL);
			
	        int nResponseStatus = 200;
	        int nPostingDataCount = 0;
	        boolean bPostData = m_byteBuffer != null;
			
	        for(; (nPostingDataCount < m_byteBuffer.length) &&
				(false == m_controller.getStopPostData()); 
				nPostingDataCount += m_MAX_WRITE_DATA_LENGTH)
	        {
	            if(bPostData)
				{
					////////////////////////////////////////////
					// 告诉控制器,需要显示进度条Form了,而且要告知总的字节数
					m_controller.setPostingDataLength(
							String.valueOf((m_byteBuffer.length - nPostingDataCount)));
					m_controller.handleEventNoThrows(GUIController.EventID.EVENT_POSTDATAING,
							null);
					
					/*m_waitFlash.setMessage(sProgressTitle + String.valueOf(m_byteBuffer.length),
					sProgress + String.valueOf((m_byteBuffer.length - nPostingDataCount)));*/
					
					////////////////////////////////////////////
				}
	            
				System.out.println("/** 剩下要发送的数据大小为:" + 
						(m_byteBuffer.length - nPostingDataCount));
				HttpConnection httpconnection = 
					(HttpConnection)Connector.open(strPostURL, 3);
	            boolean bIsEOS = nPostingDataCount + m_MAX_WRITE_DATA_LENGTH >= m_byteBuffer.length;
	            
	            int nCurrentPostDataLength;
	            if(bIsEOS)
					nCurrentPostDataLength = m_byteBuffer.length - nPostingDataCount;
	            else
					nCurrentPostDataLength = m_MAX_WRITE_DATA_LENGTH;
				postWithContentLength(httpconnection, nCurrentPostDataLength);
				
	            OutputStream output;
				
				(output = httpconnection.openOutputStream()).write(
						m_byteBuffer, nPostingDataCount, nCurrentPostDataLength);
				
				nResponseStatus = httpconnection.getResponseCode();
				System.out.println("/** asp服务器响应:" + nResponseStatus);
				
	            if(output != null)
					output.close();
	            if(httpconnection != null)
	                httpconnection.close();
				
				System.out.println("/** 上传数据完毕!");
	        }
	
			/*
			 * 只要服务器响应码小于300,就代表上传数据成功
			 * 因为最后一次传递数据,好像得到了100返回码
			 */
	        if(nResponseStatus < 300)
				strError = strError + String.valueOf(nResponseStatus);
	        else
				strError = "Success";
	    }
	    catch(IOException ioexception)
	    {
			strError = "ERROR=" + ioexception.getMessage();
	    }
		finally
		{
			m_byteBuffer = null;
		}
		
		
	    return strError;
	}
}

⌨️ 快捷键说明

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