• Struts2多文件上传


      Struts2上传多文件也很简单。在Action中把文件对应的属性用数组或者集合接收就可以了。

      File[] file; 

      String[] fileFileName;

      String[] fileContentType;

      1、FileUploadAction的代码

    package cn.luxh.struts2.action;
    
    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    
    /**
     * 上传文件
     * @author Luxh
     */
    public class FileUploadAction extends ActionSupport {
    
    	private static final long serialVersionUID = 2642160699551232611L;
    	
    	private static Log LOG = LogFactory.getLog(FileUploadAction.class);
    	
    	protected File[] file;
    	protected String[] fileFileName;
    	protected String[] fileContentType;
    	
    	
    	/**
    	 * 上传文件
    	 */
    	public String upload() {
    		try {
    			File[] files = uploadFile("/upload");
    			//after get files
    			//do some work
    			//...
    		}catch(Exception e) {
    			LOG.error("上传文件出错!");
    			throw new RuntimeException("上传文件出错");
    		}
    		return SUCCESS;
    	}
    	
    	/**
    	 *	处理上传的文件
    	 * @param saveDir
    	 * @return 
    	 * @throws IOException 
    	 */
    	public File[] uploadFile(String saveDir) throws IOException {
    		if(saveDir==null ||"".equals(saveDir.trim())) {
    			saveDir = "/upload";
    		}
    		File[] files = null;
    		if(file != null && file.length > 0) {
    			String saveDirPath = ServletActionContext.getServletContext().getRealPath(saveDir);
    			File dirFile = new File(saveDirPath);
    			if(!dirFile.exists()) {
    				dirFile.mkdir();
    			}
    			files = new File[file.length];
    			for(int i=0;i<file.length;i++) {
    				if(file[i] != null) {
    					String newFileName = generateFileName(fileFileName[i]);
    					File destFile = new File(saveDirPath,newFileName);
    					FileUtils.copyFile(file[i], destFile);
    					files[i] = destFile;
    				}
    			}
    		}
    		return files;
    	} 
    	
    	/**
    	 * 	生成文件名
    	 * @param fileName
    	 * @return
    	 */
    	private String generateFileName(String fileName) {
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    		String formatDate = sdf.format(new Date());
    		int position = fileName.lastIndexOf("."); 
    		String extension = "";
    		if(position!=-1) {
    			extension = fileName.substring(position); 
    		}
    		return formatDate + extension;     
    	}
    	
    	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;
    	}
    
    }
    

      

      2、上传页面upload.jsp

    <form  action="${pageContext.request.contextPath}/fileUpload/upload" method="post" enctype="multipart/form-data">
    			<table>
    				 <tr>
    			         <td>
    			         		请选择文件:
    			         </td>
    			     </tr>
    			     <tr>
    			         <td>
    			             <input type="file" name="file">
    			         </td>
    			     </tr>
    			     <tr>
    			         <td>
    			             <input type="file" name="file">
    			         </td>
    			     </tr>
    			     <tr>
    			         <td>
    			             <input type="file" name="file">
    			         </td>
    			     </tr>
    			</table>
    			<tr>
    				<td>
    					<input type="submit" value="Submit" id="submit">
    				</td>
    			</tr>
    </form>
    

      3、配置文件struts.xml

    <action name="upload" class="cn.luxh.struts2.action.FileUploadAction" method="upload">
                <result>/WEB-INF/pages/common/success.jsp</result>
    </action>
    

      

      

  • 相关阅读:
    centos8 将SSSD配置为使用LDAP并要求TLS身份验证
    Centos8 搭建 kafka2.8 .net5 简单使用kafka
    .net core 3.1 ActionFilter 拦截器 偶然 OnActionExecuting 中HttpContext.Session.Id 为空字符串 的问题
    Springboot根据不同环境加载对应的配置
    VMware Workstation12 安装 Centos8.3
    .net core json配置文件小结
    springboot mybatisplus createtime和updatetime自动填充
    .net core autofac依赖注入简洁版
    .Net Core 使用 redis 存储 session
    .Net Core 接入 RocketMQ
  • 原文地址:https://www.cnblogs.com/luxh/p/2566400.html
Copyright © 2020-2023  润新知