您现在的位置是:首页
>
百度网盘上传不了文件怎么办 分享:用Struts上传多个文件的方法
分享:用Strut 上传多个文件的方法 最近在做Strut 项目时遇到了上传多个文件的问题 在网上查了不少资料 也没有找到用Strut 上传多个文件的例子 我经过几天的研究 实现了用Strut 上
分享:用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
很赞哦! (1038)
相关文章
- 怎么把资料上传到百度网盘 怎么把自己的资料搞到上传到百度百科上?
- 百度网盘资源怎么分享给微信好友 百度云给微信好友分享资源,别人打开分享的网页显示文件已取消,分享视频就这样,怎么办求告知谢谢
- 怎么在百度文库上传文档 如何在百度文库上传文件?
- 百度网盘下载的文件怎么解压 百度云里的文件同步和文件备份有什么区别啊?
- 百度云链接 我想问给别人分享了百度云网盘的文件,然后是通过链接的方式,那么请问那个人可以保存下来文件吗?
- linux上传下载文件命令 百度怎么上传文件?有的文件下载需要积分,但是积分不知道怎么弄?
- 共享客 除了百度文库、新浪共享,还有没有类似的文档分享的网站呢?
- 百度网盘分享的文件不存在 百度网盘分享所生成的连结任何人都可以下载吗?怎么把上传的档案加密?要输密码才能下载?
- 省考分值分配 如何下载百度文库带有分值的文件?
- 移动文件到不同文件夹 百度云分享个好友的文件夹,里面的东西会不会随着我那个文件夹里的改变而改变
爱学记

微信收款码
支付宝收款码