`

多文件上传

阅读更多

多文件上传

 

第一步:编写上传的页面

  <%@ 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>

分享到:
评论

相关推荐

    Extjs多文件上传

    在“Extjs多文件上传”这个主题中,我们将深入探讨如何使用ExtJS实现一个支持自由添加和减少上传文件个数的多文件上传功能。 首先,ExtJS的文件上传功能通常依赖于其组件系统,特别是`Ext.form.FileField`(也称为`...

    java多文件上传实现

    在Java开发中,多文件上传是一项常见的功能,尤其在Web应用中,用户可能需要一次性上传多个文件,如图片、文档等。本知识点将详细介绍如何在Java中实现这一功能,以及结合Flash实现上传界面并显示上传进度条。 1. *...

    多文件上传和展示

    多文件上传功能则意味着用户可以一次性选择并上传多个文件,大大提高了数据传输的效率。 【详细实现】 1. **引入资源**:首先,你需要在HTML文件中引入Bootstrap和Bootstrap-fileinput的相关CSS和JS文件。这些文件...

    EXTJS 多文件上传

    在EXTJS中,`MultiFileUploadField` 是一个组件,它允许用户在单个操作中选择并上传多个文件,极大地提升了用户交互体验。这个功能对于处理大量文件上传的场景非常有用,比如在内容管理系统、图像库或者文件分享平台...

    多文件上传 源码

    这个源码可能提供了一种实现方式,允许用户同时上传多个文件,并且带有进度条显示,用户可以随时停止或删除正在上传的文件。这样的功能提高了用户体验,因为它允许用户批量处理文件,而无需逐一操作。 在实现多文件...

    基于SpringBoot的文件上传系统,前后端分离,单文件上传,多文件上传,大文件上传,断点续传,文件秒传,图片上传

    基于SpringBoot的文件上传系统,前后端分离,单文件上传,多文件上传,大文件上传,断点续传,文件秒传,图片上传 项目经过严格测试,确保可以运行! 采用前后端分离的方式进行开发,实现了几种常用的文件上传功能...

Global site tag (gtag.js) - Google Analytics