首页>代码>s2sh高仿百度文库实现文档在线预览>/仿百度文库实现文档在线预览/onlineView/src/com/eda/test/action/FileAction.java
package com.eda.test.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.eda.test.action.model.FileModel;
import com.eda.test.common.ConStant;
import com.eda.test.common.FileType;
import com.eda.test.common.GerneralSessionAction;
import com.eda.test.entity.File;
import com.eda.test.util.FileUtil;
import com.eda.test.util.FileUtils;

/**
 * 文件处理Action
 * 
 * @author Lanxiaowei
 * @createTime 2012-6-22 下午05:04:29
 */
public class FileAction extends GerneralSessionAction<FileModel> {

	/**
	 * 跳至首页 方法摘要:这里一句话描述方法的用途
	 * 
	 * @param
	 * @return String
	 */
	public String toUpload() {
		return "index";
	}

	/**
	 * 上传文件 方法摘要:这里一句话描述方法的用途
	 * 
	 * @param
	 * @return String
	 */
	public String uploadFile() {
		if(isEmptyFile()){
			this.getModel().setMessage("不允许上传空文件!");
			return "toUpload";
		}
		if(checkMaxFileSize()){
			this.getModel().setMessage("上传文件大小限制在20M以内!");
			return "toUpload";
		}
		if (checkFileType()) {
			File file = createFile();
			boolean isSuccess = getFileService().saveFile(file, this.getModel().getDoc());
			this.getModel().setFile(file);
			return "toList";
		} else {
			this.getModel().setMessage("该文件类型不允许上传!");
			return "toUpload";
		}

	}

	/**
	 * 文件预览 方法摘要:这里一句话描述方法的用途
	 * 
	 * @param
	 * @return String
	 */
	public String view() {
		return "view";
	}

	/**
	 * 查询所有文件 方法摘要:这里一句话描述方法的用途
	 * 
	 * @param
	 * @return String
	 */
	public String list() {
		this.getModel().setFileList(getFileService().findAll());
		return "list";
	}

	/**
	 * 创建File对象 方法摘要:这里一句话描述方法的用途
	 * 
	 * @param
	 * @return File
	 */
	private File createFile() {
		/** 文件上传至服务器存放路径 */
		String UPLOAD_DIR = getProjectRealPath() + ConStant.UPLOAD_BASE_DIR + "\\";
		String BASE_PATH = getBasePath() + ConStant.UPLOAD_BASE_DIR + "/";
		File file = new File();
		String fileName = this.getModel().getDocFileName();
		file.setFileName(fileName);
		String temp = getUploadFileName(fileName, null);
		String swfBasePath = BASE_PATH + temp;
		file.setDistPath(UPLOAD_DIR + temp);
		temp = FileUtils.getFileNameNoStuffix(temp) + "." + ConStant.SWF_STUFFIX;
		swfBasePath = BASE_PATH + temp;
		file.setWebBasePath(swfBasePath);
		file.setDescription(this.getModel().getDescription());
		file.setFileSize(this.getModel().getDoc().length());
		file.setUploadDate(new Date());
		String stuffix = FileUtil.getFileSufix(fileName);
		file.setFileType(getFileTypeName(stuffix));
		return file;
	}

	/**
	 * 生成上传后文件名称 方法摘要:这里一句话描述方法的用途
	 * 
	 * @param
	 * @return String
	 */
	public String getUploadFileName(String orignalFileName, String extension) {
		String stuffix = extension;
		if (null == extension || "".equals(extension)) {
			stuffix = FileUtil.getFileSufix(orignalFileName);
		}
		Date currentDate = new Date();
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return dateFormat.format(currentDate) + "." + stuffix;
	}

	/**
	 * 获取文件类型名称 方法摘要:这里一句话描述方法的用途
	 * 
	 * @param
	 * @return String
	 */
	private String getFileTypeName(String fileStuffix) {
		String fileTypeName = "";
		if (fileStuffix.equals(FileType.WORD2003.getStuffix())) {
			fileTypeName = FileType.WORD2003.getValue();
		} else if (fileStuffix.equals(FileType.WORD2007.getStuffix())) {
			fileTypeName = FileType.WORD2007.getValue();
		} else if (fileStuffix.equals(FileType.EXCEL2003.getStuffix())) {
			fileTypeName = FileType.EXCEL2003.getValue();
		} else if (fileStuffix.equals(FileType.EXCEL2007.getStuffix())) {
			fileTypeName = FileType.EXCEL2007.getValue();
		} else if (fileStuffix.equals(FileType.PPT2003.getStuffix())) {
			fileTypeName = FileType.PPT2003.getValue();
		} else if (fileStuffix.equals(FileType.PPT2007.getStuffix())) {
			fileTypeName = FileType.PPT2007.getValue();
		} else if (fileStuffix.equals(FileType.TXT.getStuffix())) {
			fileTypeName = FileType.TXT.getValue();
		} else {
			fileTypeName = "未知类型";
		}
		return fileTypeName;
	}

	/**
	 * 检查文件类型是否允许上传 方法摘要:这里一句话描述方法的用途
	 * 
	 * @param
	 * @return boolean
	 */
	private boolean checkFileType() {
		String fileType = this.getModel().getDocContentType();
		if (null == fileType || fileType.equals("")) {
			return false;
		}
		String[] allowTypes = this.getModel().getAllowTypes().split(",");
		for (int i = 0; i < allowTypes.length; i++) {
			if (allowTypes[i].equals(fileType)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 检查文件大小
	 *方法摘要:这里一句话描述方法的用途
	 *@param
	 *@return boolean
	 */
	private boolean checkMaxFileSize(){
		if(this.getModel().getMaxUploadSize() < this.getModel().getDoc().length()){
			return true;
		}
		return false;
	}
	
	/**
	 * 检查是否空文件
	 *方法摘要:这里一句话描述方法的用途
	 *@param
	 *@return boolean
	 */
	private boolean isEmptyFile(){
		return this.getModel().getDoc().length() == 0;
	}
}
最近下载更多
东北虎啸  LV3 2023年2月22日
49202046  LV7 2022年8月2日
Comedian  LV3 2022年7月19日
dududufeidad  LV9 2021年10月27日
yiyun0014  LV4 2021年5月7日
and123456  LV11 2021年4月20日
幻羽揚  LV4 2021年1月3日
2235140624  LV17 2020年11月24日
liujun0104  LV4 2020年11月8日
fjx1996820  LV5 2020年8月17日
最近浏览更多
小王wang  LV10 2月29日
lyj001 2023年10月24日
暂无贡献等级
xshxxm1  LV21 2023年3月28日
小新Coding  LV9 2023年3月8日
东北虎啸  LV3 2023年2月22日
kehehee2 2023年2月2日
暂无贡献等级
zeus123 2022年10月26日
暂无贡献等级
暂无贡献等级
honghu201  LV2 2022年9月14日
平淡 2022年9月3日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友