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

📄 uploadaction.java

📁 OBPM是一个开源
💻 JAVA
字号:
package cn.myapps.core.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import cn.myapps.constans.Environment;
import cn.myapps.util.property.DefaultProperty;
import cn.myapps.util.sequence.Sequence;

import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;

public class UploadAction extends ActionSupport implements Action {
	private File upload;

	private String uploadContentType;

	private String uploadFileName;

	private String path;

	private String newUploadFileName;

	private String webPath;

	private String allowedTypes;

	private static final String IMAGE_TYPES[] = { "image/png", "image/gif",
			"image/jpeg", "image/pjpeg" };

	public String getAllowedTypes() {
		return allowedTypes;
	}

	public void setAllowedTypes(String allowedTypes) {
		this.allowedTypes = allowedTypes;
	}

	public String getWebPath() {
		return webPath;
	}

	public void setWebPath(String webPath) {
		this.webPath = webPath;
	}

	public String getPath() throws Exception {
		return this.path;
	}

	public void setPath(String path) throws Exception {
		this.path = path;
	}

	public String getNewUploadFileName() {
		return newUploadFileName;
	}

	public void setNewUploadFileName(String newUploadFileName) {
		this.newUploadFileName = newUploadFileName;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	private String getSavePath() throws Exception { // 获取文件保存真实路径
		// 获取文件后缀名
		int index = uploadFileName.indexOf(".");
		String suffix = uploadFileName
				.substring(index, uploadFileName.length());

		// 设置文件保存的名称
		newUploadFileName = Sequence.getSequence() + suffix;

		String dir = DefaultProperty.getProperty(path);
		String savePath = getEnvironment().getWebcontextRealPath(dir);

		// 设置文件网络路径
		webPath = DefaultProperty.getProperty(path) + newUploadFileName + "_"
				+ uploadFileName;

		return savePath;
	}

	public String execute() throws Exception {
		if (!isAllowedType()) {
			addFieldError("", "The file type not allow,must be " + allowedTypes);
			return INPUT;
		}

		String savePath = getSavePath();

		if (!(new File(savePath).isDirectory())) {
			new File(savePath).mkdirs();
		}
		savePath += newUploadFileName;

		if (upload != null) {
			FileOutputStream outputStream = new FileOutputStream(savePath);

			FileInputStream fileIn = new FileInputStream(upload);
			byte[] buffer = new byte[1024];
			int len;
			while ((len = fileIn.read(buffer)) > 0) {
				outputStream.write(buffer, 0, len);
			}
			fileIn.close();
			outputStream.close();
		}

		return SUCCESS;
	}

	private boolean isAllowedType() {
		if (allowedTypes != null && allowedTypes.trim().length() > 0) {
			if (allowedTypes.equalsIgnoreCase("image")) {
				for (int i = 0; i < IMAGE_TYPES.length; i++) {
					if (uploadContentType.equalsIgnoreCase(IMAGE_TYPES[i])) {
						return true;
					}
				}
			}
		} else {
			return true;
		}

		return false;
	}

	public String doDelete() throws Exception {
		ActionContext context = ActionContext.getContext();
		String[] fileWebPaths = (String[]) context.getParameters().get(
				"fieldValue");

		if (fileWebPaths[0] != null && fileWebPaths[0].trim().length() > 0) {
			String fileRealPath = getEnvironment().getWebcontextRealPath(
					fileWebPaths[0]);
			File file = new File(fileRealPath);
			if (file.exists()) {
				setNewUploadFileName("");
				file.delete();
			}
		}

		return SUCCESS;
	}

	public static String toUtf8(String str) {
		try {
			// byte[] tmp = str.getBytes("GBK");
			// str = new String(tmp,"ISO8859_1");
			str = URLEncoder.encode(str, "utf-8");

		} catch (UnsupportedEncodingException e) {
		}

		return str;
	}

	public static String toGBK(String str) {
		try {
			str = URLDecoder.decode(str, "utf-8");

			byte[] tmp = str.getBytes("ISO8859_1");

			str = new String(tmp, "GBK");

		} catch (UnsupportedEncodingException e) {
		}

		return str;
	}

	public Environment getEnvironment() {
		Environment evt = (Environment) ActionContext.getContext()
				.getApplication().get(Environment.class.getName());

		return evt;
	}
}

⌨️ 快捷键说明

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