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

📄 mmseditaddmimeaction.java

📁 MM7彩信对接网关示例
💻 JAVA
字号:
/*
 * Created on 2005-7-10
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.rainbow.mms.manage;

import java.io.File;
import java.io.FileInputStream;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;
import com.rainbow.mms.common.MimeContent;
import com.rainbow.mms.common.MmsSmilPageUnitCollection;
import com.rainbow.mms.common.QueryMimeContentSaveStrategy;
import com.rainbow.util.tools.HibernateUtil;

/**
 * @author Rainbow MMS Group Leader —— TrWorks
 *
 * 向已有的彩信内容体中添加彩信内容体元素
 */
public class MmsEditAddMimeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private static final int MAXUPLOAD_SIZE = 50*1024;
	private static final int MAXMMS_SIZE = 50*1024;
	
	private static final String fileFieldName = "filefield";
	private static final String textFieldName = "textfield";
	private static final String referenceFieldName = "reference";
	
	private Logger log = Logger.getLogger(MmsEditAddMimeAction.class);

	public String execute() throws Exception {
		
		MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest();
		
		String addMimeID = multiWrapper.getParameter("addMimeID");
		String pageNum = multiWrapper.getParameter("pageNum");
		String pageInsertPos = multiWrapper.getParameter("pageInsertPos");
		String radioValue = multiWrapper.getParameter("radioValue");
		System.out.println("radioValue" + radioValue);
		
		MmsSmilPageUnitCollection collection = (MmsSmilPageUnitCollection)ServletActionContext.getContext().getSession().get("mmsPageMapCollection");
		if (collection == null || addMimeID == null || pageNum == null || radioValue == null){
			log.error("MmsEditAddMimeAction excute function's input params error!\r\n" +
					"collection:" + collection + "\r\naddMimeID:" + addMimeID + 
					"\r\npageNum:" + pageNum + "\r\nradioValue:" + radioValue);
			return ERROR;
		}
		
		// 计算当前彩信内容体的大小,并且求出应该插入的位置
		int pageSize = 0;
		int nFindPos = 0;
		for (int i = 0; i < collection.getOrgMmsContent().getMmsContentElments().size(); i++){
			MimeContent mime = (MimeContent)collection.getOrgMmsContent().getMmsContentElments().get(i);
			if (mime.getMimeContentID() == Integer.parseInt(addMimeID)){
				nFindPos = i;
			}
			int saveStratgy = 0;
			try{
				Session sess = HibernateUtil.currentSession();
				saveStratgy = QueryMimeContentSaveStrategy.getSaveStrategy(mime.getMimeType(), sess);
			}
			catch(Exception e1){
				e1.printStackTrace();
			}
			finally{
				HibernateUtil.closeSession();
			}
			
			if (saveStratgy == 0){
				pageSize = pageSize + mime.getBinaryContent().length;
			}
			else{
				pageSize = pageSize + mime.getCharacterContent().getBytes().length;
			}
		}
		
		// 接收用户输入的纯文本内容
		long tempFileName = System.currentTimeMillis();
		MimeContent content = null;

		String textContent = null;
		String reference = null;
		
		// 接收用户输入的纯文本内容		
		if (radioValue.equalsIgnoreCase("0")){
			if (multiWrapper.getParameter(textFieldName) != null){
				textContent = new String(multiWrapper.getParameter(textFieldName).getBytes("iso8859-1"), "GBK");			
				pageSize = pageSize + textContent.getBytes().length;
				if (pageSize > MAXMMS_SIZE){
					log.error("MmsEditAddMimeAction excute function upload text too large!");
					return ERROR;
				}
				
				content = new MimeContent();
				content.setCreateTime(new Date());
				content.setCharacterContent(textContent);
				content.setBinaryContent(null);
				content.setMimeContentName((String.valueOf(tempFileName)) + 0 + ".txt");
				content.setMimeType("text/plain");			
			}
		}
		else if (radioValue.equalsIgnoreCase("1")){	// 上传文件
			Enumeration e = multiWrapper.getFileParameterNames();

			int m = 0;
		
			// 接收用户选择的上传文件
			if (e == null){
				log.error("MmsEditAddMimeAction excute function upload failed! -- multiWrapper.getFileParameterNames() error!");
				return ERROR;
			}
			
			if (e.hasMoreElements()) {
				
				String inputValue = (String) e.nextElement();
				
				if (inputValue.length() < fileFieldName.length() ||
					!inputValue.substring(0, fileFieldName.length()).equalsIgnoreCase(fileFieldName))
				{
					return ERROR;
				}
	
				String contentType = multiWrapper.getContentTypes(inputValue)[0];
				if (contentType == null || contentType.length() == 0) {
					return ERROR;
				}

				// get the name of the file from the input tag
				String fileName = multiWrapper.getFileSystemNames(inputValue)[0];
				fileName = new String(fileName.getBytes("iso8859-1"), "GBK");
				System.out.println("上传的文件名:" + fileName);
	
				// Get a File object for the uploaded File
				File file = multiWrapper.getFiles(inputValue)[0];
	
				// If it's null the upload failed
				if (file == null) {
					addActionError("Error uploading: "
							+ multiWrapper.getFileSystemNames(inputValue)[0]);
					return ERROR;
				}
	
				FileInputStream infile = new FileInputStream(file);
				int filesize = infile.available();
				if (filesize >= MAXUPLOAD_SIZE){
					log.error("MmsEditAddMimeAction excute function upload file too large!");
					return ERROR;
				}
				byte[] btBuffer = new byte[filesize];
				infile.read(btBuffer);
			
				content = new MimeContent();
				content.setCreateTime(new Date());
				content.setMimeContentName(fileName);
				content.setMimeType(contentType);
				content.setCreateTime(new Date());
				
				// Do additional processing/logging...
				int saveStratgy = 0;
				try{
					Session sess = HibernateUtil.currentSession();
					saveStratgy = QueryMimeContentSaveStrategy.getSaveStrategy(contentType, sess);
				}
				catch(Exception e2){
					e2.printStackTrace();
				}
				finally{
					HibernateUtil.closeSession();
				}
					
				if (saveStratgy == 0) {
					content.setBinaryContent(btBuffer);
					content.setCharacterContent(null);
				} else {
					String strContent = new String((new String(btBuffer))
							.getBytes("iso8859-1"), "GBK");
					
					if (strContent.length() > 8000) {
						log.error("MmsEditAddMimeAction excute function upload text too large!");
						return ERROR;
					}
					content.setCharacterContent(strContent);
					content.setBinaryContent(null);
				}
			}
		}
		else{	// 引用
			if (multiWrapper.getParameter(referenceFieldName) != null){
				reference = new String(multiWrapper.getParameter(referenceFieldName).getBytes("iso8859-1"), "GBK");
			}

			collection.addReference(Integer.parseInt(pageNum), reference, Integer.parseInt(pageInsertPos));

			ServletActionContext.getContext().getSession().put("mmsResult", collection.getOrgMmsContent());

			return SUCCESS;
		}
		
		// 修改彩信内容体内的SMIL
		if (false == collection.addMimeElement(Integer.parseInt(pageNum), content, nFindPos + 1, Integer.parseInt(pageInsertPos))){
			log.error("MmsEditAddMimeAction excute function addMimeElement failed!\r\n" +
					"pageNum:" + pageNum + "\r\ncontent:" + content.toString() + "\r\nFindPos:" + (nFindPos + 1) + "\r\npageInsertPos:" + pageInsertPos);
			return ERROR;
		}
		else{
			ServletActionContext.getContext().getSession().put("mmsResult", collection.getOrgMmsContent());
		}
		
		return SUCCESS;
	}
}

⌨️ 快捷键说明

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