多文件上传

多文件上传

 

第一步:编写上传的页面

  <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>My JSP 'upload.jsp' starting page</title>

 

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">   

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

<script type="text/javascript">

function addMore(){

var td=document.getElementById("more");

 

var br=document.createElement("br");

var input=document.createElement("input");

var button=document.createElement("input");

 

input.type="file";

input.name="file";

 

button.type="button";

button.value="-";

 

button.onclick=function(){

td.removeChild(br);

td.removeChild(input);

td.removeChild(button);

}

td.appendChild(br);

td.appendChild(input);

td.appendChild(button);

}

</script>

 

  </head>

 

  <body>

   <s:form action="/system/fileUpload.do" method="post" enctype="multipart/form-data">

  <%--此处的action应根据是否有命名空间来添加。本例子在system的命名空间下--%>

   <table align="center" width="60%" border="1">

   <tr>

    <td>

  文件名称: <input type="text" name="title"/>

   </td>

   </tr>

   <tr>

   <td>

   选择上传的文件:

   </td>

   <td id="more">

   <%--<s:file name="file" label="选择上传的文件"></s:file>--%>

   <input type="file" name="file">

   <input type="button" value="+" onclick="addMore()"/>

   </td>

   </tr>

   <tr>

   <td>

   </td>

   <td>

   <s:submit value="上传" align="center"></s:submit>

   </td>

   </tr>   

   </table>

   </s:form>

  </body>

</html>

 

第二步:编写文件上传的action

package com.pominfo.demo.system.action;

 

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ActionContext;

import java.sql.*;

import java.text.SimpleDateFormat;

import javax.sql.*;

import javax.swing.JOptionPane;

import javax.naming.*;

import java.util.*;

import java.util.regex.Pattern;

import org.apache.struts2.ServletActionContext;///获取reqeust response session对象的包

import java.util.Date;

import java.io.*;

/*

 * @author qihuasun

 *  FileUploadAction 实现多文件上传

 */

public class UploadFileAction extends ActionSupport

{

       private List<File> file; //文件

    private String title;//文件标题

       private List<String> fileFileName;//文件名称

 

       private List<String> fileContentType; //文件类型

 

       public List<File> getFile() {

       return file;

       }

   

       public String getTitle() {

              return title;

       }

 

       public void setTitle(String title) {

              this.title = title;

       }

 

       public void setFile(List<File> file) {

       this.file = file;

       }

 

       public List<String> getFileContentType() {

       return fileContentType;

       }

 

       public void setFileContentType(List<String> fileContentType) {

       this.fileContentType = fileContentType;

       }

 

       public List<String> getFileFileName() {

       return fileFileName;

       }

 

       public void setFileFileName(List<String> fileFileName) {

       this.fileFileName = fileFileName;

       }

 

       /**

       * 动态上传文件

       * @return

       * @throws Exception

       */

       public String Upload() throws Exception {

 

       InputStream is=null;

       OutputStream ops=null;

         //1. 遍历文件    

           for(int i=0;i<file.size();i++){

           try{

       is=new FileInputStream(file.get(i));

    //2.获取路径

       String root=ServletActionContext.getRequest().getRealPath("/upload");

     //将文件名称放入的根目录下的upload文件下

   File destFile=new File(root+"/",this.getFileFileName().get(i));

       ops=new FileOutputStream(destFile);

       byte [] b=new byte[1024];

       int length=0;

       while((length=is.read(b))>0){

      //将数据写入流

       ops.write(b,0,length);

        }

       }catch(Exception ex){

       ex.printStackTrace();

       }finally{

             

              ServletActionContext.getRequest().setAttribute("page",file.size());

       is.close();

       ops.close();

       }

           }

       return "success";

       }

 

}

 

第三步:配置strutsXml文件。

      <package name="system" extends="struts-default" namespace="/system" >

      <action name="fileUpload"  class="com.pominfo.demo.system.action.UploadFileAction" method="Upload">

              <!-- 定义文件上传的过滤器 -->

              <interceptor-ref name="fileUpload"><!—此处为fileUpload。与actionname值相同只是巧合 -->

 

               <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,application/msword,audio/x-mpeg,text/html,text/plain</param>

                <param name="maximumSize">12000000</param><!-- 设定文件的大小-->

              </interceptor-ref>

              <!-- 定义默认的拦截器栈 -->

              <interceptor-ref name="defaultStack"></interceptor-ref>

              <result name="success">/succ.jsp</result>

              <result name="input">/upload.jsp</result>

              <result name="error">/upload.jsp</result>

       </action>

 

 

</package>

你可能感兴趣的:(sql,jsp,struts,css,swing)