知识大全 分享:用Struts上传多个文件的方法

Posted 文件

篇首语:路漫漫其修远兮,吾将上下而求索。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 分享:用Struts上传多个文件的方法相关的知识,希望对你有一定的参考价值。

分享:用Struts上传多个文件的方法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  最近在做Struts项目时遇到了上传多个文件的问题 在网上查了不少资料 也没有找到用Struts上传多个文件的例子 我经过几天的研究 实现了用Struts上传多个文件的功能 现在贴出来让大家共享!     一 建立ActionForm    package ehu struts form;    import javax servlet HttpServletRequest;    import apache struts action ActionError;    import apache struts action ActionErrors;    import apache struts action ActionForm;    import apache struts action ActionMapping;    import apache struts upload FormFile;    import apache struts upload MultipartRequestHandler;    /**    * <p>    * Title:UpLoadForm    * </p>    * <p>    * Copyright: Copyright (c) techyang     * </p>    * @author techyang    * @version     */    public class UpLoadForm extends ActionForm        public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = apache struts webapp upload MaxLengthExceeded ;    protected FormFile theFile;    protected FormFile theFile ;    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 theFile     */    public FormFile getTheFile ()        return theFile ;        /**    * @param theFile The theFile to set     */    public void setTheFile (FormFile theFile )        this theFile = theFile ;             二 建立ActionServlet    package ehu struts action;    import java io *;    import javax servlet *;    import apache struts action *;    import apache struts upload FormFile;    import ehu struts form UpLoadForm;    /**    * <p>    * Title:UpLoadAction    * </p>    * <p>    * Copyright: Copyright (c) techyang     * </p>    * @author techyang    * @version     */    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 )))        response setContentType( text/; charset=gb );//如果没有指定编码 编码格式为gb         UpLoadForm theForm = (UpLoadForm) form;    FormFile file = theForm getTheFile();//取得上传的文件    FormFile file =theForm getTheFile ();    try        /*    * 取当前系统路径D:\\Tomcat \\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:\\Tomcat \\webapps\\coka\\UploadFiles\\DSC JPG    /* System out println(filePath +     UploadFiles\\\\ +file getFileName());    System out println(filePath);*/    request setAttribute( fileName filePath + /     + file getFileName());    int bytesRead = ;    byte[] buffer = new byte[ ];    while ((bytesRead = stream read(buffer )) != )        bos write(buffer bytesRead);//将文件写入服务器        bos close();    stream close();    InputStream stream = file getInputStream();//把文件读入    ByteArrayOutputStream baos = new ByteArrayOutputStream();    OutputStream bos = new FileOutputStream(filePath +     UploadFiles\\\\ +file getFileName());//建立一个上传文件的输出流    int bytesRead = ;    byte[] buffer = new byte[ ];    int i= ;    while ((bytesRead = stream read(buffer )) != )        bos write(buffer bytesRead );//将文件写入服务器        bos close();    stream close();     catch (Exception e)        System err print(e);        return mapping findForward( display );             三 建立上传用的JSP文件 upload jsp    <%@ taglib uri= prefix= %>    <:>    <head>    <title>用Struts上传文件</title>    </head>    <body>    <:form action= /uploadsAction enctype= multipart/form data >    <:file property= theFile />    <:file property= theFile />    <:submit/>    </:form>    </body>    </:>     四 配置struts config xml文件    <?xml version= encoding= UTF ?>    <!DOCTYPE struts config PUBLIC //Apache Sofare Foundation//DTD Struts Configuration //EN config_ _ dtd >    <struts config>    <data sources />    <form beans >    <form bean name= uploadsForm type= ehu struts form UpLoadForm />    </form beans>    <global exceptions />    <global forwards >    </global forwards>    <action mappings >    <action name= uploadsForm type= ehu struts action UpLoadAction path= /uploadsAction >    <forward name= display path= /display jsp />    </action>    </action mappings>    </struts config> cha138/Article/program/Java/ky/201311/28148

相关参考

知识大全 C#用jquery多个文件上传

C#用jquery多个文件上传  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  <%@Pag

知识大全 在Struts 2中实现文件上传

在Struts2中实现文件上传  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Struts是通过

知识大全 Struts轻松搞定多文件上传

Struts轻松搞定多文件上传  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  在前面的《Apac

知识大全 基于Struts1.2的动态多文件上传

基于Struts1.2的动态多文件上传  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  首先是we

知识大全 struts2文件上传的采用的三种方式解析

   文件上传几乎是每个项目实现的一个必须的模块  上传就是将信息从个人计算机(本地计算机)传递到中央计算机(远程计算机)系统上让网络上的人都能看到将制作好的网页文字图片等发布到互联网上去以

知识大全 上传单个文件

Struts1.x系列教程(8):上传单个文件  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  在

知识大全 用cos进行文件上传

  在jsp中实现文件上传可用的类库很多比如有著名的jspsmart公司SmartUploadstruts里面也有我这里说说cos它是OReilly公司的OReilly的图书是很错的这个上传的组件也做

知识大全 用JSP实现上传文件的两种方法

用JSP实现上传文件的两种方法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  以前用cos作文件

知识大全 ASP+上传文件语法

ASP+上传文件语法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  这里有一个怎样用ASP+上传

知识大全 nginx+php上传大文件解决办法

最常用的方法是通过设置nginx的client_max_body_size解决nginx+php上传大文件主要是设置上传文件大小和php脚本运行时长即可用nginx来做webserver的时上传大文件