做了一個上傳下載的程式,還有很多地方需要改進的地方,還是先貼上來吧,以後在用到的時候做個參照: 上傳:用到struts提供的FormFile類,以及html:file標籤.在設計上傳頁面的時候有多個上傳瀏覽框.我這回用了一個土方法在頁面上進行了迴圈賦值,所以在ActionForm中FormFile定義成陣列,不知道還有沒有更好的方法. java 程式碼 - public class UploadAction extends Action {
-
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
-
- int totalFileSize = 0;
- List list = new ArrayList();
-
- String path = servlet.getInitParameter("file-upload");
-
- UploadForm upform = (UploadForm) form;
-
- FormFile[] file = upform.getFile();
- for (int i = 0; i < file.length; i++) {
-
- String fileName = file[i].getFileName();
-
- String size = file[i].getFileSize()+"";
-
- totalFileSize += file[i].getFileSize();
- if (fileName.equals("") || fileName.equals(null)) {
- request.setAttribute("total",totalFileSize);
- request.setAttribute("filearraylist",list);
- return mapping.findForward("success");
- } else {
-
- uploadFile(file[i], path, fileName, size, upform, request);
-
- FileInfo fi = new FileInfo();
- fi.setFileName(fileName);
- fi.setFileSize(size);
-
- list.add(fi);
-
-
- request.setAttribute("flags", true);
-
-
- File fileList = new File(path);
-
- String[] files = fileList.list();
-
- request.setAttribute("fileList", files);
- }
- }
- request.setAttribute("total",totalFileSize);
- request.setAttribute("filearraylist",list);
- return mapping.findForward("success");
- }
-
- private void uploadFile(FormFile file, String path, String fileName,
- String size, UploadForm upform, HttpServletRequest request) {
- try {
- InputStream is = file.getInputStream();
- OutputStream os = new FileOutputStream(path + "/" + fileName);
-
- byte[] buffer = new byte[20480];
- while ((is.read(buffer, 0, buffer.length)) != -1) {
- os.write(buffer, 0, buffer.length);
- }
- os.flush();
- file.destroy();
-
- close(is, os);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- private void close(InputStream is, OutputStream os) throws IOException {
- is.close();
- os.close();
- }
-
- }
-
- public class UploadForm extends ActionForm {
-
- private FormFile[] file = new FormFile[4];
-
- private String fileName;
-
- private String size;
-
- public FormFile[] getFile() {
- return file;
- }
-
- public void setFile(FormFile[] file) {
- this.file = file;
- }
-
- public String getFileName() {
- return fileName;
- }
-
- public void setFileName(String fileName) {
- this.fileName = fileName;
- }
-
- public String getSize() {
- return size;
- }
-
- public void setSize(String size) {
- this.size = size;
- }
-
- }
-
upload頁面: jsp 程式碼 - <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
- <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
- <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
-
- <%@ page language="java" contentType="text/html; charset=GB2312"%>
-
- <logic:present name="flags">
- <script language="javascript" >alert("上傳成功!")script>
- logic:present>
- <html:html>
- <head>
- <title>test struts upload filetitle>
- head>
- <body>
- <html:errors/>
- <html:messages id="messages" message="true">
- <LI><bean:write name="messages" />LI>
- html:messages>
- <html:form action="upload" enctype="multipart/form-data">
- <table cellpadding="0" cellspacing="0" border="0" >
- <tr>
- <td>
- <table height="80%" width="80%">
- <tr>
- <td bgcolor="#ddcc">
- 選擇檔案
- td>
- tr>
- table>
- td>
- tr>
- <tr height="50">
- <td width=2%>
- <table height="80%" width="80%" cellpadding="0" cellspacing="0" border="0">
- <tr>
- <% for(int i=0;i<4;i++){%>
- <td>
- <html:file property='' />
- td>
- <%
- }
- %>
- tr>
- table>
- td>
- tr>
- <tr>
- <td>
- <html:submit onclick="return(confirm('你確認要上傳檔案嗎?'))">上傳檔案html:submit>
- td>
- tr>
- <tr>
- <td>
- <table height="80%" width="80%">
- <tr bgcolor="#ddcc">
- <td width="50%">檔名td>
- <td width="50%">檔案大小(byte)td>
- tr>
- <tr>
- <logic:empty name="filearraylist" scope="request">
- <tr><td width="50%">總計td><td width="50%">0td>tr>
- logic:empty>
- <logic:notEmpty name="filearraylist" scope="request">
- <logic:iterate id="element" name="filearraylist" scope="request">
- <tr>
- <td width="50%"><bean:write name="element" property="fileName" />td>
- <td width="50%"><bean:write name="element" property="fileSize" />td>
- tr>
- logic:iterate>
- <tr><td>總計td><td><bean:write name="total"/>td>tr>
- logic:notEmpty>
- tr>
- table>
- td>
- tr>
- table>
- html:form>
- <br/><br/><br/>
- <%int i=0;%>
- <logic:notEmpty name="fileList" scope="request">
- <table cellpadding="0" cellspacing="0" border="0">
- <tr>
- <td>
- <logic:iterate id="element" name="fileList" scope="request">
- <%= ++i%>
- <a href="?filename=">
|