`
lsong89
  • 浏览: 39044 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

struts上传文件 教程

阅读更多

 

LoadImgForm.java代码:

Java代码 复制代码
  1. /*  
  2.  * Generated by MyEclipse Struts  
  3.  * Template path: templates/java/JavaClass.vtl  
  4.  */   
  5. package  com.jqqd.struts.formAction;  
  6.   
  7. import  javax.servlet.http.HttpServletRequest;  
  8. import  org.apache.struts.action.ActionErrors;  
  9. import  org.apache.struts.action.ActionForm;  
  10. import  org.apache.struts.action.ActionMapping;  
  11. import  org.apache.struts.upload.FormFile;  
  12.   
  13. /**   
  14.  * MyEclipse Struts  
  15.  * Creation date: 06-06-2008  
  16.  *   
  17.  * XDoclet definition:  
  18.  * @struts.form name="upLoadImg"  
  19.  */   
  20. public   class  UpLoadImgForm  extends  ActionForm {  
  21.     /*  
  22.      * Generated Methods  
  23.      */   
  24.   
  25.     private  FormFile file;  
  26.     /**  
  27.      *   
  28.      */   
  29.     private   static   final   long  serialVersionUID = 1L;  
  30.   
  31.     /**   
  32.      * Method validate  
  33.      * @param mapping  
  34.      * @param request  
  35.      * @return ActionErrors  
  36.      */   
  37.     public  ActionErrors validate(ActionMapping mapping,  
  38.             HttpServletRequest request) {  
  39.         // TODO Auto-generated method stub   
  40.         return   null ;  
  41.     }  
  42.   
  43.     /**   
  44.      * Method reset  
  45.      * @param mapping  
  46.      * @param request  
  47.      */   
  48.     public   void  reset(ActionMapping mapping, HttpServletRequest request) {  
  49.         // TODO Auto-generated method stub   
  50.     }  
  51.   
  52.     public  FormFile getFile() {  
  53.         return  file;  
  54.     }  
  55.   
  56.     public   void  setFile(FormFile file) {  
  57.         this .file = file;  
  58.     }  
  59.       
  60.       
  61. }  
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.jqqd.struts.formAction;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/** 
 * MyEclipse Struts
 * Creation date: 06-06-2008
 * 
 * XDoclet definition:
 * @struts.form name="upLoadImg"
 */
public class UpLoadImgForm extends ActionForm {
	/*
	 * Generated Methods
	 */

	private FormFile file;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** 
	 * Method validate
	 * @param mapping
	 * @param request
	 * @return ActionErrors
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	/** 
	 * Method reset
	 * @param mapping
	 * @param request
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// TODO Auto-generated method stub
	}

	public FormFile getFile() {
		return file;
	}

	public void setFile(FormFile file) {
		this.file = file;
	}
	
	
}

 UpLoadImgAction.java代码

Java代码 复制代码
  1. /*  
  2.  * Generated by MyEclipse Struts  
  3.  * Template path: templates/java/JavaClass.vtl  
  4.  */   
  5. package  com.jqqd.struts.action;  
  6.   
  7. import  java.io.ByteArrayOutputStream;  
  8. import  java.io.FileOutputStream;  
  9. import  java.io.InputStream;  
  10. import  java.io.OutputStream;  
  11.   
  12. import  javax.servlet.http.HttpServletRequest;  
  13. import  javax.servlet.http.HttpServletResponse;  
  14. import  org.apache.struts.action.Action;  
  15. import  org.apache.struts.action.ActionForm;  
  16. import  org.apache.struts.action.ActionForward;  
  17. import  org.apache.struts.action.ActionMapping;  
  18. import  org.apache.struts.upload.FormFile;  
  19.   
  20. import  com.jqqd.struts.formAction.UpLoadImgForm;  
  21.   
  22. /**  
  23.  * MyEclipse Struts Creation date: 06-06-2008  
  24.  *   
  25.  * XDoclet definition:  
  26.  *   
  27.  * @struts.action path="/upLoadImg" name="upLoadImg" input="/uploadImg.jsp"  
  28.  *                scope="request" validate="true"  
  29.  * @struts.action-forward name="error" path="/error.jsp"  
  30.  * @struts.action-forward name="success" path="/success.jsp"  
  31.  */   
  32. public   class  UpLoadImgAction  extends  Action {  
  33.     /*  
  34.      * Generated Methods  
  35.      */   
  36.   
  37.     /**  
  38.      * Method execute  
  39.      *   
  40.      * @param mapping  
  41.      * @param form  
  42.      * @param request  
  43.      * @param response  
  44.      * @return ActionForward  
  45.      */   
  46.     public  ActionForward execute(ActionMapping mapping, ActionForm form,  
  47.             HttpServletRequest request, HttpServletResponse response) {  
  48.         if  (form  instanceof  UpLoadImgForm) { // 如果form是uploadsForm   
  49.             String encoding = request.getCharacterEncoding();  
  50.             if  ((encoding !=  null ) && (encoding.equalsIgnoreCase( "utf-8" ))) {  
  51.                 response.setContentType("text/html; charset=gb2312" ); // 如果没有指定编码,编码格式为gb2312   
  52.             }  
  53.             UpLoadImgForm upLoad = (UpLoadImgForm) form;  
  54.             FormFile formFile = upLoad.getFile();  
  55.             try  {  
  56.                 InputStream stream = formFile.getInputStream();  
  57.                 String realPath = request.getRealPath("/" + "upload" );  
  58.                 ByteArrayOutputStream baos = new  ByteArrayOutputStream();  
  59.                 OutputStream bao = new  FileOutputStream(realPath +  "/"   
  60.                         + formFile.getFileName());  
  61.                 int  bytesRead =  0 ;  
  62.                 byte [] buffer =  new   byte [ 8192 ];  
  63.                 while  ((bytesRead = stream.read(buffer,  0 8192 )) != - 1 ) {  
  64.                     bao.write(buffer, 0 , bytesRead);  
  65.                 }  
  66.                 bao.flush();  
  67.                 bao.close();  
  68.                 stream.close();  
  69.             } catch  (Exception e) {  
  70.                 System.out.println(e);  
  71.             }  
  72.             return  mapping.findForward( "success" );  
  73.         }  
  74.   
  75.         return  mapping.findForward( "error" );  
  76.     }  
  77. }  
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.jqqd.struts.action;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.jqqd.struts.formAction.UpLoadImgForm;

/**
 * MyEclipse Struts Creation date: 06-06-2008
 * 
 * XDoclet definition:
 * 
 * @struts.action path="/upLoadImg" name="upLoadImg" input="/uploadImg.jsp"
 *                scope="request" validate="true"
 * @struts.action-forward name="error" path="/error.jsp"
 * @struts.action-forward name="success" path="/success.jsp"
 */
public class UpLoadImgAction extends Action {
	/*
	 * Generated Methods
	 */

	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if (form instanceof UpLoadImgForm) {// 如果form是uploadsForm
			String encoding = request.getCharacterEncoding();
			if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
				response.setContentType("text/html; charset=gb2312");// 如果没有指定编码,编码格式为gb2312
			}
			UpLoadImgForm upLoad = (UpLoadImgForm) form;
			FormFile formFile = upLoad.getFile();
			try {
				InputStream stream = formFile.getInputStream();
				String realPath = request.getRealPath("/"+"upload");
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				OutputStream bao = new FileOutputStream(realPath + "/"
						+ formFile.getFileName());
				int bytesRead = 0;
				byte[] buffer = new byte[8192];
				while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
					bao.write(buffer, 0, bytesRead);
				}
				bao.flush();
				bao.close();
				stream.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			return mapping.findForward("success");
		}

		return mapping.findForward("error");
	}
}

 

struts-config.xml代码:

Xml代码 复制代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">   
  3.   
  4. < struts-config >   
  5.   < data-sources   />   
  6.   < form-beans   >   
  7.         < form-bean   name = "upLoadImg"   type = "com.jqqd.struts.formAction.UpLoadImgForm"   />   
  8.          
  9.   </ form-beans >   
  10.   
  11.   < global-exceptions   />   
  12.   < global-forwards   />   
  13.   < action-mappings   >   
  14.         < action   
  15.       attribute = "upLoadImg"   
  16.       validate = "false"   
  17.       name = "upLoadImg"   
  18.       path = "/upLoadImg"   
  19.       scope = "request"   
  20.       type = "com.jqqd.struts.action.UpLoadImgAction" >   
  21.       < forward   name = "error"   path = "/error.jsp"   />   
  22.       < forward   name = "success"   path = "/success.jsp"   />   
  23.     </ action >   
  24.       
  25.       
  26.   </ action-mappings >   
  27.   
  28.   < message-resources   parameter = "com.jqqd.struts.ApplicationResources"   />   
  29. </ struts-config >   
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
        <form-bean name="upLoadImg" type="com.jqqd.struts.formAction.UpLoadImgForm" />
       
  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
        <action
      attribute="upLoadImg"
      validate="false"
      name="upLoadImg"
      path="/upLoadImg"
      scope="request"
      type="com.jqqd.struts.action.UpLoadImgAction">
      <forward name="error" path="/error.jsp" />
      <forward name="success" path="/success.jsp" />
    </action>
    
    
  </action-mappings>

  <message-resources parameter="com.jqqd.struts.ApplicationResources" />
</struts-config>

 uploadImg.jsp文件的代码:

Html代码 复制代码
  1. < %@ page  language = "java"   pageEncoding = "GB2312" % >   
  2. < %@ taglib  uri = "http://struts.apache.org/tags-html"   prefix = "html"  % >   
  3. < html >   
  4.   < head >   
  5.     < title > uploadImg.do </ title >   
  6.     < link   type = "text/css"   rel = "stylesheet"   href = "css/upload.css"   />   
  7.   </ head >   
  8.   < body >   
  9.   < html:form   action = "upLoadImg.do"   enctype = "multipart/form-data" >   
  10.   < div   id = "uploadD" >   
  11.   < div   id = "uploadTitD" > 图片上传 </ div >   
  12.   < div   id = "uploadConD" >   
  13.   < html:file   property = "file" > </ html:file > < br /> < br />   
  14.   < html:submit > </ html:submit > < html:reset > </ html:reset > </ div >   
  15.   </ div >   
  16.  </ html:form >     
  17.   </ body >   
  18.     
  19. </ html >   
<%@ page language="java" pageEncoding="GB2312"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html>
  <head>
    <title>uploadImg.do</title>
    <link type="text/css" rel="stylesheet" href="../../css/upload.css" />
  </head>
  <body>
  <html:form action="upLoadImg.do" enctype="multipart/form-data">
  <div id="uploadD">
  <div id="uploadTitD">图片上传</div>
  <div id="uploadConD">
  <html:file property="file"></html:file><br/><br/>
  <html:submit></html:submit><html:reset></html:reset></div>
  </div>
 </html:form>  
  </body>
  
</html>

 

 base.css代码:

Html代码 复制代码
  1. html,body{margin:0px;border:0px; background-color:#333333; text-align:center; font-size:12px; color:#FFFFFF; }  
  2. body img,body div{border:0px; margin-left:auto; margin-right:auto;}  
html,body{margin:0px;border:0px; background-color:#333333; text-align:center; font-size:12px; color:#FFFFFF; }
body img,body div{border:0px; margin-left:auto; margin-right:auto;}

 upload.css代码:

 

Html代码 复制代码
  1. @import url(base.css);  
  2. #uploadD{width:600px; height:500px; border:1px solid #FFFFFF; margin-top:50px;}  
  3. #uploadTitD,#uploadConD{width:600px; height:30px; border:0px; background-color:#999999; line-height:2.5em; height:2.5em;}  
  4. #uploadConD{background-color:#666666;} 

 

---------------------------------------------------------------------------------

学习工作累了,休闲娱乐一下 ~~~

天迹网游世界: http://tjwl.jimdo.com/

 

我们的淘客网开通啦,欢迎大家去逛逛 ~\( ≧▽≦ )/~ 啦啦啦 ~~~
淘宝购物资讯网: http://taoke178.jimdo.com

淘乐园: http://taoleyuan.jimdo.com

 

分享到:
评论

相关推荐

    struts2文件上传教程

    struts2文件上传教程

    Struts2文件上传和下载教程

    Struts2文件上传和下载教程

    struts2 文件上传 教程

    struts2文件上传-基本

    struts构建文件上传教程

    struts构建文件上传,如果有这方面需求的朋友,希望能给你一点帮助.

    Struts1.x的上传文件示例

    Struts1.x的上传文件示例。Struts1.x的教程参考我的Blog:http://blog.csdn.net/boyazuo

    struts2学习教程

    struts2学习教程包括:第一个Struts2程序,处理一个form多个submit,struts.xml常用配置解析,使用validate方法验证数据,使用Validation框架验证数据,在Action类中获得HttpServletResponse对象的四种方法,上传...

    struts2入门教程

    很好的struts2入门教程,网上搜集整理的! 内容: ... ·Struts 2中实现文件上传 ·Struts 2中的OGNL ·Strus 2的新表单标签的使用 ·Struts 2与AJAX ·Struts2分页 ·完全Struts's Tiles入门

    struts2.0中文教程

    01 为Struts 2.0做好准备 02 常用的Struts 2.0的标志(Tag) 03 Struts 2.0的Action讲解...09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新表单标志的使用 13 Struts 2与AJAX

    Struts2入门教程(全新完整版)

    十二、总结 本教程对struts2的基本知识进行了一些说明,关于struts2的更多详细内容应参看struts2的官方文档及提供的app实例。 下面对struts2的基本执行流程作一简要说明,此流程说明可以结合官方提供的struts2结构图...

    struts2的文件上传

    struts2的文件上传教程,讲的挺好的,值得下载

    浪曦Struts2系列视频教程

    教程名称:浪曦Struts2系列视频教程课程目录:【】1 Struts2入门与配置【】10 Struts2的核心 拦截器【】11 Struts2的核心 拦截器续【】12 Struts2的文件上传和下载【】13 Struts2的文件上传和下载续【】14 Struts2的...

    文件上传struts2相关经典教程

    struts2相关经典教程 fileupload struts2

    struts+spring+hibernate(mysql)用户登录及文件上传

    博文链接:https://zzwwyf.iteye.com/blog/231491

    个人认为目前最完备的Struts2教程

    01 为Struts 2.0做好准备 02 常用的Struts 2.0的标志(Tag) 03 Struts 2.0的Action...09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 Struts 2的新表单标志的使用 13 Struts 2与AJAX

    struts2简单教程

    itcast的struts2简单教程 包括:环境搭建、国际化、OGNL、标签、拦截器、文件上传等

    Struts2中文教程

    Struts2中文教程,包括:标志,Action讲解,国际化,拦截器,文件上传等等,内容非常详细!

    Struts2.0中文教程权威版

    09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新表单标志的使用 13 Struts 2与AJAX Struts2中用Spring实现IoC.doc Struts2中的零配置与CoC(Convention over Configration...

    Struts2学习手册之文件上传基础教程

    Struts2提供的文件上传下载机制十分简便,使得我们写很少的代码,下面这篇文章主要给大家介绍了关于Struts2学习手册之文件上传的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

    struts中文教程及实例资源

    包含: Struts2.0中文教程 struts2权威指南 struts架构的指导 struts框架详细介绍 struts整体架构及核心标签库 validator页面验证 用struts架构实现文件上传任务

Global site tag (gtag.js) - Google Analytics