知识大全 分享:用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相关参考