审查视图

.svn/pristine/af/af46bc48f8ac50466c15474e0675e3f5d9c2bff4.svn-base 6.0 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
package com.espeed.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.espeed.centre.pojo.YxyUserInfo;
import com.espeed.log.LogClass;
import com.espeed.tool.GetRandom;
import com.espeed.vo.UploadResult;
import com.opensymphony.xwork2.ActionContext;
/**
 * 程序名称:    	EspeedMail_时速邮箱
 * 程序版本:    	V1.0
 * 作    者:    	深圳市科飞时速网络技术有限公司(0755-88843776)
 * 版权所有:    	深圳市科飞时速网络技术有限公司
 * 技术支持:    	Tech@21gmail.com
 * 单元名称:     文件上传
 * 开始时间:    	2013.10.22
 * 程 序 员:    	
 * 最后修改:    
 * 备    注:		文件上传    
 */
public class FileUploadAction extends BaseAction{
	private static final long serialVersionUID = 2660658197476593891L;
	
	/**删除上传附件*/
	public String DeleteAttr(){
		String loginid="erro";
		String domain="erro";
		try {
			//用户session
			ActionContext ac = ActionContext.getContext();
			Map<String,Object> sess = ac.getSession();			
			YxyUserInfo user=(YxyUserInfo) sess.get("yxyuser");
			loginid=user.getLogin_id();//用户账号
			domain=user.getDomain();//用户所属域名
			
			String fileName=filePath.split("#####")[0];
			String root = ServletActionContext.getRequest().getRealPath("/mailtempfile");
			File file = new File(root+"\\"+domain+"\\"+loginid+"\\attachment"+"\\"+fileName);		
			if(file.exists()){
				file.delete();
			}
			result="1";
		} catch (Exception e) {
			result="0";
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
	        String str = sw.toString();
	        LogClass.errolog(str,loginid+"@"+domain);
		}
		return SUCCESS;
	}

	/**上传附件*/
	public String uploadAttr(){
		String loginid="erro";
		String domain="erro";
		try {
			//获取用户session
			ActionContext ac = ActionContext.getContext();
			Map<String,Object> sess = ac.getSession();			
			YxyUserInfo user=(YxyUserInfo) sess.get("yxyuser");
			loginid=user.getLogin_id();//用户账号
			domain=user.getDomain();//用户所属域名
			
			//项目路径
			String pathew = this.getClass().getClassLoader().getResource("/").getPath();
			pathew = pathew.substring(1, pathew.length());
			pathew = pathew.replace("/WEB-INF/classes/","");
			
			//时间定义
			SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmssSSS");

			//循环处理附件
			String filetype="";//附件格式
			
			String []isUserType={"doc","docx","xls","xlsx","txt","pdf","rar","zip","jpg","wps","png","gif","psd"};
			
			boolean typeisOk=false;//格式是否正确
			//附件存储路径
			String attrSavePath="";
			
			//附件大小
			long attrSize=0;
			typeisOk=false;
			//判断文件格式是否正确
			int s=attrfilename.lastIndexOf(".");
			filetype=attrfilename.substring(s + 1);//后缀
			//格式验证
			for(int j=0;j<isUserType.length;j++){
				if(filetype.equals(isUserType[j])){
					typeisOk=true;
					break;
				}
			}
			//返回对象
			UploadResult resultAttr=new UploadResult();
			//符合格式
			if(typeisOk){
				//判断大小
				attrSize=attrfile.length()/1024;//KB
				if(attrSize==0){
					attrSize=1;
				}
				if(attrSize>uploadBig){
					//返回数据
					resultAttr.setOldFileName(attrfilename);
					resultAttr.setResult(2);
					resultAttr.setSize(attrSize);
				}else{
					//附件存放路径
					attrSavePath=pathew+"/mailtempfile/"+domain+"/"+loginid+"/attachment";
					//附件名(当前时间)					
					String nowdate=df.format(new Date())+GetRandom.getRandomString(10);
					//保存该文件
					try {
						File f=new File(attrSavePath);
						f.mkdirs();					
						FileOutputStream fout = new FileOutputStream(attrSavePath + "\\"+ nowdate+"."+filetype);
						FileInputStream fin = new FileInputStream(attrfile);
						byte[] buffer = new byte[1024];
						int len = 0;
						while ((len = fin.read(buffer)) > 0) {
							fout.write(buffer, 0, len);
						}
						fout.flush();
						fout.close();
						fin.close();
						//成功后返回数据
						resultAttr.setNewFileName(nowdate+"."+filetype);
						resultAttr.setOldFileName(attrfilename);
						resultAttr.setResult(1);
						resultAttr.setSize(attrSize);
						resultAttr.setDivId(GetRandom.getRandomString(2));
						
					} catch (Exception e) {
						e.printStackTrace();
					}
				}	
			}else{
				//返回格式不正确信息
				resultAttr.setOldFileName(attrfilename);
				resultAttr.setResult(0);
				resultAttr.setSize(attrSize);
			}
			uploadResult.add(resultAttr);
			
		} catch (Exception e) {
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw, true));
	        String str = sw.toString();
	        LogClass.errolog(str,loginid+"@"+domain);
		}
		return "attr";
	}
	
	private String result;
	private String filePath;
	private File attrfile;//导入的文件路径
	private String attrfilename;//上传文件名
	private int uploadBig;//附件上传大小
	List<UploadResult> uploadResult=new ArrayList<UploadResult>();
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public int getUploadBig() {
		return uploadBig;
	}
	public void setUploadBig(int uploadBig) {
		this.uploadBig = uploadBig;
	}
	public List<UploadResult> getUploadResult() {
		return uploadResult;
	}
	public void setUploadResult(List<UploadResult> uploadResult) {
		this.uploadResult = uploadResult;
	}
	public File getAttrfile() {
		return attrfile;
	}
	public void setAttrfile(File attrfile) {
		this.attrfile = attrfile;
	}
	public String getAttrfilename() {
		return attrfilename;
	}
	public void setAttrfilename(String attrfilename) {
		this.attrfilename = attrfilename;
	}
}