电信增值业务一之彩信 实验报告 任课教师: 实验地点:实验时间: 小组成员:实验二 网页版彩信发送页面(修改版)代码:Client.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
彩信发送页面
彩信填写
附加文件选择
Index.jsp<%request.getRequestDispatcher("/http/MmsService").forward(request, response);%>实验截图:实验三 实验代码:MMS.javapackage com.cq.view;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.FlowLayout;import java.io.File;import java.io.;import java.io.;import java.io.;import java.io.IOException;import java.io.InputStream;import javax.swing.*;import javax.swing.;import com.cq.main.ClientInit;impormonUtil;import sun.misc.BASE64Encoder;public class MMS extends JFrame {private String sendAddress = ""; private String userName = "2010214442"; private String password = "123"; private String serviceId = "2"; private String channelId = "1"; private String phone = ""; private String subject = "重庆早报(20120114)"; String ; JFrame jf = new JFrame("发彩信");//顶层容器 JPanel jp = new JPanel(); //面板容器 JLabel l_name = new JLabel("号码:"); static JTextField t_name = new JTextField(10); JButton seleFile = new JButton("附件"); JLabel lb = new JLabel("短信内容: "); static JTextArea t_test = new JTextArea(12,18); JButton send = new JButton("发送"); JButton cancel = new JButton("取消");public MMS() { jp.add(l_name); jp.add(t_name); jp.add(seleFile); jp.add(lb); jp.add(t_test); jp.add(send); jp.add(cancel); jf.add(jp);// 事件OK按钮监听 sele(new ActionListener() {public void actionPerformed(ActionEvent e) { J = new J("D:\\"); int returnVal = (); = ().getAbsolutePath();// 这个就是你选择的文件夹的路径 System.out.println();}});send.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { send();}});// 事件Cancel按钮监听 cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0);}});jf.setVisible(true);jf.setSize(230, 350); jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE); jf.setLocation(500, 270);}public void send(String ) { ClientInit client = new ClientInit(); // 1.获取下发内容String content = client.getMMSContent();// "D:/test/mms.zip"// 2.下发 StringBuffer sendParam = new StringBuffer(); phone = t_name.getText(); sendParam.append("phone=" + phone); sendParam.append("&userName=" + userName); sendParam.append("&password=" + CommonUtil.MD5Encode(password)); sendParam.append("&serviceId=" + serviceId);sendParam.append("&channelId=" + channelId); sendParam.append("&subject=" + subject); sendParam.append("&content=" + content); String resp = CommonUtil.connectURL(String.valueOf(sendParam), sendAddress);System.out.println("Send MMS response:" + resp);} public String getMMSContent(String path) { String result = ""; InputStream is = null; try {BASE64Encoder encoder = new BASE64Encoder(); is = new (new )); byte[] bAry = new byte[is.available()]; is.read(bAry); result = encoder.encode(bAry); } catch (Exception e) {System.out.println(e);} finally {if (null != is) {try {is.close();} catch (IOException e) { System.out.println(e); }}}return result;} public static void main(String[] args) { new MMS();}}ClientInit.javapackage com.cq.main;import java.io.File;import java.io.;import java.io.IOException;import java.io.InputStream;import sun.misc.BASE64Encoder;impormonUtil;public class ClientInit { private static String sendAddress = ""; private static String userName = "2010214442"; private static String password = "123"; private static String serviceId = "2"; private static String channelId = "1"; private String phone = ""; private String subject = "重庆早报(20120114)"; public static void main(String[] args) { ClientInit client = new ClientInit(); // 1.获取下发内容 String content = client.getMMSContent("D:/test/mms.zip"); // 2.下发 StringBuffer sendParam = new StringBuffer(); sendParam.append("phone=" + client.phone); sendParam.append("&userName=" + userName); sendParam.append("&password=" + CommonUtil.MD5Encode(password)); sendParam.append("&serviceId=" + serviceId); sendParam.append("&channelId=" + channelId); sendParam.append("&subject=" + client.subject); sendParam.append("&content=" + content); String resp = CommonUtil.connectURL(String.valueOf(sendParam), sendAddress);System.out.println("Send MMS response:" + resp);} public String getMMSContent(String path) { String result = ""; InputStream is = null; try { BASE64Encoder encoder = new BASE64Encoder(); is = new (new )); byte[] bAry = new byte[is.available()]; is.read(bAry); result = encoder.encode(bAry); } catch (Exception e) { System.out.println(e); } finally { if (null != is) { try { is.close(); } catch (IOException e) { System.out.println(e); }}}return result;}}CommonUtil.javapackage com.cq.util;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.OutputStream;impor.HttpURLConnection;impor.URL;import java.security.MessageDigest;public class CommonUtil { public static String connectURL(String commString, String address) { HttpURLConnection urlConn = null; String rec_string = ""; URL url = null; try { url = new URL(address);urlConn = (HttpURLConnection) url.openConnection(); urlConn.setConnectTimeout(30000); urlConn.setReadTimeout(30000); urlConn.setRequestMethod("POST"); urlConn.setDoOutput(true);OutputStream out = urlConn.getOutputStream(); out.write(commString.getBytes("UTF-8")); out.flush();out.close();BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "UTF-8")); StringBuffer sb = new StringBuffer(); int ch; while ((ch = rd.read()) > -1) { sb.append((char) ch);} rec_string = sb.toString().trim(); rd.close(); } catch (Exception e) { rec_string = "-107"; } finally { if (urlConn != null) { urlConn.disconnect(); }} return rec_string;} public static String MD5Encode(String sourceString) { String resultString = null; try { resultString = new String(sourceString); MessageDigest md = MessageDigest.getInstance("MD5"); resultString = byte2hexString(md.digest(resultString.getBytes())); } catch (Exception ex) { System.out.println(ex); } return resultString;} private static String byte2hexString(byte[] bytes) {StringBuffer bf = new StringBuffer(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { if ((bytes[i] & 0xff) < 0x10) { bf.append("0");} bf.append(Long.toString(bytes[i] & 0xff, 16)); } return bf.toString();}}JdbcUtil.javapackage com.cq.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class JdbcUtil { public static final int CONNTYPE_MYSQL = 1; public static Connection getConnect(String ip, String db, String user, String password) { return getConnect(CONNTYPE_MYSQL, ip, 3306, db, user, password);} public static Connection getConnect(int type, String ip, int port, String db, String user, String password) { Connection conn = null; String driver = null; String url = null; switch (type) { case CONNTYPE_MYSQL: driver = "com.mysql.jdbc.Driver"; url = "jdbc:mysql://" + ip + ":" + port + "/" + db; break; } if (null != driver && null != url) { try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); } catch (Exception e) { System.out.println(e); }}return conn;} public static void closeConnect(Connection conn) { if (null != conn) { try { conn.close(); } catch (SQLException e) { System.out.println(e);}}}public static ResultSet query(Connection conn, String sql) { Statement st = null; ResultSet rs = null; try { st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = st.executeQuery(sql); } catch (Exception e) { System.out.println(e);} return rs;}}实验截图:实验四实验代码:Form1.csusing System;using System.Collections.Generic;usinponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;usin;usin.Sockets;using System.IO;namespace demo{ public partial class Form1 : Form { private static String sendAddress; private static String userName; private static String password; private static String serviceId; private static String channelId; public String phone; public String subject; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { textBox1.Text = ""; textBox2.Text = "2010214787"; textBox3.Text = "***"; textBox4.Text = "2"; textBox5.Text = "1"; textBox6.Text = ""; textBox7.Text = "重庆邮电大学欢迎您"; textBox8.Text = "D:/mms.zip"; comboBox1.SelectedIndex = 0;} public String getMMSContent(String path) { string result=""; byte[] b_data = new byte[2500]; char[] c_data = new char[2500]; try{ lock (this){ if ((path) == false){ MessageBox.Show("文件不存在", "提示信息");} else{ fs = new (path, );fs.Seek(0, SeekOrigin.Begin); fs.Read(b_data, 0, 2000);Decoder d = Encoding.UTF8.GetDecoder();d.GetChars(b_data, 0, b_data.Length, c_data, 0);result = new string(c_data); }}return result;} catch (IOException e){return(e.Message);}}private void button1_Click(object sender, EventArgs e) { sendAddress = textBox1.Text.Trim(); userName = textBox2.Text.Trim(); password = textBox3.Text.Trim(); serviceId = textBox4.Text.Trim(); channelId = textBox5.Text.Trim(); phone = textBox6.Text.Trim(); subject = textBox7.Text.Trim(); string path = textBox8.Text.Trim(); string content = getMMSContent(path); IPEndPoint point = new IPEndPoint(IPAddress.Parse("172.22.118.16"), 8080); string sendParam = "phone=" + phone + "&userName=" + userName + "&password=" + password + "&serviceId=" + serviceId + "&channelId=" + channelId + "&subject=" + subject + "&content=" + content; try{ switch (comboBox1.SelectedIndex){ case 0:string data=CommonUtil.connectURL(sendParam, sendAddress);MessageBox.Show("成功发送:"+data,"结果"); break;case 1:string msg=mySockets.send(sendParam, point); MessageBox.Show("返回:"+msg,"结果"); break;default:MessageBox.Show("请选择正确的发送方式!", "提示信息"); break; }} catch(Exception ex){ MessageBox.Show(ex.Message,"警告信息"); }}private void button2_Click(object sender, EventArgs e){if (open() == DialogResult.OK){ textBox8.Text = open;} }private void label2_Click(object sender, EventArgs e) { }private void label3_Click(object sender, EventArgs e){ } }}mySocket.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;usin;usin.Sockets;namespace demo{ class mySockets{ public static string send(string message,IPEndPoint point){ NetworkStream stream; byte[] buff=new byte[512]; string msg = ""; try{ TcpClient client = new TcpClient(); client.Connect(point); if (client.Connected){byte[] buff2 = Encoding.UTF8.GetBytes(message); stream = client.GetStream();stream.Write(buff2, 0, buff2.Length); int bytes=stream.Read(buff,0,512); if(bytes>0){ msg = Encoding.UTF8.GetString(buff); } client.Close(); }} catch(Exception ){ throw new Exception();} return msg;} }}CommonUtil.csusing System;using System.Collections.Generic;usinponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;usin;using System.Security.Policy;namespace demo{ public class CommonUtil{public static string connectURL(string cString, String address){WebRequest wr = WebRequest.Create(address); wr.Method = "post";StringBuilder urlEncoded = new StringBuilder(); byte[] somebytes = null; if (cString != null){somebytes = Encoding.UTF8.GetBytes(cString);Stream newStream = wr.GetRequestStream();newStream.Write(somebytes, 0, cString.Length); newStream.Close();}else wr.ContentLength = 0; string re = ""; try{WebResponse result = wr.GetResponse();Stream ReceiveStram = result.GetResponseStream(); Byte[] read = new Byte[512]; int bytes = ReceiveStram.Read(read, 0, 512); if(bytes > 0){ re = Encoding.UTF8.GetString(read); }} catch (Exception ){ throw new Exception();} return re; } }}实验截图:8 / 8文档可自由编辑打印实验小组分工:网页版彩信发送页面代码调试修改(陈诚,杨欢);网页版彩信发送界面的优化(杨欢);MMS代码的调试修改(陈诚);MMS代码实现的测试(杨欢);TCP相关代码的了解与实现(杨欢);SOCKET代码的调试及实现(陈诚);实验总结:总体来说,几次实验让我们了解了关于彩信发送的具体实现方法,熟悉了通信增值业务的开发流程,并完成了交给的任务。
通过完成这些实验,在这个过程中,明白了很多以前似懂非懂的知识,感觉收获很大但是在很多方面还是发现了的不足,下来要多花时间加强下自己的专业方面的知识在操作方面 ,还略显不足 ,不够熟练 ,以后会多花时间加强知识的积累以及操作的练习 。