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

📄 mmsender.java

📁 用于开发mms应用的Java库
💻 JAVA
字号:
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the Tambur MMS library.
 *
 * The Initial Developer of the Original Code is FlyerOne Ltd.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * 	Anders Lindh <alindh@flyerone.com>
 *
 * ***** END LICENSE BLOCK ***** */

package net.tambur.mms;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;

import org.apache.log4j.Logger;

/**
 * A class for sending MMMessages. 
 * <p>
 * This class is a plug-in replacement for the Nokia equivalant, meant for submitting MMMessages.
 * <p>
 * 
 * @author Anders Lindh
 * @copyright Copyright FlyerOne Ltd 2005
 * @version $Revision: 1.1.1.1 $ $Date: 2005/04/14 09:04:10 $
 */

public class MMSender {
	
	/**
	 * logger 
	 */
	protected transient Logger log = Logger.getLogger(this.getClass());

	/**
	 * Variables
	 */
	protected String mmscUrl = null;
	protected Hashtable headers = null;
	
	/**
	 * Code
	 */
	public MMSender() {
		headers = new Hashtable();
	}
	
	/**
	 * Add a new header to the HTTP request
	 */
	public void addHeader(String name, String value) {
		headers.put(name, value);
	}
	
	/**
	 * Clear all headers
	 */
	public void clearHeader() {
		headers.clear();
	}
	
	/**
	 * Set the MMSC Url
	 */
	public void setMMSCURL(String url) {
		mmscUrl = url;
	}
	
	/**
	 * Get the MMSC Url
	 */
	public String getMMSCURL() {
		return mmscUrl;
	}
	
	/**
	 * Send a MMMessage
	 * 
	 * @param msg Either a MMMessage object, or a byte array representing a multimedia message
	 * @return Return code from server
	 */
	public int send(Object mmMsg) throws Exception {
		byte[] content = null;
												
		if (mmMsg instanceof MMMessage) {
			MMMessage msg = null;
			msg = (MMMessage) mmMsg;
			content = msg.encode();
		} else if (mmMsg instanceof String) {
			content = ((String) mmMsg).getBytes();
		} else
			content = ((byte[]) mmMsg);
				
	    URL u = new URL(mmscUrl);
        HttpURLConnection con = (HttpURLConnection) u.openConnection();

		con.setDoInput(true);
	    con.setDoOutput(true);
	    con.setRequestMethod("POST");
	    con.setUseCaches(false);
	    con.setDefaultUseCaches(false);
	    con.setAllowUserInteraction(false);
	    con.setRequestProperty("Content-Length", String.valueOf(content.length));
	
		log.debug("Submitting [" + content.length + "] bytes to [" + mmscUrl + "]"); 
		
		// set content type
		con.setRequestProperty("Content-type", "application/vnd.wap.mms-message");
		
		// con.setRequestProperty("Connection", "close");
		    	
		Enumeration enum = headers.keys();
		while (enum.hasMoreElements()) {    		
    		String name = (String) enum.nextElement();
    		Object o = headers.get(name);
    		String value = (String) o;

           	con.setRequestProperty(name, value);
           	System.out.println(name + ": " + value);
    	}       	
		    	
		OutputStream out = con.getOutputStream();

      //  con.connect();

        out.write(content);
        out.flush();
        out.close();
        
		int responseCode = con.getResponseCode();
		
		
		String s = null;
		int i = 1;
				
		while ((s = con.getHeaderFieldKey(i)) != null) {
			log.debug(s + ": " + con.getHeaderField(i));
			i++;
		}
		
		/*byte[] output = null;	
		 
		if (responseCode != 204) { 
			InputStream input = con.getInputStream();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
    
			byte[] buf = new byte[1];
			int len = 0;	
        		
			while ((len = input.read(buf, 0, buf.length)) != -1) 
				baos.write(buf, 0, len);
     		
			output = baos.toByteArray();   		
			
			System.out.println(new String(output));
		}*/
            
       	return responseCode;             
	}		

}

⌨️ 快捷键说明

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