JSP/Java/Struts實現檔案上傳__div

來源:互聯網
上載者:User

 首先說明struts.xml裡面如果要限制上傳檔案的類型可以配置一個攔截器:

<!-- 通過動態設定allowTypes的屬性來動態指定允許上傳的檔案類型 -->

             <param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg</param>

單檔案上傳JSP:

<body><center><form action="upload.action" method="post"enctype="multipart/form-data"><table cellpadding="0" cellspacing="0" style="width: 350px;"><tr><td colspan="2" class="getcenter">單檔案上傳</td></tr><tr><td>檔案標題:</td><td><input type="text" name="title" class="txt" /></td></tr><tr><td>選擇檔案:</td><td><input type="file" name="file" class="file" /></td></tr><tr><td colspan="2" class="getcenter"><input type="submit" value="上傳" /><input type="reset" value="重設" /></td></tr></table></form></center></body>
單檔案上傳成功介面:
<body>檔案上傳成功!!!<hr>檔案標題:${upload.title}<br />檔案名稱:${upload.fileFileName}<br />檔案類型:${upload.fileContentType}<br /></body>

多檔案:

<script type="text/javascript">      function addTR(){         var tab = document.getElementById("tab");         var newRow = tab.insertRow(tab.rows.length);         newRow.insertCell(0).innerHTML="<input type=\"file\" name=\"file\"/>";      }            function delRow(){         var tab = document.getElementById("tab");         if(tab.rows.length>3){            tab.deleteRow(tab.rows.length-1);         }else{            alert("至少保留二行。");         }      }           </script></head><body><center><form action="multiupload.action" method="post"enctype="multipart/form-data"><table cellpadding="0" cellspacing="0" style="width: 500px;"id="tab"><tr><td><input type="button" value="增加上傳檔案" onclick="addTR()" /><input type="button" value="減少" onclick="delRow()" /><input type="reset" value="重設" /><input type="submit" value="上傳" /></td></tr><tr><td><input type="file" name="file" /></td></tr><tr><td><input type="file" name="file" /></td></tr></table></form></center></body>

多檔案成功介面:

<body><c:forEach items="${fileFileName}" var="file" varStatus="status">檔案上傳成功!!!<hr><br />檔案名稱:${upload.fileFileName[status.index]}<br />檔案類型:${upload.fileContentType[status.index]}<br /></c:forEach></body>

web.xml://配置struts-clean,避免第一次上傳取不到檔案

<filter><filter-name>struts-cleanup</filter-name><filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class></filter><filter-mapping><filter-name>struts-cleanup</filter-name><url-pattern>/*</url-pattern></filter-mapping>

struts.xml

<struts><constant name="struts.i18n.encoding" value="utf-8"></constant><package name="default" namespace="/" extends="struts-default"><action name="product" class="com.tudou.struts.action.ProductAction"></action><action name="upload" class="com.tudou.struts.action.FileUpLoadAction"><param name="savePath">/uploadfiles</param><result name="success">/fileSuccess.jsp</result></action><action name="multiupload" class="com.tudou.struts.action.MultiFileUpLoadAction"><param name="savePath">/uploadfiles</param><result name="success">/success.jsp</result></action></package><package name="myPackage" namespace="/" extends="struts-default"><action name="login" class="com.tudou.struts.action.LoginAction"><result name="input">/login.jsp</result><result name="index">/index.jsp</result></action></package>
單檔案action:
package com.tudou.struts.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;public class FileUpLoadAction {private String title; // 檔案標題private File file; // 檔案域private String fileContentType; // 檔案類型private String fileFileName; // 檔案名稱private String savePath; // 上傳地址public void setSavePath(String savePath) {this.savePath = savePath;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public File getFile() {return file;}public void setFile(File file) {this.file = file;}public String getFileContentType() {return fileContentType;}public void setFileContentType(String fileContentType) {this.fileContentType = fileContentType;}public String getFileFileName() {return fileFileName;}public void setFileFileName(String fileFileName) {this.fileFileName = fileFileName;}// 返回儲存地址public String getSavePath() throws Exception {return ServletActionContext.getRequest().getRealPath(savePath);}public String execute() throws Exception {// 以伺服器的檔案儲存地址的原檔案名稱建立上傳檔案輸出資料流FileOutputStream fos = new FileOutputStream(this.getSavePath() + "\\"+ this.getFileFileName());// 以上傳檔案建立檔案輸入資料流FileInputStream fis = new FileInputStream(this.getFile());// 將上傳檔案內容寫入伺服器byte[] b = new byte[1024];int len = 0;while ((len = fis.read(b)) > 0) {fos.write(b, 0, len);}ActionContext.getContext().put("upload", this);return "success";}}

多檔案上傳action:

package com.tudou.struts.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;public class MultiFileUpLoadAction {// private String[] title; // 檔案標題private File[] file; // 檔案域private String fileContentType[]; // 檔案類型private String[] fileFileName; // 檔案名稱private String savePath; // 上傳地址public void setSavePath(String savePath) {this.savePath = savePath;}// public String[] getTitle() {// return title;// }//// public void setTitle(String[] title) {// this.title = title;// }public File[] getFile() {return file;}public void setFile(File[] file) {this.file = file;}public String[] getFileContentType() {return fileContentType;}public void setFileContentType(String[] fileContentType) {this.fileContentType = fileContentType;}public String[] getFileFileName() {return fileFileName;}public void setFileFileName(String[] fileFileName) {this.fileFileName = fileFileName;}// 返回儲存地址public String getSavePath() throws Exception {return ServletActionContext.getRequest().getRealPath(savePath);}public String execute() throws Exception {for (int i = 0; i < file.length; i++) {// 以伺服器的檔案儲存地址的原檔案名稱建立上傳檔案輸出資料流FileOutputStream fos = new FileOutputStream(this.getSavePath()+ "\\" + this.getFileFileName()[i]);// 以上傳檔案建立檔案輸入資料流FileInputStream fis = new FileInputStream(this.getFile()[i]);// 將上傳檔案內容寫入伺服器byte[] b = new byte[1024];int len = 0;while ((len = fis.read(b)) > 0) {fos.write(b, 0, len);}}ActionContext.getContext().put("upload", this);return "success";}}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.