`
xiaojiit
  • 浏览: 133899 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

Struts的文件上传

    博客分类:
  • Java
阅读更多

Struts的文件上传
本文用的是Struts1.1的org.apache.struts.upload.FormFile类。很方便,不用自己写。也不用写一个jsp调用jspsmartupload就可以搞定。

选择上传文件页面:selfile.jsp



--------------------------------------------------------------------------------
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<html:form action="/uploadsAction.do" enctype="multipart/form-data">
<html:file property="theFile"/>
<html:submit/>
</html:form>
</html:html>

--------------------------------------------------------------------------------
UpLoadAction.java
--------------------------------------------------------------------------------
import java.io.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;



public class UpLoadAction extends Action {
 <wbr> </wbr><wbr>public ActionForward execute(ActionMapping mapping,
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> ActionForm form,
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> HttpServletRequest request,
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> HttpServletResponse response)
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>throws Exception {
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>if (form instanceof uploadsForm) {//如果form是uploadsForm
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>String encoding = request.getCharacterEncoding();
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>{
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>response.setContentType("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>}
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>UpLoadForm theForm = (UpLoadForm ) form;
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>FormFile file = theForm.getTheFile();//取得上传的文件
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>try {
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>InputStream stream = file.getInputStream();//把文件读入
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>String filePath = request.getRealPath("/");//取当前系统路径
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>ByteArrayOutputStream baos = new ByteArrayOutputStream();
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>OutputStream bos = new FileOutputStream(filePath + "/" +
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>file.getFileName());//建立一个上传文件的输出流
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>//System.out.println(filePath+"/"+file.getFileName());
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>int bytesRead = 0;
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>byte[] buffer = new byte[8192];
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>bos.write(buffer, 0, bytesRead);//将文件写入服务器
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>}
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>bos.close();
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>stream.close();
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>}catch(Exception e){
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>System.err.print(e);
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>}
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>//request.setAttribute("dat",file.getFileName());
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>return mapping.findForward("display");
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>}
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>return null;
 </wbr><wbr> </wbr><wbr>}
}



--------------------------------------------------------------------------------

UpLoadForm.java

--------------------------------------------------------------------------------


import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;



public class UpLoadForm extends ActionForm {
 </wbr><wbr> </wbr><wbr>public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org.apache.struts.webapp.upload.MaxLengthExceeded";
 </wbr><wbr> </wbr><wbr>protected FormFile theFile;
 </wbr><wbr> </wbr><wbr>public FormFile getTheFile() {
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>return theFile;
 </wbr><wbr> </wbr><wbr>}
 </wbr><wbr> </wbr><wbr>public void setTheFile(FormFile theFile) {
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>this.theFile = theFile;
 </wbr><wbr> </wbr><wbr>}
 </wbr><wbr> </wbr><wbr>public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
 </wbr><wbr> </wbr><wbr>{
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>ActionErrors errors = null;
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>//has the maximum length been exceeded?
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>Boolean maxLengthExceeded = (Boolean)
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue()))
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>{
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>errors = new ActionErrors();
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED, new ActionError("maxLengthExceeded"));
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>}
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr>return errors;

 </wbr><wbr> </wbr><wbr>}
}
//这是相对应的form,还有其他属性可以设置,具体可以参考struts的上传例子。




--------------------------------------------------------------------------------

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>
 </wbr><wbr> </wbr><wbr><form-beans>
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr><form-bean name="uploadsForm" type="UpLoadForm" />
 </wbr><wbr> </wbr><wbr></form-beans>
 </wbr><wbr> </wbr><wbr><action-mappings>
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr><action name="uploadsForm" type="UpLoadAction" path="/uploadsAction">
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr><forward name="display" path="/display.jsp" />
 </wbr><wbr> </wbr><wbr> </wbr><wbr> </wbr><wbr></action>
 </wbr><wbr> </wbr><wbr></action-mappings>
</struts-config>
<!--display.jsp就是随便写一个成功页-->

分享到:
评论

相关推荐

    struts文件上传示例

    在这个"Struts文件上传示例"中,我们将深入探讨如何在Struts框架下实现文件上传功能,以及相关的关键知识点。 首先,理解文件上传的基本流程至关重要。在Web应用中,用户通过表单选择本地文件,然后提交到服务器。...

    JavaEE Struts文件上传

    JavaEE Struts 文件上传是Web开发中的一个常见需求,它允许用户从客户端向服务器传输文件。Struts作为JavaEE框架的一部分,提供了强大的功能来处理这种交互。以下是对这个主题的详细解析。 首先,理解文件上传的...

    struts 文件上传 Struts实现文件上传

    Struts 提供了强大的支持来处理文件上传操作。下面将详细介绍Struts 实现文件上传的相关知识点。 ### 1. Struts 框架概述 Struts 是Apache软件基金会下的一个开源项目,其主要目标是提供一个可扩展且易于维护的...

    struts文件上传大小限制问题

    本篇文章将详细探讨Struts文件上传大小限制的问题。 首先,我们需要理解文件上传的基本流程。在Struts中,当用户通过表单提交文件时,数据会被封装到`ActionForm`对象中,然后由Struts的控制器组件处理。默认情况下...

    struts文件上传.pdf

    ### Struts文件上传知识点 #### 一、Struts框架简介 Struts是一个开源的MVC(Model-View-Controller)框架,它简化了基于Java的Web应用程序开发过程。Struits框架提供了一种结构化的方式来组织代码,使得开发更加...

    jsp+struts文件上传

    在本文中,我们将深入探讨如何使用JSP(JavaServer Pages)和Struts框架实现文件上传功能。这是一个在Web开发中常见的需求,特别是在处理用户提交的数据时。Struts是一个基于MVC(Model-View-Controller)设计模式的...

    struts文件上传功能

    Apache Commons FileUpload库是Struts文件上传的核心,它提供了处理`multipart/form-data`请求的能力。你需要在项目中引入这个库,并在Action中使用`FileItemFactory`和`ServletFileUpload`来解析请求中的文件项。 ...

    struts文件上传大小限制

    一、Struts文件上传大小限制的配置 1. **struts-default.xml配置** 在Struts2框架中,我们可以通过修改`struts-default.xml`或自定义的配置文件来设置文件上传的最大大小。在`&lt;constant&gt;`标签中,可以设置以下两个...

    struts文件上传下载

    "Struts文件上传下载"是Struts框架中的一个重要功能,允许用户通过Web界面上传文件到服务器,同时也支持从服务器下载文件到用户的本地设备。在实际的业务场景中,例如在线文档共享、图片分享、文件管理等,文件上传...

    Struts文件上传

    Struts文件上传是Java Web开发中的一个重要功能,它允许用户通过Web界面上传文件到服务器。在Struts框架中实现文件上传,主要涉及到Struts的ActionForm、Servlet API以及相关的配置文件。下面将详细介绍Struts文件...

    Struts文件上传和Excel导出

    在本项目中,"Struts文件上传和Excel导出"涵盖了两个关键功能:文件上传和数据导出到Excel。这里我们将深入探讨这两个主题。 **文件上传** 1. **表单设计**:在HTML页面上,我们需要一个`&lt;input type="file"&gt;`标签...

    struts文件上传

    Struts文件上传是一个在Java Web开发中常见的功能,主要用于允许用户通过Web应用程序上传文件到服务器。Struts是Apache软件基金会的Jakarta项目中的一个开源框架,它基于Model-View-Controller(MVC)架构模式,旨在...

    struts 文件上传

    首先,我们来了解一下Struts文件上传的基本步骤: 1. **配置struts.xml**:在Struts配置文件中,你需要定义一个Action,这个Action将处理文件上传请求。通过`&lt;action&gt;`标签设置`executeResult`方法,以及与之关联的...

    struts 文件上传下载 程序

    ### Struts 文件上传下载程序详解 #### 一、概述 Struts 是一款开源的MVC框架,被广泛应用于Java Web应用程序开发中。本篇将基于一个具体的`struts`文件上传下载程序进行深入剖析,旨在帮助读者理解Struts在文件...

    struts 文件上传实例

    总结,Struts文件上传实例展示了如何在Java Web应用中使用Struts框架实现文件上传功能。这个过程涉及Struts配置、表单Bean、Action类以及服务器端的文件处理。理解和掌握这个实例,可以帮助开发者更好地应对实际项目...

    struts文件上传例子

    这个"struts文件上传例子"很可能是展示如何在Struts应用中处理文件上传的一个示例项目。 首先,你需要在JSP页面上创建一个`&lt;input type="file"&gt;`元素,让用户选择要上传的文件。然后,将该表单提交到一个对应的...

    struts文件上传小项目

    在这个"struts文件上传小项目"中,我们将探讨如何利用Struts框架实现文件上传功能,这是一个常见的Web应用程序需求。 首先,我们需要了解Struts的配置文件`struts-config.xml`。在这个项目中,它会定义一个或多个...

Global site tag (gtag.js) - Google Analytics