Struts2中实现多文件上传于下载

做web开发,经常要实现文件的上传功能,Struts2使用的是jakarta中的commons-fileupload-1.2.jar和commons-io-1.3.1.jar来实现文件上传的,所以必须将这两个包放入lib目录

 

实现文件上传必须将表单的提交方式设为post(默认为get),并将enctype设置为multipart/form-data.不然无法完成文件上传

那么现在咋们就开始实现文件上传吧

1、完成一个上传表单upload.jsp和下载页面down.jsp

<form action="upload_upload" method="post" enctype="multipart/form-data">
  	<input type="file" name="files"><br>
  	<input type="file" name="files"><br>
  	<input type="submit" value="上传">
</form>
<s:iterator value="filesFileName" id="file">
  			<a href='down_down?filename=<s:property value="#file"/>'><s:property value="#file"/></a><br>
</s:iterator>



2、上传和下载Action

public class UploadAction extends ActionSupport
{
	private File[] files;
	private String[] filesFileName;
	public File[] getFiles()
	{
		return files;
	}
	public void setFiles(File[] files)
	{
		this.files = files;
	}



	public String[] getFilesFileName()
	{
		return filesFileName;
	}



	public void setFilesFileName(String[] filesFileName)
	{
		this.filesFileName = filesFileName;
	}
	public String[] getFilesContentType()
	{
		return filesContentType;
	}



	public void setFilesContentType(String[] filesContentType)
	{
		this.filesContentType = filesContentType;
	}

	private String[] filesContentType;
	
	
	
	public String upload()
	{
		System.out.println("upload方法执行了...............");
		String path=ServletActionContext.getServletContext().getRealPath("upload");
		System.out.println("paht==>"+path);
		FileInputStream is=null;
		FileOutputStream os=null;
		for(int i=0;i<files.length;i++)
		{
			try
			{
				is=new FileInputStream(files[i]);
				os=new FileOutputStream(new File(path,filesFileName[i]));
				
				int len=-1;
				byte[]buffer=new byte[1024];
				while(-1!=(len=is.read(buffer)))
				{
						os.write(buffer, 0, len);
				}
				
				is.close();
				os.close();
			} catch (FileNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		ServletActionContext.getRequest().getSession().setAttribute("filename", filesFileName);
		return "success";
	}
}



 

public class DownAction extends ActionSupport
{
	
		private String filename;
		public String getFilename()
		{
			return filename;
		}
		public void setFilename(String filename)
		{
			try
			{
				this.filename = new String(filename.getBytes("iso-8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		public InputStream getDownFile()
		{
			return ServletActionContext.getServletContext().getResourceAsStream("/upload/"+filename);
		}
		public String down()
		{
			return "success";
		}
}


 

3、配置文件struts.xml

<action name="upload_*" class="com.action.UploadAction" method="{1}">
 		<result name="success">/result.jsp</result>
 		<result name="input">/upload.jsp</result>
 		</action>
 		
 		
 		<action name="down_*" class="com.action.DownAction" method="{1}">
 		<result type="stream">
 		<param name="inputName">downFile</param>
 		<param name="contentDisposition">attachment;filename="${filename}"</param>
 		</result>
 		</action>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值