Struts2中的多文件上传:实例

8 篇文章 0 订阅

Struts2中的多文件上传:实例

第一步:先创建一个上传文件的jsp页面(upload.jsp)

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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; charset=utf-8">
<title>Insert title here</title>
</head>
<body>

<form action="upload" method="post" enctype="multipart/form-data">
	文件上传:<br/>
	<input type="file" name="myfile"/><br/>
	<input type="file" name="myfile"/><br/>
	<input type="file" name="myfile"/><br/>
	<input type="submit" value="提交"/>
</form>

</body>
</html>

第二步:在src目录下新建一个com.test.action包,然后新建一个 UploadAction 类

package com.gyx.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

public class UploadAction implements Action {

private File[] myfile;
private String[] myfileFileName;
private String[] myfileContentType;
private String savepath = "/upload";

@Override
public String execute() throws Exception {
	String realpath = ServletActionContext.getServletContext().getRealPath(savepath);

	File file = new File(realpath);
	if (!file.exists()) {
		file.mkdirs();
	}
	HttpServletRequest request = ServletActionContext.getRequest();
	List imgpath = new ArrayList();
	for (int i=0;i<myfile.length;i++) {
		FileUtils.copyFile(myfile[i], new File(file, myfileFileName[i]));
		imgpath.add("/Struts"+savepath+"/"+myfileFileName[i]);
	}
	request.setAttribute("imgpath",imgpath);
	return SUCCESS;
}

public File[] getMyfile() {
	return myfile;
}

public void setMyfile(File[] myfile) {
	this.myfile = myfile;
}

public String[] getMyfileFileName() {
	return myfileFileName;
}

public void setMyfileFileName(String[] myfileFileName) {
	this.myfileFileName = myfileFileName;
}

public String[] getMyfileContentType() {
	return myfileContentType;
}

public void setMyfileContentType(String[] myfileContentType) {
	this.myfileContentType = myfileContentType;
}

}

第三步:在src目录下新建一个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.i18n.encoding" value="UTF-8"></constant>
     <constant name="devMode" value="true"></constant>
     
     <package name="gyx" extends="struts-default">
	<!-- 上传文件action -->
	<action name="upload" class="com.gyx.action.UploadAction">
		<interceptor-ref name="defaultStack">
		    <!--允许文件上传的类型(这里为了方便显示,只允许图片上传)-->
			<param name="fileUpload.allowedTypes">image/jpg,image/jpeg,image/png</param>
			<!--允许文件上传的扩展名-->
			<param name="fileUpload.allowedExtensions">.jpg,.png,.gif</param>
		</interceptor-ref>
		<result>/index.jsp</result>
		<result name="input" type="redirect">upload.jsp</result>
	</action>
</package>
</struts>

第四步:文件上传成功后,我们需要一个页面展示;我们新建一个 index.jsp 页面用来显示上传的图片。

<%@ page isELIgnored="false" language="java" contentType="text/html; charset=utf-8"
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; charset=utf-8">
<title>首页</title>
</head>
<body>
首页
<s:iterator  value="#request.imgpath" var="path" status="i">
	<img alt="图片无法显示" src="<s:property value='path'/>">
</s:iterator>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值