`
fosa0989
  • 浏览: 109726 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

struts2多文件上传

阅读更多
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class UploadRegFile extends ActionSupport implements RequestAware,SessionAware,ServletContextAware{
	private Map<String, Object> request;
	private Map<String, Object> session;
	private ServletContext context;
	
    // 用File数组来封装多个上传文件域对象
    private File[] upload;
    // 用String数组来封装多个上传文件名
    private String[] uploadFileName;
    // 用String数组来封装多个上传文件类型
    private String[] uploadContentType;
    
	/**
	 * 上传文件处理
	 * @return
	 * @throws Exception
	 */
	public String execute()throws Exception
	{
		int i=0;
		String preFile=upload(upload[i], uploadFileName[i++]);
		String bidFile=upload(upload[i], uploadFileName[i++]);
		String durgCat=upload(upload[i], uploadFileName[i++]);

		
		return SUCCESS;
	}    
	
	/**
	 * 上传文件
	 * @param url
	 * @return
	 */
	private String upload(File src,String srcFileName)throws Exception
	{
        String targetDirectory = context.getRealPath("/upload");    
        String targetFileName = generateFileName(srcFileName);    
        File target = new File(targetDirectory, targetFileName);
        String url="/upload/"+targetFileName;

        FileUtils.copyFile(src, target);
        
        return url;
     }	
	/**
	 * 根据当前时间生成存储文件名
	 * @param fileName
	 * @return
	 */
    private String generateFileName(String fileName) {    
        DateFormat format = new SimpleDateFormat("yyMMddHHmmss");    
        String formatDate = format.format(new Date());            
            
        return formatDate + fileName;    
    }  	
 
	/**
	 * @return the upload
	 */
	public File[] getUpload() {
		return upload;
	}

	/**
	 * @param upload the upload to set
	 */
	public void setUpload(File[] upload) {
		this.upload = upload;
	}

	/**
	 * @return the uploadFileName
	 */
	public String[] getUploadFileName() {
		return uploadFileName;
	}

	/**
	 * @param uploadFileName the uploadFileName to set
	 */
	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	/**
	 * @return the uploadContentType
	 */
	public String[] getUploadContentType() {
		return uploadContentType;
	}

	/**
	 * @param uploadContentType the uploadContentType to set
	 */
	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public void setServletContext(ServletContext ctx) {
		this.context = ctx;		
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}  
	
}

 

分享到:
评论

相关推荐

    Struts2多文件上传

    总结来说,Struts2多文件上传涉及到Struts2配置、Action编写、HTML表单设计以及结果处理等多个环节。通过以上步骤,你可以在Struts2应用中实现稳定可靠的多文件上传功能。不过,实际项目中还需要考虑错误处理、安全...

    struts2多文件上传显示进度

    Struts2是一个非常流行的Java Web框架,...总的来说,Struts2多文件上传并显示进度的实现需要结合前端和后端的技术,通过AJAX和XMLHttpRequest的`onprogress`事件来动态更新进度条,同时确保后端处理的高效和安全性。

    Struts2多个文件上传

    在Struts2中,文件上传功能是一个常用特性,尤其在处理用户提交的多个文件时。本文将详细讲解如何使用Struts2进行多个文件的上传,重点是使用List集合进行上传。 首先,要实现Struts2的文件上传,必须引入必要的...

    Struts2多文件上传下载实例

    在实际项目中,文件上传和下载功能是必不可少的,本实例将详细讲解如何在Struts2框架下实现单个文件及多个文件的上传与下载。 首先,我们需要在Struts2的配置文件(struts.xml)中添加相关的Action配置,以便处理文件...

    struts2多文件上传和下载

    在Struts2中,文件上传和下载是常见的功能,对于用户交互和数据交换至关重要。这篇内容将深入讲解如何在Struts2中实现多文件的上传和下载。 1. **文件上传** 文件上传在Web应用中常常用于让用户提交各种类型的文件...

    struts2实现多文件上传下载

    文件上传比较多,多文件上传少一点 文件下载很少的,看似简单,实则不然 网上的Struts2进行的文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子 提供多文件上传,上传成功后,提供...

    Ext+Struts2多文件上传

    通过以上步骤,我们可以利用ExtJS的用户界面和Struts2的后台处理能力,实现一个完整的多文件上传功能。这个功能不仅提高了用户体验,还简化了开发流程。在实际项目中,还可以进一步优化,例如添加进度条显示、预览...

    struts2实现多文件上传功能

    Struts2提供了完善的文件上传支持,让我们来详细探讨如何在Struts2中实现多文件上传。 首先,我们需要在Struts2的配置文件(struts.xml)中启用文件上传的支持。这通常涉及到添加`&lt;constant&gt;`标签来设置`struts....

    AjaxFileUpload Struts2 多文件上传

    本篇文章将深入讲解如何利用AjaxFileUpload与Struts2实现多文件上传,并结合jQuery进行前端交互。 首先,我们需要在项目中引入必要的库。Struts2提供了struts2-jquery-plugin,这是一个基于jQuery的插件,包含了...

Global site tag (gtag.js) - Google Analytics