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

📄 sendmms.java

📁 Java library for encoding/decoding MMS messages. Also provides a simlpe client for sending MMS throu
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2008 Andrea Zito *  * This file is part of SendMMS. * * jMmsLib is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as  * published by the Free Software Foundation, either version 3 of  * the License, or  (at your option) any later version. * * SendMMS is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public  * License along with SendMMS.  If not, see <http://www.gnu.org/licenses/>. */ package net.sourceforge.jmmslib.sendmms;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import java.util.Properties;import java.util.Random;import net.sourceforge.jwap.PostRequest;import net.sourceforge.jwap.Response;import net.sourceforge.jwap.WAPClient;import net.sourceforge.jmmslib.*;/** * SendMms main class.<br> * Enforce some constraints on element sizes and, if all is ok, build an mms message * with the specified contents and send it trough the configured wap gateway.  *  * @author Andrea Zito * */public class SendMms {	/* Configuration constants */	private static final String CONFIG_FILE_PROPERTY="confFile";	private static final String CONFIG_FILE_DEFAULT="./mmssend.properties";			private static final String CONFIG_WAP_GATEWAY_HOST="waphost";	private static final String CONFIG_WAP_GATEWAY_PORT="wapport";	private static final String CONFIG_MMS_SERVLET="mmsservlet";		private static final String CONFIG_SUBJECT_PROPERTY="subject";	private static final String CONFIG_SENDER_PROPERTY="sender";	private static final String CONFIG_IMAGE_MAX_SIZE_PROPERTY="imageMaxSize";	private static final String CONFIG_IMAGE_MAX_SIZE_DEFAULT="200000";	private static final String CONFIG_TEXT_MAX_SIZE_PROPERTY="textMaxSize";	private static final String CONFIG_TEXT_MAX_SIZE_DEFAULT="200000";	private static final String CONFIG_MMS_MAX_SIZE_PROPERTY="mmsMaxSize";	private static final String CONFIG_MMS_MAX_SIZE_DEFAULT="1000000";	private static final String CONFIG_SKIP_PROPERTY="skip";	private static final String CONFIG_SKIP_DEFAULT="false";		/* Command line constants */	private static final String SUBJECT_SWITCH = "-s";	private static final String TEXT_STRING_SWITCH = "-t";	private static final String TEXT_FILE_SWITCH = "-T";	private static final String IMAGE_FILE_SWITCH = "-i";	private static final String IMAGE_MAXSIZE_SWITCH = "-is";	private static final String TEXT_MAXSIZE_SWITCH = "-ts";	private static final String MMS_MAXSIZE_SWITCH = "-S";	private static final String SKIP_OVERSIZE_SWITCH = "--skip";	private static final String HELP_SWITCH = "-h";	private static final String HELP_SWITCH_L = "--help";	private static final String VERBOSE_SWITCH = "-v";				/*================================================	 * Class Variable	 *================================================*/	private String senderNumber;	private String destNumber;	private String subject;	private ArrayList<ContentEntry> contentEntries;	private int textMaxSize;	private int imageMaxSize;	private int mmsMaxSize;	private boolean skip = false;	private boolean verbose;		private byte[] encodedMms;		/*================================================	 * Constructors	 *================================================*/	private SendMms(String senderNumber, String destNumber, String subject, ArrayList<ContentEntry> entries){		this.senderNumber = senderNumber;		this.destNumber = destNumber;		this.subject = subject;		this.contentEntries = entries;	}			/*================================================	 * Methods	 *================================================*/		private void setTextMaxSize(int textMaxSize) {		this.textMaxSize = textMaxSize;	}	private void setImageMaxSize(int imageMaxSize) {		this.imageMaxSize = imageMaxSize;	}	private void setMmsMaxSize(int mmsMaxSize) {		this.mmsMaxSize = mmsMaxSize;	}	private void setSkip(boolean skip) {		this.skip = skip;	}			/**	 * Checks that every content part of the message has a acceptable size	 * and that the whole mms size is less than the maximum	 * @throws SendMmsException	 */	private void checkContentEntries() throws SendMmsException{		Iterator<ContentEntry> i = contentEntries.iterator();				ContentEntry e;		boolean test;		int size=0;				int totalSize=0;		while(i.hasNext()){			e = i.next();			test = true;			try{				e.loadContent();				size = e.getContent().length;				if (e.getType() == ContentEntry.TEXT_FILE || e.getType() == ContentEntry.TEXT_STRING)					test = size <= textMaxSize;				else					test = size <= imageMaxSize;			}			catch(SendMmsException ex){				if (!skip) throw ex;				test = false;			}						if (test) totalSize += size;			else{				if (skip) i.remove();				else throw new SendMmsException("Size of content \"" + e.getS() + "\" too big.");			}		}				if (totalSize > mmsMaxSize) 			throw new SendMmsException("Size of the MMS message too big");	}		/**	 * Builds the mms message	 * @throws MmsMessageException 	 * @throws MmsEncodingException 	 */	private void buildMMS() throws MmsMessageException, MmsEncodingException{		Random r = new Random();		int tid = r.nextInt(999999999);				MmsMessage mms = new MmsMessage();		mms.setMessageSender(senderNumber, MmsMessage.MMS_ADDRESS_TYPE_MOBILE_NUMBER);		mms.addMessageReceiver(destNumber, MmsMessage.MMS_ADDRESS_TYPE_MOBILE_NUMBER);				mms.setTransactionID(String.valueOf(tid));		mms.setMessageDate(System.currentTimeMillis());		mms.setMessageSubject(subject);		mms.setMessageContentType(MmsMessage.CTYPE_APPLICATION_MULTIPART_MIXED);		mms.setMessageType(MmsMessage.MMS_MESSAGE_TYPE_SEND_REQUEST);		mms.setVersion(MmsMessage.MMS_VERSION_1);				Iterator<ContentEntry> i = contentEntries.iterator();		ContentEntry e;		MmsPart p;		while(i.hasNext()){			e = i.next();			p = new MmsPart();			p.setPartContent(e.getContent());						if (e.getType() == ContentEntry.TEXT_FILE || e.getType() == ContentEntry.TEXT_STRING){				p.setPartContentType(MmsMessage.CTYPE_TEXT_PLAIN);			}else{				p.setPartContentType(MmsMessage.CTYPE_IMAGE_JPEG);				}						mms.addPart(p);		}				MmsEncoder mmse = new MmsEncoder(mms);		this.encodedMms = mmse.encodeMessage();	}		/**	 * Sends the encoded mms	 * @throws IllegalStateException 	 * @throws IOException 	 * @throws MmsMessageException 	 * @throws MmsDecoderException 	 */	private void sendMMS(String wapGatewayHost, int wapGatewayPort, String servlet) throws IllegalStateException, IOException, MmsDecoderException, MmsMessageException {		WAPClient wapClient = new WAPClient(wapGatewayHost, wapGatewayPort);		PostRequest request = new PostRequest(servlet);		request.setContentType("application/vnd.wap.mms-message");		request.setRequestBody(this.encodedMms);				if (verbose) System.out.println("Connecting to \"" + wapGatewayHost +"\":"+wapGatewayPort +"...");		wapClient.connect();				if (verbose) System.out.println("Sending mms message through \"" + servlet +"\"...");		Response response = wapClient.execute(request);		byte[] binaryMms = response.getResponseBody();		wapClient.disconnect();		MmsDecoder dec = new MmsDecoder(binaryMms);		MmsMessage mms = dec.decodeMessage();		if (!mms.getResponseStatus().equals(MmsMessage.MMS_RESPONSE_STATUS_OK)){			throw new MmsMessageException("Message not sent: error="+mms.getResponseStatus()+"; description="+mms.getResponseText());		}		if (verbose) System.out.println("Message sent!");	}			/**	 * Parse the command line	 * @param args command line	 */	public static void main(String args[]){		String wapGatewayHost = null;		int wapGatewayPort = -1;		String mmsServlet = null;				String senderNumber = null;		String destNumber = null;		String subject = null;		ArrayList<ContentEntry> contentEntries = new ArrayList<ContentEntry>();		int imageMaxSize=0;

⌨️ 快捷键说明

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