首页 > 代码库 > 基于struts2的ajaxfileupload异步上传插件的使用
基于struts2的ajaxfileupload异步上传插件的使用
实例:
jsp页面
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html"> <title>文件上传</title> <link rel="stylesheet" href=http://www.mamicode.com/"${pageContext.request.contextPath }/js/AjaxFileUploaderV2.1/ajaxfileupload.css">>Action代码
package org.zsm.learn.struts2.action; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import org.apache.commons.io.IOUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport { private static final long serialVersionUID = -1063647163701747470L; // 与上传文件相关的三个参数 private File file; private String fileFileName; private String fileContentType; // Action相关参数的设置 private String savePath; private long maximumSize; private String allowedTypes; // 返回结果 private Map<String, Object> result; public Map<String, Object> getResult() { return result; } public String upload() { result = new HashMap<String, Object>(); // 判断上传文件是否符合要求 if (getFile() == null) { result.put("success", false); result.put("messages", "上传文件不能为空!!!"); } else if (!validateUploadLength(maximumSize)) { result.put("success", false); result.put("message", "上传文件超出了限制!!!"); } else if (!validateUploadType(allowedTypes)) { result.put("success", false); result.put("message", "上传文件类型不符合要求!!!"); } else { // 获取存储上传目录 String storageDir = ServletActionContext.getServletContext() .getRealPath(savePath); // 判断存储上传目录 File storageFile = new File(storageDir); if (!storageFile.exists()) { storageFile.mkdirs(); } // 上传文件 File destFile = generateDestFile(storageDir, fileFileName); uploadFile(destFile, file); result.put("success", true); result.put("message", "文件上传成功!!!"); } return "success"; } public String ActionError(){ result = new HashMap<String, Object>(); result.put("success", false); result.put("messages", "上传文件超过了限制的大小"); return "actionError"; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public File generateDestFile(String destFileDir, String sourceFileName) { File destDir = new File(destFileDir); if (!destDir.exists()) { destDir.mkdirs(); } // 生成规则 return new File(destDir, sourceFileName); } public static void uploadFile(File destFile, File sourceFile) { InputStream input = null; OutputStream output = null; try { input = new FileInputStream(sourceFile); output = new FileOutputStream(destFile); IOUtils.copy(input, output); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(input); } } /** * 判断是否支持上传文件的类型 * * @param types * @return */ public boolean validateUploadType(String types) { String[] allowedTypes = null; // 获取文件上传类型 String fileType = getFileContentType(); allowedTypes = types.split(","); for (String type : allowedTypes) { if (type.equals(fileType)) { return true; } } return false; } public boolean validateUploadLength(long maximumSize) { if (getFile().length() > maximumSize) { return false; } return true; } public String getSavePath() { return savePath; } public void setSavePath(String savePath) { this.savePath = savePath; } public long getMaximumSize() { return maximumSize; } public void setMaximumSize(long maximumSize) { this.maximumSize = maximumSize; } public String getAllowedTypes() { return allowedTypes; } public void setAllowedTypes(String allowedTypes) { this.allowedTypes = allowedTypes; } }struts.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- <constant name="struts.custom.i18n.resources" value=http://www.mamicode.com/"ApplicationResources" >
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。