熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

分享:用Struts上傳多個文件的方法

2022-06-13   來源: Java開源技術 

  最近在做Struts項目時遇到了上傳多個文件的問題在網上查了不少資料也沒有找到用Struts上傳多個文件的例子我經過幾天的研究實現了用Struts上傳多個文件的功能現在貼出來讓大家共享!
  
  建立ActionForm
  
  package ehustrutsform;
  
  import javaxservlethttpHttpServletRequest;
  
  import orgapachestrutsactionActionError;
  
  import orgapachestrutsactionActionErrors;
  
  import orgapachestrutsactionActionForm;
  
  import orgapachestrutsactionActionMapping;
  
  import orgapachestrutsuploadFormFile;
  
  import orgapachestrutsuploadMultipartRequestHandler;
  
  /**
  
  * <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 = orgapachestrutswebappuploadMaxLengthExceeded;
  
  protected FormFile theFile;
  
  protected FormFile theFile;
  
  public FormFile getTheFile()
  
  {
  
  return theFile;
  
  }
  
  public void setTheFile(FormFile theFile)
  
  {
  
  thistheFile = theFile;
  
  }
  
  public ActionErrors validate(ActionMapping mapping
  
  HttpServletRequest request)
  
  {
  
  ActionErrors errors = null;
  
  //has the maximum length been exceeded?
  
  Boolean maxLengthExceeded = (Boolean) request
  
  getAttribute(MultipartRequestHandlerATTRIBUTE_MAX_LENGTH_EXCEEDED);
  
  if ((maxLengthExceeded != null) && (maxLengthExceededbooleanValue()))
  
  {
  
  errors = new ActionErrors();
  
  errorsadd(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)
  
  {
  
  thistheFile = theFile;
  
  }
  
  }
  
  建立ActionServlet
  
  package ehustrutsaction;
  
  import javaio*;
  
  import javaxservlethttp*;
  
  import orgapachestrutsaction*;
  
  import orgapachestrutsuploadFormFile;
  
  import ehustrutsformUpLoadForm;
  
  /**
  
  * <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 = requestgetCharacterEncoding();
  
  if ((encoding != null) && (encodingequalsIgnoreCase(utf)))
  
  {
  
  responsesetContentType(text/html; charset=gb);//如果沒有指定編碼編碼格式為gb
  
  }
  
  UpLoadForm theForm = (UpLoadForm) form;
  
  FormFile file = theFormgetTheFile();//取得上傳的文件
  
  FormFile file=theFormgetTheFile();
  
  try
  
  {
  
  /*
  
  * 取當前系統路徑D:\Tomcat\webapps\coka\ 其中coka 為當前context
  
  */
  
  String filePath = thisgetServlet()getServletContext()
  
  getRealPath(/);
  
  InputStream stream = filegetInputStream();//把文件讀入
  
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
  /*
  
  * 建立一個上傳文件的輸出流 如果是linux系統請把UploadFiles後的\\換成/
  
  */
  
  OutputStream bos = new FileOutputStream(filePath +
  
  UploadFiles\\+filegetFileName());
  
  //D:\Tomcat\webapps\coka\UploadFiles\DSCJPG
  
  /* Systemoutprintln(filePath +
  
  UploadFiles\\+filegetFileName());
  
  Systemoutprintln(filePath);*/
  
  requestsetAttribute(fileNamefilePath + /
  
  + filegetFileName());
  
  int bytesRead = ;
  
  byte[] buffer = new byte[];
  
  while ((bytesRead = streamread(buffer )) != )
  
  {
  
  boswrite(buffer bytesRead);//將文件寫入服務器
  
  }
  
  bosclose();
  
  streamclose();
  
  InputStream stream = filegetInputStream();//把文件讀入
  
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
  OutputStream bos = new FileOutputStream(filePath +
  
  UploadFiles\\+filegetFileName());//建立一個上傳文件的輸出流
  
  int bytesRead = ;
  
  byte[] buffer = new byte[];
  
  int i=;
  
  while ((bytesRead = streamread(buffer )) != )
  
  {
  
  boswrite(buffer bytesRead);//將文件寫入服務器
  
  }
  
  bosclose();
  
  streamclose();
  
  } catch (Exception e)
  
  {
  
  Systemerrprint(e);
  
  }
  
  return mappingfindForward(display);
  
  }
  
  }
  
  建立上傳用的JSP文件 uploadjsp
  
  <%@ taglib uri=html prefix=html %>
  
  <html:html>
  
  <head>
  
  <title>用Struts上傳文件</title>
  
  </head>
  
  <body>
  
  <html:form action=/uploadsAction enctype=multipart/formdata>
  
  <html:file property=theFile/>
  
  <html:file property=theFile/>
  
  <html:submit/>
  
  </html:form>
  
  </body>
  
  </html:html>
  
  配置strutsconfigxml文件
  
  <?xml version= encoding=UTF?>
  
  <!DOCTYPE strutsconfig PUBLIC //Apache Software Foundation//DTD Struts Configuration //EN config__dtd>
  
  <strutsconfig>
  
  <datasources />
  
  <formbeans >
  
  <formbean name=uploadsForm type=ehustrutsformUpLoadForm />
  
  </formbeans>
  
  <globalexceptions />
  
  <globalforwards >
  
  </globalforwards>
  
  <actionmappings >
  
  <action name=uploadsForm type=ehustrutsactionUpLoadAction path=/uploadsAction>
  
  <forward name=display path=/displayjsp />
  
  </action>
  
  </actionmappings>
  
  </strutsconfig>
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28148.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.