`
zengjinliang
  • 浏览: 305449 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

用Struts上传多个文件的方法

阅读更多
一。建立ActionForm
  
  package com.cnehu.struts.form;
  
  import javax.servlet.http.HttpServletRequest;
  
  import org.apache.struts.action.ActionError;
  
  import org.apache.struts.action.ActionErrors;
  
  import org.apache.struts.action.ActionForm;
  
  import org.apache.struts.action.ActionMapping;
  
  import org.apache.struts.upload.FormFile;
  
  import org.apache.struts.upload.MultipartRequestHandler;
  
  /**
  
  * <p>
  
  * Title:UpLoadForm
  
  * </p>
  
  * <p>
  
  * Copyright: Copyright (c) 2005 techyang http://blog.csdn.net/techyang
  
  * </p>
  
  * @author techyang
  
  * @version 1.0
  
  */
  
  public class UpLoadForm extends ActionForm
  
  {
  
  public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org.apache.struts.webapp.upload.MaxLengthExceeded";
  
  protected FormFile theFile;
  
  protected FormFile theFile2;
  
  public FormFile getTheFile()
  
  {
  
  return theFile;
  
  }
  
  public void setTheFile(FormFile theFile)
  
  {
  
  this.theFile = theFile;
  
  }
  
  public ActionErrors validate(ActionMapping mapping,
  
  HttpServletRequest request)
  
  {
  
  ActionErrors errors = null;
  
  //has the maximum length been exceeded?
  
  Boolean maxLengthExceeded = (Boolean) request
  
  .getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
  
  if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue()))
  
  {
  
  errors = new ActionErrors();
  
  errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED, new ActionError(
  
  "maxLengthExceeded"));
  
  }
  
  return errors;
  
  }
  
  /**
  
  * @return Returns the theFile2.
  
  */
  
  public FormFile getTheFile2()
  
  {
  
  return theFile2;
  
  }
  
  /**
  
  * @param theFile2 The theFile2 to set.
  
  */
  
  public void setTheFile2(FormFile theFile2)
  
  {
  
  this.theFile2 = theFile2;
  
  }
  
  }
  
  二。建立ActionServlet
  
  package com.cnehu.struts.action;
  
  import java.io.*;
  
  import javax.servlet.http.*;
  
  import org.apache.struts.action.*;
  
  import org.apache.struts.upload.FormFile;
  
  import com.cnehu.struts.form.UpLoadForm;
  
  /**
  
  * <p>
  
  * Title:UpLoadAction
  
  * </p>
  
  * <p>
  
  * Copyright: Copyright (c) 2005 techyang http://blog.csdn.net/techyang
  
  * </p>
  
  * @author techyang
  
  * @version 1.0
  
  */
  
  public class UpLoadAction extends Action
  
  {
  
  public ActionForward execute(ActionMapping mapping, ActionForm form,
  
  HttpServletRequest request, HttpServletResponse response)
  
  throws Exception
  
  {
  
  String encoding = request.getCharacterEncoding();
  
  if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
  
  {
  
  response.setContentType("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312
  
  }
  
  UpLoadForm theForm = (UpLoadForm) form;
  
  FormFile file = theForm.getTheFile();//取得上传的文件
  
  FormFile file2=theForm.getTheFile2();
  
  try
  
  {
  
  /*
  
  * 取当前系统路径D:\Tomcat5\webapps\coka\ 其中coka 为当前context
  
  */
  
  String filePath = this.getServlet().getServletContext()
  
  .getRealPath("/");
  
  InputStream stream = file.getInputStream();//把文件读入
  
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
  /*
  
  * 建立一个上传文件的输出流 如果是linux系统请把UploadFiles后的"\\"换成"/"
  
  */
  
  OutputStream bos = new FileOutputStream(filePath +
  
  "UploadFiles\\"+file.getFileName());
  
  //D:\Tomcat5\webapps\coka\UploadFiles\DSC01508.JPG
  
  /* System.out.println(filePath +
  
  "UploadFiles\\"+file.getFileName());
  
  System.out.println(filePath);*/
  
  request.setAttribute("fileName",filePath + "/"
  
  + file.getFileName());
  
  int bytesRead = 0;
  
  byte[] buffer = new byte[8192];
  
  while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
  
  {
  
  bos.write(buffer, 0, bytesRead);//将文件写入服务器
  
  }
  
  bos.close();
  
  stream.close();
  
  InputStream stream2 = file2.getInputStream();//把文件读入
  
  ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
  
  OutputStream bos2 = new FileOutputStream(filePath +
  
  "UploadFiles\\"+file2.getFileName());//建立一个上传文件的输出流
  
  int bytesRead2 = 0;
  
  byte[] buffer2 = new byte[8192];
  
  int i=0;
  
  while ((bytesRead2 = stream2.read(buffer2, 0, 8192)) != -1)
  
  {
  
  bos2.write(buffer2, 0, bytesRead2);//将文件写入服务器
  
  }
  
  bos2.close();
  
  stream2.close();
  
  } catch (Exception e)
  
  {
  
  System.err.print(e);
  
  }
  
  return mapping.findForward("display");
  
  }
  
  }
  
  三。建立上传用的JSP文件 upload.jsp
  
  <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
  
  <html:html>
  
  <head>
  
  <title>用Struts上传文件</title>
  
  </head>
  
  <body>
  
  <html:form action="/uploadsAction" enctype="multipart/form-data">
  
  <html:file property="theFile"/>
  
  <html:file property="theFile2"/>
  
  <html:submit/>
  
  </html:form>
  
  </body>
  
  </html:html>
  
  四。配置struts-config.xml文件
  
  <?xml version="1.0" encoding="UTF-8"?>
  
  <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
  
  <struts-config>
  
  <data-sources />
  
  <form-beans >
  
  <form-bean name="uploadsForm" type="com.cnehu.struts.form.UpLoadForm" />
  
  </form-beans>
  
  <global-exceptions />
  
  <global-forwards >
  
  </global-forwards>
  
  <action-mappings >
  
  <action name="uploadsForm" type="com.cnehu.struts.action.UpLoadAction" path="/uploadsAction">
  
  <forward name="display" path="/display.jsp" />
  
  </action>
  
  </action-mappings>
  
  </struts-config>
分享到:
评论

相关推荐

    Struts2多个文件上传

    在这个例子中,`name="files"`的`input`元素允许用户选择多个文件,`multiple`属性表示可以同时上传多个文件。 在Struts2的动作类(Action)中,你需要创建一个List类型的属性来接收这些文件。例如: ```java ...

    用struts上传多个文件的方法

    这里定义了两个文件上传字段`theFile`和`theFile2`,并通过`validate`方法检查文件大小是否超过了最大限制。 ##### 3.3 实现Action 接下来需要创建一个Action类来处理文件上传的具体逻辑。以`UpLoadAction`为例: ...

    struts上传多文件

    在实际应用中,有时我们需要同时上传多个文件,这在Struts中是可以实现的。本篇文章将深入讲解如何在Struts框架中实现多文件上传。 首先,了解Struts2文件上传的基本原理。Struts2使用了Apache的Commons FileUpload...

    一个Struts1多文件上传实例(附Form中传List示例)

    通过分析和学习这些代码,你可以更好地理解和掌握Struts1中多文件上传和表单中传递List的实践方法。 总之,这个实例提供了关于Struts1中文件上传和复杂表单数据处理的实战经验,对于学习Java Web开发和Struts1框架...

    struts1上传多个文件同时

    要实现多个文件的同时上传,我们需要利用一些额外的工具或库来扩展Struts1的功能。 在描述中提到的"jquery multi"可能是指jQuery的某个插件,用于实现前端的多文件选择和预览,例如`jQuery Multi File Uploader`或`...

    struts2实现的多个文件上传

    下面将详细阐述如何使用Struts2来实现多个文件的上传。 首先,理解文件上传的基本原理。在HTTP协议中,文件上传通常通过multipart/form-data编码类型来实现。这种编码方式将表单数据分割成多个部分,每部分包含一个...

    用Struts上传多个文件的方法(含源码)

    在标题和描述中提到的"用Struts上传多个文件的方法",主要是指在Struts框架下处理用户通过表单提交的多个文件。下面我们将详细介绍如何实现这个功能。 1. **创建ActionForm** ActionForm是Struts框架中处理请求...

    struts1 多个文件上传

    这篇内容将深入探讨Struts1中的单个文件上传和多个文件上传的实现机制。 首先,我们来看单个文件上传。在Struts1中,要实现文件上传,你需要在表单中包含一个`&lt;input type="file"&gt;`标签,并在ActionForm中创建对应...

    struts2实现多文件上传下载

    1.能够对多个文件进行上传(可以选择上传文件个数,也即上传文件个数不定) 2.能够对上传路径进行配置文件指定(upload.properties),使用了一些类似单例模式的静态代码块 3.Struts2进行下载处理,能对上传的所有...

    Struts2实现单个文件多个文件上传与下载-多个拦截器

    本项目主要展示了如何在Struts2框架下实现单个文件和多个文件的上传及下载,并且运用了多个拦截器来增强功能和安全性。 首先,让我们详细了解一下文件上传的过程。在Struts2中,文件上传主要依赖于`struts2-...

    使用struts1.x上传多个文件的一中方法

    下面我们将详细介绍如何使用Struts1.x来实现多个文件的上传。 首先,我们需要在HTML表单中添加`&lt;input type="file"&gt;`元素,允许用户选择多个文件。在HTML5中,可以使用`multiple`属性来支持选取多个文件: ```html...

    struts多文件上传

    在Struts1.2版本中,实现多文件上传是一项常见的需求,它允许用户在一次提交中上传多个文件,这对于数据交互、资源分享等场景非常实用。在本教程中,我们将深入探讨如何在Struts1.2中实现这一功能。 首先,理解多...

    struts2实现文件上传下载

    对于大量文件上传,可以考虑使用多线程处理,提高上传效率。还可以添加进度条显示、断点续传等功能,提升用户体验。 以上就是使用Struts2框架实现文件上传下载的基本步骤和关键知识点。在实际开发中,可以根据项目...

    struts2实现多文件上传功能

    通过组合使用Struts2的配置、Action类和HTML表单,我们可以轻松地处理多个文件的上传操作。这个过程中的关键知识点包括:Struts2拦截器、文件上传配置、Action类设计、HTML表单的构建以及服务器端的文件处理逻辑。

    实现Struts上传多个文件

    ### 实现Struts上传多个文件 在开发基于Struts框架的应用程序时,有时我们需要实现文件上传功能,尤其是在需要用户上传多张图片的情况下。本篇内容将详细介绍如何在Struts项目中实现多文件上传,并提供相应的代码...

    swfuplaod+struts2实现多文件上传

    3. **并行上传**:允许同时上传多个文件,提高上传速度。 4. **分片上传**:对于大文件,可以采用分片上传,降低网络中断的影响。 通过上述步骤和注意事项,你可以利用SWFUpload和Struts2构建一个功能强大且安全的...

    struts2多文件的上传

    在本项目中,"struts2多文件的上传"实现了用户一次性上传多个文件的能力。 要理解这个功能,首先我们需要了解Struts2中的Action类和Interceptor(拦截器)。Action类是处理用户请求的核心,而Interceptor则用于处理...

    struts 单文件上传和多文件上传带进度条

    在Action类中,我们需要定义一个或多个`java.io.File`或`org.apache.struts2.dispatcher.multipart.FileItem`类型的属性,这些属性会被用来存储上传的文件。 对于单文件上传,我们需要在表单中添加一个`...

    struts2 多个文件上传 插件goouploader

    Goouploader插件允许用户在Web表单中选择多个文件进行同时上传,极大地提高了用户体验。 在Struts2中,传统的文件上传是通过`&lt;s:file&gt;`标签实现的,但只支持单个文件上传。Goouploader插件则提供了更强大的多文件...

    struts1.x多文件上传

    总的来说,Struts1.x的多文件上传功能通过合理的表单设计、框架配置和后端处理,可以方便地实现用户在Web应用中上传多个文件。然而,随着技术的发展,现在的Web应用更多地转向了Spring MVC等更现代的框架,它们提供...

Global site tag (gtag.js) - Google Analytics