您现在的位置是:首页 >

百度网盘上传不了文件怎么办 分享:用Struts上传多个文件的方法

火烧 2021-08-12 02:14:17 1062
分享:用Strut 上传多个文件的方法   最近在做Strut 项目时遇到了上传多个文件的问题 在网上查了不少资料 也没有找到用Strut 上传多个文件的例子 我经过几天的研究 实现了用Strut 上

分享:用Struts上传多个文件的方法  

百度网盘上传不了文件怎么办 分享:用Struts上传多个文件的方法
  最近在做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 webappscoka 其中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 webappscokaUploadFilesDSC JPG        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> lishixinzhi/Article/program/Java/ky/201311/28148  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码