struts2多文件上传

xiaoxiao2025-02-13  16

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'upload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> <mce:script type="text/javascript"><!-- function addMore(){ var td=document.getElementById("more"); var br=document.createElement("br"); var input=document.createElement("input"); var button=document.createElement("input"); input.type="file"; input.name="myFile"; button.type="button"; button.value="删除"; button.οnclick=function(){ td.removeChild(br); td.removeChild(input); td.removeChild(button); } td.appendChild(br); td.appendChild(input); td.appendChild(button); } // --></mce:script> </head> <body> <form action="myupload.action" method="post" enctype="multipart/form-data"> <table align="center" width="60%" border="1"> <tr> <td> 选择上传的文件: </td> <td id="more"> <input type="file" name="myFile"> <input type="button" value="增加" οnclick="addMore()"/> </td> </tr> <tr> <td> </td> <td> <input type="submit" value="上传" align="center"></s:submit> </td> </tr> </table> </form> </body> </html>

上传多文件时,需要动态生成文件上传表单。

如果需要制定文件上传的最大值,可以在struts.xml文件里面设置:

<!-- 设置文件上传的最大值 --> <constant name="struts.multipart.maxSize" value="102400000"></constant>

<!-- 多文件上传 --> <action name="myupload" class="com.struts2.ManyFileAction"> <param name="savepath">/upload</param> <result name="success" >/uploadSuc.jsp</result> </action>

package com.struts2; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.List; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class ManyFileAction extends ActionSupport { private static final long serialVersionUID = -3528215463223608989L; private List<File> myFile ; private List<String> myFileFileName ; private List<String> myFileContentType ; private String savepath ; public List<File> getMyFile() { return myFile; } public void setMyFile(List<File> myFile) { this.myFile = myFile; } public List<String> getMyFileFileName() { return myFileFileName; } public void setMyFileFileName(List<String> myFileFileName) { this.myFileFileName = myFileFileName; } public List<String> getMyFileContentType() { return myFileContentType; } public void setMyFileContentType(List<String> myFileContentType) { this.myFileContentType = myFileContentType; } public String getSavepath() { return savepath; } public void setSavepath(String savepath) { this.savepath = savepath; } public static long getSerialVersionUID() { return serialVersionUID; } public String execute()throws Exception{ FileInputStream fis = null ; FileOutputStream fos = null ; BufferedInputStream bis = null ; BufferedOutputStream bos = null ; savepath = ServletActionContext.getServletContext().getRealPath(savepath) ; System.out.println("路径:"+savepath); int size = getMyFile().size() ; if(size!=0){ for(int i=0;i<size;i++){ fis = new FileInputStream(myFile.get(i)) ; bis = new BufferedInputStream(fis) ; byte[] tmp = new byte[bis.available()] ; fos = new FileOutputStream(new File(savepath+"\\"+myFileFileName.get(i))) ; bos = new BufferedOutputStream(fos) ; int len =0 ; while((len=bis.read(tmp))!=-1){ bos.write(tmp, 0, len) ; } } bis.close() ; bos.close() ; } return SUCCESS ; } }

至此,文件上传就完成了.

相关资源:ajaxFileUpload+struts2实现多文件上传(源码)
转载请注明原文地址: https://www.6miu.com/read-5024682.html

最新回复(0)