struts實現上傳篇

xuehongliang發表於2007-06-29

做了一個上傳下載的程式,還有很多地方需要改進的地方,還是先貼上來吧,以後在用到的時候做個參照:

上傳:用到struts提供的FormFile類,以及html:file標籤.在設計上傳頁面的時候有多個上傳瀏覽框.我這回用了一個土方法在頁面上進行了迴圈賦值,所以在ActionForm中FormFile定義成陣列,不知道還有沒有更好的方法.

java 程式碼
  1. public class UploadAction extends Action {
  2. public ActionForward execute(ActionMapping mapping, ActionForm form,
  3. HttpServletRequest request, HttpServletResponse response)
  4. throws Exception {
  5. int totalFileSize = 0;
  6. List list = new ArrayList();
  7. // 在web.xml中讀取系統上傳預設路徑
  8. String path = servlet.getInitParameter("file-upload");
  9. // 獲取頁面資訊,得到上傳檔案的相關資訊.
  10. UploadForm upform = (UploadForm) form;
  11. // FormFile是struts提供的處理上傳的類
  12. FormFile[] file = upform.getFile();
  13. for (int i = 0; i < file.length; i++) {
  14. // 取得上傳檔案的檔名.
  15. String fileName = file[i].getFileName();
  16. // 取得上傳檔案的大小,轉成字串型別的目的是為了在頁面上顯示檔案大小.
  17. String size = file[i].getFileSize()+"";
  18. totalFileSize += file[i].getFileSize();
  19. if (fileName.equals("") || fileName.equals(null)) {
  20. request.setAttribute("total",totalFileSize);
  21. request.setAttribute("filearraylist",list);
  22. return mapping.findForward("success");
  23. } else {
  24. // 主要方法,上傳檔案
  25. uploadFile(file[i], path, fileName, size, upform, request);
  26. FileInfo fi = new FileInfo();
  27. fi.setFileName(fileName);
  28. fi.setFileSize(size);
  29. list.add(fi);
  30. // 上傳成功後彈出上傳成功對話方塊,在這裡加一個標誌,在頁面進行判斷用.
  31. request.setAttribute("flags", true);
  32. // 取得檔案上傳路徑
  33. File fileList = new File(path);
  34. // 取得所有檔名
  35. String[] files = fileList.list();
  36. // 將檔名放入request範圍在頁面顯示用.
  37. request.setAttribute("fileList", files);
  38. }
  39. }
  40. request.setAttribute("total",totalFileSize);
  41. request.setAttribute("filearraylist",list);
  42. return mapping.findForward("success");
  43. }
  44. private void uploadFile(FormFile file, String path, String fileName,
  45. String size, UploadForm upform, HttpServletRequest request) {
  46. try {
  47. InputStream is = file.getInputStream();
  48. OutputStream os = new FileOutputStream(path + "/" + fileName);
  49. // 檔案大小
  50. byte[] buffer = new byte[20480];
  51. while ((is.read(buffer, 0, buffer.length)) != -1) {
  52. os.write(buffer, 0, buffer.length);
  53. }
  54. os.flush();
  55. file.destroy();
  56. // 關閉流
  57. close(is, os);
  58. } catch (FileNotFoundException e) {
  59. e.printStackTrace();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. private void close(InputStream is, OutputStream os) throws IOException {
  65. is.close();
  66. os.close();
  67. }
  68. }
  69. public class UploadForm extends ActionForm {
  70. private FormFile[] file = new FormFile[4];
  71. private String fileName;
  72. private String size;
  73. public FormFile[] getFile() {
  74. return file;
  75. }
  76. public void setFile(FormFile[] file) {
  77. this.file = file;
  78. }
  79. public String getFileName() {
  80. return fileName;
  81. }
  82. public void setFileName(String fileName) {
  83. this.fileName = fileName;
  84. }
  85. public String getSize() {
  86. return size;
  87. }
  88. public void setSize(String size) {
  89. this.size = size;
  90. }
  91. }

upload頁面:

jsp 程式碼
  1. <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
  2. <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
  3. <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
  4. <%@ page language="java" contentType="text/html; charset=GB2312"%>
  5. <logic:present name="flags">
  6. <script language="javascript" >alert("上傳成功!")script>
  7. logic:present>
  8. <html:html>
  9. <head>
  10. <title>test struts upload filetitle>
  11. head>
  12. <body>
  13. <html:errors/>
  14. <html:messages id="messages" message="true">
  15. <LI><bean:write name="messages" />LI>
  16. html:messages>
  17. <html:form action="upload" enctype="multipart/form-data">
  18. <table cellpadding="0" cellspacing="0" border="0" >
  19. <tr>
  20. <td>
  21. <table height="80%" width="80%">
  22. <tr>
  23. <td bgcolor="#ddcc">
  24. 選擇檔案
  25. td>
  26. tr>
  27. table>
  28. td>
  29. tr>
  30. <tr height="50">
  31. <td width=2%>
  32. <table height="80%" width="80%" cellpadding="0" cellspacing="0" border="0">
  33. <tr>
  34. <% for(int i=0;i<4;i++){%>
  35. <td>
  36. <html:file property='' />
  37. td>
  38. <%
  39. }
  40. %>
  41. tr>
  42. table>
  43. td>
  44. tr>
  45. <tr>
  46. <td>
  47. <html:submit onclick="return(confirm('你確認要上傳檔案嗎?'))">上傳檔案html:submit>
  48. td>
  49. tr>
  50. <tr>
  51. <td>
  52. <table height="80%" width="80%">
  53. <tr bgcolor="#ddcc">
  54. <td width="50%">檔名td>
  55. <td width="50%">檔案大小(byte)td>
  56. tr>
  57. <tr>
  58. <logic:empty name="filearraylist" scope="request">
  59. <tr><td width="50%">總計td><td width="50%">0td>tr>
  60. logic:empty>
  61. <logic:notEmpty name="filearraylist" scope="request">
  62. <logic:iterate id="element" name="filearraylist" scope="request">
  63. <tr>
  64. <td width="50%"><bean:write name="element" property="fileName" />td>
  65. <td width="50%"><bean:write name="element" property="fileSize" />td>
  66. tr>
  67. logic:iterate>
  68. <tr><td>總計td><td><bean:write name="total"/>td>tr>
  69. logic:notEmpty>
  70. tr>
  71. table>
  72. td>
  73. tr>
  74. table>
  75. html:form>
  76. <br/><br/><br/>
  77. <%int i=0;%>
  78. <logic:notEmpty name="fileList" scope="request">
  79. <table cellpadding="0" cellspacing="0" border="0">
  80. <tr>
  81. <td>
  82. <logic:iterate id="element" name="fileList" scope="request">
  83. <%= ++i%>
  84. <a href="?filename=">

相關文章