Struts2中的多文件上傳:實例

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章