多個檔案上傳

來源:互聯網
上載者:User

標籤:

//準備檔案

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>

  

 

 

 

 

 

多個檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.