Struts2实现多文件上传

 

strus2 配置文件

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

	//封装文件标题请求参数的属性
	private String title;
	
	//封装上传文件域的属性
	private File[] upload;
	
	//封装上传文件类型的属性
	private String[] uploadContentType;
	
	//封装上传文件名的属性
	private String[] uploadFileName;
	
	//直接在struts.xml文件中配置的属性
	private String savePath;
	
	private String allowType;
	
	
	//返回上传文件的保存位置
	public String getSavePath() throws Exception{
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}

	
	//接受struts.xml文件配置值的方法
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String upload() throws Exception {

		for(int i=0;i<upload.length;i++){
			
			//以服务器的文件保存地址和原文件名建立上传文件输出流
			FileInputStream fis = new FileInputStream(upload[i]);
			FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
					+ getTitle()+uploadFileName[i]);
			
			System.out.println(getUploadFileName()+"=================");
			
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			fos.close();
			fis.close();
		}

		return SUCCESS;

	}

	
	
	
	
	

	//文件标题的setter和getter方法
	public String getTitle() {
		return title;
	}




	public void setTitle(String title) {
		this.title = title;
	}

	//上传文件对应文件内容的setter和getter方法
	public File[] getUpload() {
		return upload;
	}

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

	//上传文件的文件类型的setter和getter方法
	public String[] getUploadContentType() {
		return uploadContentType;
	}

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

	//上传文件的文件名的setter和getter方法
	public String[] getUploadFileName() {
		return uploadFileName;
	}

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


	public String getAllowType() {
		return allowType;
	}


	public void setAllowType(String allowType) {
		this.allowType = allowType;
	}



	
}


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="mess" />
	<!-- 设置该应用使用的解码集 -->
	<constant name="struts.i18n.encoding" value="UTF-8" />

	<package name="upload1" extends="struts-default">

		<!-- 配置处理文件上传的Action -->
		<action name="upload" class="action.UploadAction" method="upload">

			<!-- 动态设置Action的属性值 -->
			<param name="savePath">/uploadFiles</param>

			<interceptor-ref name="fileUpload">
				<!-- 配置允许上传的文件类型 -->
				<param name="allowedTypes">image/png,image/gif,image/jpeg,image/jpg</param>
				<!-- 配置允许上传的文件大小 -->
				<param name="maximumSize">20000</param>
			</interceptor-ref>

			<!--配置内部的拦截器, -->
			<interceptor-ref name="defaultStack" />


			<!-- 配置Struts 2默认的视图页面 -->
			<result>/succ.jsp</result>

			<!-- 拦截器调用 -->
			<result name="input">/index.jsp</result>
		</action>

	</package>

</struts>



jsp 页面

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>


<html>
  <head>

    
    <title>简单的文件上传</title>

  </head>
  
  <body>
     <s:fielderror/>
     
     <s:form action="/upload" enctype="multipart/form-data">
      <s:textfield name="title" label="文件标题"/>
      <s:file name="upload" label="选择文件"/>
      <s:file name="upload" label="选择文件"/>
      <s:file name="upload" label="选择文件"/>
      <s:submit value="上传"/>
     
     </s:form>
  </body>
</html>


succ.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>


<html>
  <head>

    
    <title>上传成功</title>

  </head>
  
  <body>
      
      上传成功<br/>
        文件标题:<s:property value=" +title"/><br/>
        文件为: <img src="<s:property value=" 'uploadFiles/' +uploadFileName"/>"/><br/>  
 
  </body>
</html>


 

国际化:

mess.properties

#\u6539\u53D8\u6587\u4EF6\u7C7B\u578B\u4E0D\u5141\u8BB8\u7684\u63D0\u793A\u4FE1\u606F
struts.messages.error.content.type.not.allowed=\u60A8\u4E0A\u4F20\u7684\u6587\u4EF6\u7C7B\u578B\u53EA\u80FD\u662F\u56FE\u7247\u6587\u4EF6\uFF01\u8BF7\u91CD\u65B0\u9009\u62E9\uFF01
#\u6539\u53D8\u4E0A\u4F20\u6587\u4EF6\u592A\u5927\u7684\u63D0\u793A\u4FE1\u606F
struts.messages.error.file.too.large=\u60A8\u8981\u4E0A\u4F20\u7684\u6587\u4EF6\u592A\u5927\uFF0C\u8BF7\u91CD\u65B0\u9009\u62E9\uFF01

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值