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

Struts輕松搞定多文件上傳

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

  在前面的《Apache Commons FileUpload實現多文件上傳》文章中介紹了使用第三方組件ApacheCommons FileUpload來實現多文件的上傳對比今天用struts實現的多文件上傳總覺得還是struts用起來更加順手也更加簡潔方便不忍自己獨享所以拿出來跟大家一塊分享一下如果你讀過前面提到的那篇文章相信再理解本文將會更加容易另外提供源碼以方便大家學習由於本文屬於一片純技術文章廢話就不多說了下面看一下struts是如何實現多文件上傳的吧

  首先來看前台在前台頁面中通過js實現上傳控件的添加和刪除描述的詳細一些就是點一下增加一行按鈕就會增加一個上傳控件同樣點擊該控件後邊的刪除則會將該上傳控件去除目的在於用js實現添加任意數目的文件的功能

  [html]

  <span ><%@ page language=java pageEncoding=GB%>

  <%@ taglib uri=/tagshtml prefix=html %>

  <!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN>

  <html>

  <head>

  <title>multiUploadDemo</title>

  <script type=text/javascript src=js/myjsjs>

  </script>

  </head>

  <body bgcolor=#ffffff>

  <div id=status></div>

  <form method=post action=uploaddo enctype=multipart/formdata >

  <table id=tb></table>

  <input type=button name=AddOnLine value=增加一行 onclick=additem(tb)/>

  <input type=submit name=btnUpload value=上傳 onclick=upload()/>

  </form>

  </body>

  </html>

  </span>

  在前台頁面中引用的js文件中的代碼如下

  [javascript]

  <span > var num = ;

  function upload(){

  documentgetElementById(status)innerHTML = 文件上傳中;

  }

  function additem(id)

  {

  var rowcellstr;

  row = eval(documentall[++id++])insertRow();

  if(row != null )

  {

  cell = rowinsertCell();

  str=<input type=++file++ name=uploadFile[+ num +]file><input type=++button++ value=++刪除++ onclick=deleteitem(this++tb++);>

  cellinnerHTML=str;

  }

  num++;

  }

  function deleteitem(objid)

  {

  var rowNumcurRow;

  curRow = objparentNodeparentNode;

  rowNum = eval(documentall+id)rowslength ;

  eval(documentall[++id++])deleteRow(curRowrowIndex);

  }

  function callback(msg)

  {

  documentgetElementById(status)innerHTML = 文件上傳完成<br> + msg;

  }</span>

  前台頁面在實現了動態添加上傳控件的功能後真正用來上傳文件的後台代碼就要隆重登場了後台代碼中新建三個類他們分別為UploadActionFormjavaUploadFilejava以及UploadTestActionjava

  UploadActionFormjava類代碼如下

  [java]

  <span >package myupload;

  import javautilArrayList;

  import javautilList;

  import orgapachestrutsactionActionForm;

  import orgapachestrutsuploadFormFile;

  public class UploadActionForm extends ActionForm {

  private List myFiles;

  public UploadActionForm() {

  myFiles = new ArrayList();

  }

  public List getMyFiles() {

  return myFiles;

  }

  // 注意這個方法的定義 不加中間的循環是會出錯的

  public UploadFile getUploadFile(int index) {

  int size = myFilessize();

  if (index > size ) {

  for (int i = ; i < index size + ; i++) {

  myFilesadd(new UploadFile());

  }

  }

  return (UploadFile) myFilesget(index);

  }

  public void setMyFiles(List myFiles) {

  thismyFiles = myFiles;

  }

  }

  </span>

  UploadFilejava類代碼如下

  [java]

  <span >package myupload;

  import javaioSerializable;

  import orgapachestrutsuploadFormFile;

  public class UploadFile implements Serializable {

  private FormFile file;

  public FormFile getFile() {

  Systemoutprintln(run  in  uploadFilegetFile());

  return file;

  }

  public void setFile(FormFile file) {

  thisfile = file;

  }

  }

  </span>

  UploadTestActionjava類代碼如下

  [java]

  <span >package myupload;

  import javaioFileOutputStream;

  import javaioIOException;

  import javaioPrintWriter;

  import javautilArrayList;

  import javaxservletServletException;

  import javaxservlethttpHttpServletRequest;

  import javaxservlethttpHttpServletResponse;

  import orgapachestrutsactionAction;

  import orgapachestrutsactionActionForm;

  import orgapachestrutsactionActionForward;

  import orgapachestrutsactionActionMapping;

  import orgapachestrutsuploadFormFile;

  import llectionsList;

  public class UploadTestAction extends Action {

  public ActionForward execute(ActionMapping mapping ActionForm form

  HttpServletRequest request HttpServletResponse response)

  throws IOException ServletException {

  UploadActionForm multiUploadForm = (UploadActionForm) form;

  ArrayList myFiles = (ArrayList)multiUploadFormgetMyFiles();

  String fileStr = ;

  Systemoutprintln(myFilessize());

  for (int i = ; i < myFilessize(); i++) {

  UploadFile uploadFile = (UploadFile) myFilesget(i);

  FormFile file = uploadFilegetFile();

  if (file == null) {

  Systemoutprintln(file  is  null);

  } else {

  FileOutputStream fos = new FileOutputStream(H:\\ + filegetFileName());

  foswrite(filegetFileData());

  fosflush();

  fosclose();

  }

  }

  return mappingfindForward(success);

  }

  }</span>

  最後就是我們的strutsconfigxml文件了

  [html]

  <span ><?xml version= encoding=ISO ?>

  <!DOCTYPE strutsconfig PUBLIC

  //Apache Software Foundation//DTD Struts Configuration //EN

  /struts/dtds/strutsconfig__dtd>

  <strutsconfig>

  <formbeans>

  <formbean name=uploadForm type=myuploadUploadActionForm/>

  </formbeans>

  <actionmappings>

  <action path=/upload

  type=myuploadUploadTestAction

  name=uploadForm

  scope=request

  >

  <forward name=success path=/upload_successjsp/>

  </action>

  </actionmappings>

  <controller maxFileSize=M/>

  </strutsconfig></span>

  該文件中配置了jsp頁面提交後交由哪個Action進行處理的具體信息以及上傳成功後的頁面跳轉信息相信通過應用struts你會發現實現多文件上傳會是一件更簡單的事情


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28178.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.