浏览 4136 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-12-30
第二步:用过滤器在web.xml中配置Struts2的核心控制器。 <!-- 配置struts2过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 第三步: 上传的jsp页面: <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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"> --> <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="file"; button.type="button"; button.value="-"; button.onclick=function(){ td.removeChild(br); td.removeChild(input); td.removeChild(button); } td.appendChild(br); td.appendChild(input); td.appendChild(button); } </script> </head> <body> <s:form action="upload!Upload.action" method="post" enctype="multipart/form-data"> <table align="center" width="60%" border="1"> <tr> <td> 选择上传的文件: </td> <td id="more"> <%--<s:file name="file" label="选择上传的文件"></s:file>--%> <input type="file" name="file"> <input type="button" value="+" onclick="addMore()"/> </td> </tr> <tr> <td> </td> <td> <s:submit value="上传" align="center"></s:submit> </td> </tr> </table> </s:form> </body> </html> 第四步:后台Action package com.struts2.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private List<File> file; private List<String> fileFileName; private List<String> fileContentType; public List<File> getFile() { return file; } public void setFile(List<File> file) { this.file = file; } public List<String> getFileContentType() { return fileContentType; } public void setFileContentType(List<String> fileContentType) { this.fileContentType = fileContentType; } public List<String> getFileFileName() { return fileFileName; } public void setFileFileName(List<String> fileFileName) { this.fileFileName = fileFileName; } /** * 动态上传文件 * @return * @throws Exception */ public String Upload() throws Exception { InputStream is=null; OutputStream ops=null; for(int i=0;i<file.size();i++){ try{ is=new FileInputStream(file.get(i)); String root=ServletActionContext.getRequest().getRealPath("/uploads"); File destFile=new File(root+"/",this.getFileFileName().get(i)); //File destFile=new File(root,this.getFileFileName().get(i)); ops=new FileOutputStream(destFile); byte [] b=new byte[400]; int length=0; while((length=is.read(b))>0){ ops.write(b,0,length); //ops.write(b); 这样子同样可行 } }catch(Exception ex){ ex.printStackTrace(); }finally{ is.close(); ops.close(); } } return "show"; } } 第五步:在src建一个Struts2的配置文件 struts.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > <struts> <include file="struts-default.xml"></include> < -- 这里设置编码方式,注意上传页面也要用此编码方式,否则得到的文件名是乱码-- > <constant name="struts.i18n.encoding" value="gbk"></constant> <constant name="struts.multipart.saveDir" value="d:\struts2upload\"></constant> <package name="struts2ofupload" extends="struts-default"> <action name="upload" class="com.struts2.action.UploadAction"> <result name="upload">/upload.jsp</result> <result name="show">/show.jsp</result> <result name="input">/upload.jsp</result> <!—定义拦截器 拦截上传的文件格式,上传文件的大小 注:文件格式可以参考tomcat下conf下web.xml 的一些文件格式-- > <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,application/msword,audio/x-mpeg,text/html,text/plain</param> <param name="maximumSize">8192000</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package> </struts> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |