java上传多个文件

    技术2022-05-11  66

    原本打算用struts的FormFile 做同时上传多个文件的解析,实践中才发现FormFile只能处理一个文件的上传操作,不支持同时多个文件上传,真是期待struts能进一步完善这部分功能啊。

    以下同样用到struts,直接从ActionForm中获得多个文件,进行解析的。

    html:

    注意:<form action="*"  mothed="post" enctype="multipart/form-data">的设置

    action类:

    package test;

    import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.util.Hashtable;import java.util.Iterator;

    import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.upload.FormFile;

    public class UploadFileSampAction extends Action {

     public ActionForward execute(ActionMapping mapping, ActionForm form,   HttpServletRequest request, HttpServletResponse response) {  // 获得上传的文件信息  Hashtable fileh = form.getMultipartRequestHandler().getFileElements();  String dirPath = getServlet().getServletContext().getRealPath("/")    + "upload";

      if (fileh != null) {   // 如果目录不存在,则创建   File fpath = new File(dirPath);   if (!fpath.exists()) {    fpath.mkdir();   }   // 解析上传文件   for (Iterator it = fileh.keySet().iterator(); it.hasNext();) {    String key = (String) it.next();    try {     FormFile formfile = (FormFile) fileh.get(key);     String filename = formfile.getFileName().trim(); // 文件名     if (!"".equals(filename)) {      InputStream ins = formfile.getInputStream();      OutputStream os = new FileOutputStream(dirPath        + File.separatorChar + filename);      System.out.println("=======file dirPath========="        + dirPath);      System.out.println("=======file separatorChar========="        + File.separatorChar);      System.out.println("=======file filename========="        + filename);      int bytesRead = 0;      byte[] buffer = new byte[8192];      while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {       os.write(buffer, 0, bytesRead);      }      os.close();      ins.close();

         }    } catch (Exception ex) {     ex.printStackTrace();    }   }  }

      return mapping.findForward("success"); }}

    struts-config.xml配置:

    <?xml version="1.0" encoding="gb2312" ?><!DOCTYPE struts-config PUBLIC          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"          "http://struts.apache.org/dtds/struts-config_1_2.dtd">

    <struts-config> <form-beans>   <form-bean name="uploadFileForm" type="test.UploadFileSample"/> </form-beans>   <action-mappings>

      <action    path="/uploadfile"       type="test.UploadFileSampAction"       name="uploadFileForm"       scope="request">   <forward name="success"    path="/test.html"/>  </action>

      </action-mappings>

    </struts-config>

     

    web.xml配置:

    <?xml version="1.0" encoding="UTF-8"?><web-app>        <servlet>        <servlet-name>action</servlet-name>        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>         <init-param>         <param-name>config</param-name>         <param-value>     /WEB-INF/struts-config.xml    </param-value>      </init-param>     <load-on-startup>2</load-on-startup>    </servlet>

     <servlet-mapping>     <servlet-name>action</servlet-name>     <url-pattern>*.do</url-pattern> </servlet-mapping>

    </web-app>

    以上是主要的代码以及配置,最后需要部署到应用服务器上运行试试!!祝大家好运。


    最新回复(0)