af46bc48f8ac50466c15474e0675e3f5d9c2bff4.svn-base 6.0 KB
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;
	}
}