知识大全 基于Struts1.2的动态多文件上传
Posted 知
篇首语:人若是把一生的光阴虚度,便是抛下黄金未买一物。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 基于Struts1.2的动态多文件上传相关的知识,希望对你有一定的参考价值。
基于Struts1.2的动态多文件上传 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
首先是web xml配置
<?xml version= encoding= ISO ?> <!DOCTYPE web app PUBLIC //Sun Microsystems Inc //DTD Web Application //EN app_ _ dtd >
<web app>
<servlet> <servlet name>action</servlet name> <servlet class> apache struts action ActionServlet</servlet class> <init param> <param name>config</param name> <param value>/WEB INF/struts config xml</param value> </init param> <load on startup> </load on startup> </servlet>
<servlet mapping> <servlet name>action</servlet name> <url pattern>* do</url pattern> </servlet mapping>
</web app>然后是struts config xml
<?xml version= encoding= UTF ?> <!DOCTYPE struts config PUBLIC //Apache Sofare Foundation//DTD Struts Configuration //EN config_ _ dtd >
<struts config> <data sources /> <form beans > <form bean name= multiUploadForm type= hb struts form MultiUploadForm /> </form beans>
<global exceptions /> <global forwards /> <action mappings > <action attribute= multiUploadForm name= multiUploadForm path= /multiUpload scope= request type= hb struts action MultiUploadAction validate= false />
</action mappings> <message resources parameter= hb struts ApplicationResources /> </struts config>
然后是页面test jsp 这段就是网上COPY的动态生成页面
<%@ page language= java pageEncoding= utf %>
<%@ taglib uri= bean prefix= bean %> <%@ taglib uri= prefix= %> <%@ taglib uri= logic prefix= logic %> <%@ taglib uri= tiles prefix= tiles %>
<!DOCTYPE HTML PUBLIC //W C//DTD HTML Transitional//EN > <:> <head> <title> multiUploadDemo </title>
<script language= javascript > var num = ;
function upload() document getElementById( status ) innerHTML = 文件上传中 ; multiUploadForm submit();
function additem(id) var row cell str;
row = eval( document all[ + +id+ + ] ) insertRow(); if(row != null ) cell = row insertCell(); str= <input type= + + file + + name=uploadFile[ + num + ] file><input type= + + button + + value= + + 删除 + + onclick= deleteitem(this + + tb + + ); > cell innerHTML=str; num++; function deleteitem(obj id) var rowNum curRow; curRow = obj parentNode parentNode; rowNum = eval( document all +id) rows length ; eval( document all[ + +id+ + ] ) deleteRow(curRow rowIndex); function callback(msg) document getElementById( status ) innerHTML = 文件上传完成 <br> + msg; </script> <body bgcolor= #ffffff > <div id= status ></div> <:form method= post action= /multiUpload do enctype= multipart/form data target= hidden_frame >
<table id= tb > </table>
</:form> <iframe name= hidden_frame id= hidden_frame ></iframe> <input type= button name= btnAddFile value= Add File onclick= additem( tb ) /> <input type= button name= btnUpload value= upload onclick= upload() /> </body> </:>
下面是FORM和ACTION
我把下面 个文件放在FORM包中UploadFile java
package hb struts form; import java io Serializable;
import apache struts upload FormFile;
public class UploadFile implements Serializable private FormFile file; public FormFile getFile() System out println( run in uploadFile getFile() ); return file; public void setFile(FormFile file) this file = file;
MultiUploadForm java
package hb struts form; import java util ArrayList; import java util List;
import apache struts action ActionForm;
import hb struts UploadFile;
public class MultiUploadForm extends ActionForm private List myFiles;
public MultiUploadForm() myFiles = new ArrayList(); myFiles add(new UploadFile()); public List getMyFiles() return myFiles;
//注意这个方法的定义 不加中间的循环是会出错的
public UploadFile getUploadFile(int index) int size = myFiles size(); if(index>size ) for(int i= ;i<index size+ ;i++) myFiles add(new UploadFile()); return (UploadFile)myFiles get(index); public void setMyFiles(List myFiles) this myFiles = myFiles;
最后是action
package hb struts action;
import java io IOException; import java io PrintWriter; import java util List;
import javax servlet HttpServletRequest; import javax servlet HttpServletResponse;
import apache struts action Action; import apache struts action ActionForm; import apache struts action ActionForward; import apache struts action ActionMapping; import apache struts upload FormFile;
public class MultiUploadAction extends Action public ActionForward execute(ActionMapping mapping ActionForm form HttpServletRequest request HttpServletResponse response) MultiUploadForm multiUploadForm = (MultiUploadForm) form;
List myFiles = multiUploadForm getMyFiles(); String fileStr = ; for (int i = ; i < myFiles size(); i++) UploadFile uploadFile = (UploadFile) myFiles get(i); FormFile file = uploadFile getFile();
if (file == null) System out println( file is null ); else
// 能运行到这里 就可以使用单个文件上传的方法进行上传了 循环而已 System out println( filename>>>>>>>>>> + file getFileName()); System out println( file size>>>>>>>>>> + file getFileSize()); fileStr += filename::: + file getFileName() + file size::: + file getFileSize();
// 返回文本 try response setHeader( ContentType text/;charset=gbk ); PrintWriter out = response getWriter(); out write( <script>parent callback( upload file success + fileStr + )</script> ); catch (IOException e) e printStackTrace();
return null;
cha138/Article/program/Java/ky/201311/28228
相关参考
Struts轻松搞定多文件上传 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 在前面的《Apac
ASP.NET带进度条多文件上传 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! )解压jquer
JavaStruts1.2中文乱码处理 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 建立文件继
cha138/Article/program/net/201311/14386
一款利用asp文件上传组件把文件上传到服务器之前进行判断文件大小否超过指定大小了本实例讲的是upload_xsoft文件上传组件哦filefilesize>就可以限制文件为多少klishix
最常用的方法是通过设置nginx的client_max_body_size解决nginx+php上传大文件主要是设置上传文件大小和php脚本运行时长即可用nginx来做webserver的时上传大文件
PHP文件编程综合案例-文件上传的实现 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! PHP文件
PHP文件上传代码用法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! php文件上传代码编写过程
我们是利用upload文件上传组件进行文件上传哦 <%ServerScriptTimeOut=%><!#includefile="connasp"><!#inclu
ASP+上传文件语法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 这里有一个怎样用ASP+上传