struts2学习笔记(十) 利用fileUpload实现文件的上传
来源:百度文库 编辑:神马文学网 时间:2024/04/18 09:54:19
今天用struts2 的fileupload 实现了文件的上传。。 1 web.xml
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
index.jsp
2 struts.xml
/show.jsp
/index.jsp
/index.jsp
application/vnd.ms-powerpoint,image/bmp,image/png,image/gif,image/jpeg,image/pjpeg,image/jpg,application/msword,audio/x-mpeg,text/html,text/plain
8192000
3 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; private List fileFileName; private List fileContentType; public List getFile() {
return file;
} public void setFile(List file) {
this.file = file;
} public List getFileContentType() {
return fileContentType;
} public void setFileContentType(List fileContentType) {
this.fileContentType = fileContentType;
} public List getFileFileName() {
return fileFileName;
} public void setFileFileName(List fileFileName) {
this.fileFileName = fileFileName;
} /**
* 动态上传文件
* @return
* @throws Exception
*/
public String Upload() throws Exception { InputStream is=null;
OutputStream ops=null;
for(int i=0;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";
}
}4 jsp <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
My JSP 'upload.jsp' starting page
我试了下,上传成功,比较好用。 可以通过设置来设定上传文件的类型,8192000来设置上传文件的最大极限。
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
application/vnd.ms-powerpoint,image/bmp,image/png,image/gif,image/jpeg,image/pjpeg,image/jpg,application/msword,audio/x-mpeg,text/html,text/plain
8192000
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
return file;
} public void setFile(List
this.file = file;
} public List
return fileContentType;
} public void setFileContentType(List
this.fileContentType = fileContentType;
} public List
return fileFileName;
} public void setFileFileName(List
this.fileFileName = fileFileName;
} /**
* 动态上传文件
* @return
* @throws Exception
*/
public String Upload() throws Exception { InputStream is=null;
OutputStream ops=null;
for(int i=0;i
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";
}
}4 jsp <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
选择上传的文件: | <%-- |
我试了下,上传成功,比较好用。 可以通过设置来设定上传文件的类型,8192000来设置上传文件的最大极限。
struts2学习笔记(十) 利用fileUpload实现文件的上传
struts2学习笔记(十) 利用fileUpload实现文件的上传
struts2学习笔记(10) 利用fileUpload实现文件的上传
利用Jakarta commons fileupload组件实现多文件上传
利用Jakarta commons fileupload组件实现多文件上传
用commons.fileupload实现文件的上传和下载
使用Anthem.NET 1.5中的FileUpload控件实现Ajax方式的文件上传 -...
利用MultipartFile实现文件上传
使用FileUpload控件做文件上传
使用FileUpload控件做文件上传
利用http上传文件(关于RFC1867的使用小结)
struts2.01学习笔记! dhj1
通过网页实现文件上传
文件上传原理简单实现
Spring中实现文件上传
文件上传原理简单实现
ASP 中实现文件上传方法的研究
struts2学习笔记(4)——数据类型转换
struts2学习笔记(2)struts2中的类型转换_0-1World
commons-fileupload 上传时的中文乱码问题
在Struts 2中实现文件上传
在Struts 2中实现文件上传
在Struts 2中实现文件上传
使用ASP实现文件上载(上传)的各种方法 | ASP文章中心 | ASP | SQL SERVER | XMLHTTP | ACCESS | PhpUp Studio