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

📄 mmspagebuildaction.java

📁 MM7彩信对接网关示例
💻 JAVA
字号:
/*
 * Created on 2005-7-2
 *
 * 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 java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;

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.QueryMimeContentSaveStrategy;
import com.rainbow.util.tools.HibernateUtil;

/**
 * 新建彩信内容体的一桢(一页),包括该页显示的时间和里面的彩信内容体元素
 * @author Rainbow MMS Group Leader —— TrWorks
 *
 */
public class MmsPageBuildAction extends ActionSupport {

	private Logger log = Logger.getLogger(MmsPageBuildAction.class);
	
	private static final String timeIntervalFieldName = "timeInterval";

	private static final String pageElementNumFieldName = "pageElementNum";

	private static final String textFieldName = "textfield";

	private static final String fileFieldName = "filefield";
	
	private static final int MAXUPLOAD_SIZE = 50*1024;

	private static final long serialVersionUID = 1L;
	
	private int pageSize = 0;
	
	private TreeMap pageMap = new TreeMap();
	
	private int timeInterval = 0;
	
	public int getTimeInterval() {
		return timeInterval;
	}
	
	public TreeMap getPageMap() {
		return pageMap;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	
	public String execute() throws Exception {

		MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) ServletActionContext
				.getRequest();
		
		// 检查
		if (multiWrapper.hasErrors()) {
			Collection errors = multiWrapper.getErrors();
			Iterator i = errors.iterator();

			while (i.hasNext()) {
				addActionError((String) i.next());
			}

			log.error("MmsPageBuildAction execute function upload file check error!");
			return ERROR;
		}

		// 接收用户选择的该页面的显示时长
		timeInterval = Integer.parseInt(multiWrapper.getParameter(timeIntervalFieldName));
		int pageElementNum = Integer.parseInt(multiWrapper
				.getParameter(pageElementNumFieldName));

		MimeContent content = null;

		int pageSize = 0;
		
		// 接收用户输入的纯文本内容
		for (int i = 0; i < pageElementNum; i++) {
			String textContent = new String(multiWrapper.getParameter(textFieldName + i).getBytes("iso8859-1"), "GBK");
			if (textContent == null || textContent.length() == 0) {
				continue;
			} else {
				long tempFileName = System.currentTimeMillis();
				
				pageSize = pageSize + textContent.getBytes().length;

				content = new MimeContent();
				content.setCreateTime(new Date());
				content.setCharacterContent(textContent);
				content.setBinaryContent(null);
				content.setMimeContentName((String.valueOf(tempFileName)) + i + ".txt");
				content.setMimeType("text/plain");

				pageMap.put(new Integer(i), content);
				
				log.info("New Text File : " + (String.valueOf(tempFileName))
						+ ".txt");
			}
		}

		Enumeration e = multiWrapper.getFileParameterNames();

		int m = 0;
		
		// 接收用户选择的上传文件
		while (e.hasMoreElements()) {
			
			String inputValue = (String) e.nextElement();
			
			if (inputValue.length() < fileFieldName.length() ||
				!inputValue.substring(0, fileFieldName.length()).equalsIgnoreCase(fileFieldName))
			{
				log.error("MmsPageBuildAction excute function fileFieldName error.");
				return ERROR;
			}
			else{
				m = Integer.parseInt(inputValue.substring(fileFieldName.length(), inputValue.length()));
			}
			
			//System.out.println("***** InputValue:" + inputValue);

			String contentType = multiWrapper.getContentTypes(inputValue)[0];
			//System.out.println("***** ContentType:" + contentType);
			if (contentType == null || contentType.length() == 0) {
				continue;
			}

			// get the name of the file from the input tag
			String fileName = multiWrapper.getFileSystemNames(inputValue)[0];
			//System.out.println("***** FileName:" + 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]);
				continue;
			}

			FileInputStream infile = new FileInputStream(file);
			int filesize = infile.available();
			//System.out.println("***** FileSize:" + filesize);
			if (filesize >= MAXUPLOAD_SIZE){
				log.error("MmsPageBuildAction excute function upload file too large!");
				return ERROR;
			}
			byte[] btBuffer = new byte[filesize];
			infile.read(btBuffer);
			
			// Do additional processing/logging...
			int saveStratgy = 0;
			try{
				Session sess = HibernateUtil.currentSession();
				content = new MimeContent();
				content.setCreateTime(new Date());
				saveStratgy = QueryMimeContentSaveStrategy.getSaveStrategy(contentType, sess);
			}
			catch(Exception e1){
				e1.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("MmsPageBuildAction excute function upload textfile too large!");
					return ERROR;
				}
				content.setCharacterContent(strContent);
				content.setBinaryContent(null);
			}
			content.setMimeContentName(fileName);
			content.setMimeType(contentType);
			content.setCreateTime(new Date());

			pageMap.put(new Integer(m), content);
		}		

		// 将这个页面加入到彩信的页面队列中,等待最后生成确定。
		List mmsPageList = (List)ActionContext.getContext().getSession().get("mmsPageList");
		if (mmsPageList == null){
			System.out.println("new mmsPageList list");
			mmsPageList = new LinkedList();
		}
		
		mmsPageList.add(this);
		
		ActionContext.getContext().getSession().put("mmsPageList", mmsPageList);
		
		return SUCCESS;
	}

}

⌨️ 快捷键说明

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