`

老师说struts2文件上传而已,你看下面。

阅读更多
1.文件上传的页面upload.jsp。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>文件上传</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">
  </head>
  
  <body>
   <form action="user_test" method="post" enctype="multipart/form-data">
   	文件:<input type="file" name="file"/>
   <br/>
   <input type="submit" value="submit">
   </form>
  </body>
</html>

注意:
1.文件上传操作表单的method必须为post。
2.表单数据编码的enctype为multipart/form-data
3.记住file的name为file一会说它。

2.struts2文件上传默认使用的是apache commons的FileUpload。
action层的代码这个样子的:
public class UserAction extends ActionSupport {

       /**文件上传相关属性**/
	private File file;//前台表单名字需叫file
	private String fileFileName;
	private String fileContentType;
        //setter and getter 此处省略了
       //上传的主要方法
        @SuppressWarnings("deprecation")
	protected boolean upload(){
		boolean flag = false;
		InputStream is = null;
		OutputStream os = null;
		try{
		 is = new FileInputStream(file);
		String root = ServletActionContext.getRequest().getRealPath("/upload");//文件保存的路径
		File destFile = new File(root,this.getFileFileName());
		os = new FileOutputStream(destFile);
		byte[] buffer = new byte[400];
		int length =0;
		while((length = is.read(buffer))>0){
			os.write(buffer,0,length);
		}
		flag = true;
		}catch(Exception ex){
			flag = false;
		}finally{
			try {
				is.close();
				os.close();
			} catch (IOException e) {
				//do nothing
			}
			
		}
		return flag;
		
	}
	


}
/**
*前台表单提交到这里
*/
public String test(){
		if(upload()){
			successPath = "/upload/uploadresult.jsp";
			return SUCCESS;
		}else{
			 successPath = "/upload/uploadtest.jsp";
			return SUCCESS;
		}
		
	}


注意:
1.文件上传的相关属性那里,页面file的name是file,
这里就定义一个file,fileFileName,fileContentType。
如果页面file的name为abc,
这里就定义一个abc,abcFileName,abcContentType。


3。这里以图片为例子显示上传的图片页面uploadresult.jsp。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>显示图片</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">
  </head>
  <body>
   <img  src="<%=request.getContextPath() %>/upload/<s:property value="fileFileName"/>" />
  </body>
</html>


分享到:
评论

相关推荐

    Struts2文件上传源码

    Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码

    struts2文件上传和下载

    struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载

    struts2文件上传

    struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传

    struts2实现文件上传下载

    在struts2基础上实现的包括单文件、多文件的上传及下载

    struts2 文件上传

    struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload

    Struts2多个文件上传

    Struts2多个文件上传 多个文件上传分为List集合和数组,下面我们着重介绍一下list集合的上传。都大同小异。

    struts2文件上传实例

    struts2文件上传实例,程序员宝典......

    struts2文件上传jar

    里面包括的是实现struts2文件上传所需要的全部jar包

    struts2文件上传的两种方法

    struts2文件上传的两种方法,单文件上传与多文件上传,很经典的,是我总结的两种方法,要看struts2文件上传,大家一定要下载看看啊

    struts2 文件上传功能

    Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的...

    Struts2文件批量上传.zip

    Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip

    struts2 文件上传 测试通过版

    struts2 fileupload 文件上传

    Struts2文件上传与下载

    Struts2中文件上传与下载案例

    Struts2文件上传

    基于Struts2实现头像或文件上传,基于Struts2实现头像或文件上传,基于Struts2实现头像或文件上传

    struts2文件上传极其简单struts2文件上传极其简单

    struts2文件上传极其简单struts2文件上传极其简单struts2文件上传极其简单struts2文件上传极其简单struts2文件上传极其简单

    struts2文件上传下载源代码

    http://blog.csdn.net/johnjobs/article/details/8076832博文中附件的下载链接

    struts2文件上传下载

    struts2文件上传与下载, &lt;s:fielderror&gt;&lt;/s:fielderror&gt; 文件"&gt;&lt;/s:file&gt; &lt;s:submit id="submit" value="sbumit"&gt;&lt;/s:submit&gt; &lt;/s:form&gt;

    struts2配置文件以及代码示例

    struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置文件以及代码示例struts2配置...

    JavaEE Struts文件上传

    JavaEE Struts文件上传

Global site tag (gtag.js) - Google Analytics