`
lsong89
  • 浏览: 40839 次
  • 性别: 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中实现文件上传。 首先,我们需要在项目的WEB-INF目录下添加两个关键的库文件:`commons-fileupload-1.2.1.jar` 和 `commons-io-1.3.2.jar`。这些文件是Apache Commons项目的一部分,...

    简单易懂的struts2文件上传

    本教程将深入浅出地讲解如何在Struts2中实现文件上传,并提供一个简单的实例来帮助理解。 1. **Struts2文件上传概述** 在Struts2中,文件上传主要依赖于Apache的Commons FileUpload库。这个库提供了处理HTTP多部分...

    struts多文件上传

    在Struts1.2版本中,实现多文件上传是一项常见的需求,它允许用户在一次提交中上传多个文件,这对于数据交互、资源分享等场景非常实用。在本教程中,我们将深入探讨如何在Struts1.2中实现这一功能。 首先,理解多...

    struts2 文件上传 教程

    在这个"Struts2文件上传教程"中,我们将深入探讨如何在Struts2环境中实现文件上传功能。 文件上传在web应用中很常见,例如用户可能需要上传头像、提交文档或分享图片。在Struts2中,这个过程涉及到几个关键组件和...

    Struts2文件上传和下载教程

    为了提高代码的灵活性和可扩展性,推荐使用集合(如List)来封装多个上传文件。这样可以在Action中统一处理所有文件,而不是为每个文件域指定单独的属性。 **2.2 文件过滤** 实现文件过滤通常是为了限制用户上传的...

    Struts2视频教程

    - **文件上传与下载**:Struts2内置了对文件上传的支持,可以轻松实现文件上传功能;同时也可以配置下载功能,满足文件分发的需求。 - **权限控制**:利用拦截器实现细粒度的权限控制,确保只有授权用户才能访问敏感...

    无中文乱码的Struts上传下载(任意文件上传)

    本教程将详细介绍如何在Struts中实现无中文乱码的文件上传与下载,并涵盖任意数量的文件上传。 首先,解决中文乱码问题的关键在于配置正确的字符编码。在Struts的配置文件`struts-config.xml`中,你需要为...

    struts构建文件上传教程

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

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

    而在文件上传功能中,Hibernate可能用于持久化上传文件的相关元数据。通过Hibernate的Session接口,开发者可以执行SQL查询,处理CRUD(创建、读取、更新、删除)操作。 至于文件上传,通常会涉及到Servlet和文件流...

    出血推荐-使用Struts上传文件

    以上代码中,我们获取了上传文件的临时路径,验证了文件大小和类型,然后将其保存到服务器的指定位置。注意,实际操作中还需要处理可能出现的异常,以及进行必要的安全检查。 总结一下,使用 Struts 实现文件上传...

    struts2文件上传

    这样,当你上传文件时,Struts2会自动处理文件的临时存储和验证。 最后,当文件成功上传后,你可以重定向到一个成功页面(如`/success.jsp`),展示上传结果。如果上传失败,用户将被重定向回上传页面(`/upload....

    struts2上传文件

    在Struts2中,文件上传功能是一项常用的功能,允许用户通过Web表单上传文件到服务器。这篇博客文章(遗憾的是,由于没有直接提供具体内容,我无法提供更详细的博客内容摘要),可能详细介绍了如何在Struts2框架中...

    struts2 文件上传

    Struts2 文件上传是Web开发中的一个重要功能,它允许用户通过网页上传文件到服务器。Struts2 是一个基于MVC(Model-View-Controller)设计模式的Java Web框架,提供了丰富的特性和插件来简化这一过程。在本教程中,...

    Struts2文件上传

    在Struts2中,文件上传是一个常见的需求,它允许用户从客户端上传文件到服务器端。本教程将深入讲解如何利用Struts2组件来实现多文件上传的功能。 首先,我们需要了解文件上传的基本原理。在Web应用中,文件上传...

    黑马程序员 struts2.1 视频教程

    16_黑马程序员_struts2.1视频教程_文件上传.rar涵盖了Web开发中常见的文件上传功能。Struts2提供了方便的文件上传支持,包括文件大小限制、文件类型检查等,这一部分将详细解释如何使用Struts2实现文件上传操作。 ...

    Struts2的文件上传下载.rar

    记得设置`enctype="multipart/form-data"`,这是上传文件的必需属性。 - **接收文件**: 在Action类中,创建一个字段来绑定文件域,并使用`@Params`或`@FileUpload`注解进行映射。例如,`private File uploadFile; ...

    struts上传(已解决中文问题)

    在Struts框架中,这可以通过Struts的`&lt;s:file&gt;`标签来创建上传文件的输入元素。 当用户选择文件并提交表单时,Struts框架会使用`Commons FileUpload`库来解析请求中的多部分数据。这个库负责将请求体中的二进制数据...

    struts文件上传例子

    最后,用户成功上传文件后,会被重定向到`/jsp/success.jsp`页面,你可以在该页面上显示上传结果。 这个"struts文件上传例子"是一个基础的实践教程,对于了解Struts框架如何处理文件上传非常有帮助。通过学习和理解...

    struts上传.rar

    总的来说,"struts上传.rar"这个压缩包中包含的是关于如何在Struts框架下实现文件上传的示例或教程,涵盖了从前端表单设计到后端文件处理的整个流程,对于理解和实现Struts的文件上传功能具有指导意义。在实际开发中...

Global site tag (gtag.js) - Google Analytics