標籤:
//準備檔案
1.上傳多個檔案頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@taglib uri="/struts-tags" prefix="s" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>登入頁面</title> </head> <body> <s:form action="upload.action" enctype="multipart/form-data" method="post"> <s:file name="upload" label="選擇檔案" /><br /><s:file name="upload" label="選擇檔案" /><br /><s:file name="upload" label="選擇檔案" /><br /><s:submit name="submit" value="上傳檔案"></s:submit> </s:form> </body></html>
2.在struts.xml中配置相應的action
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- 動態方法引動過程 --> <constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default"> <action name="upload" class="cn.happy.action.UploadAction" method="execute"> <param name="savePath">/image</param> <result name="success">/upload/success.jsp</result> </action> </package></struts>
3.在根據action節點找對應的類
package cn.happy.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 {/*** * 多檔案上傳 * @return */// 上傳檔案的屬性private File[] upload;// 上傳檔案的類型private String[] uploadContentType;// 上傳檔案的名稱private String[] uploadFileName;// 上傳檔案的地址private String savePath;@Overridepublic String execute() throws Exception{byte[] buffer=new byte[1024];for(int i=0;i<upload.length;i++){//建立上傳檔案的出入流FileInputStream fis=new FileInputStream(getUpload()[i]);//建立上傳檔案的輸出資料流, getImageFileName()[i]FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName()[i]); int length=fis.read(buffer); while(length>0){ fos.write(buffer,0,length); length=fis.read(buffer); } fis.close();fos.flush();fos.close();}return SUCCESS;}public File[] getUpload() {return upload;}public void setUpload(File[] upload) {this.upload = upload;}public String[] getUploadContentType() {return uploadContentType;}public void setUploadContentType(String[] uploadContentType) {this.uploadContentType = uploadContentType;}public String[] getUploadFileName() {return uploadFileName;}public void setUploadFileName(String[] uploadFileName) {this.uploadFileName = uploadFileName;}public String getSavePath() {return ServletActionContext.getServletContext().getRealPath(savePath);}public void setSavePath(String savePath) {this.savePath = savePath;}}
4.如果成功就去找成功頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@taglib uri="/struts-tags" prefix="s" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>成功頁面</title> </head> <body> 您所上傳的檔案是:<s:property value="uploadFileName"/><br/>檔案類型:<s:property value="uploadContentType"/> </body></html>
多個檔案上傳