阅读教程

struts上传文件 教程

[日期:2008-06-07] 来源:  作者:志伟

struts 上传,struts2上传教程,struts2.0上传.


UpLoadImgForm.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. }  

 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, 08192)) != -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. }  

 

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>  

 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>  

 

 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;}  

 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;} 


      
      使用百度搜索:struts上传文件 教程百度中搜索:struts上传文件 教程
阅读:
录入:志伟

评论 】 【 推荐 】 【 打印
上一篇:Struts框架,构建Model组件
下一篇:Struts Spring 集成示例[推荐]
本文评论       全部评论
发表评论


点评: 字数
姓名:

 
搜一下


 
本周热门教程
 

关于我们 | 广告合作 | 法律声明 | 联系站长 | 网站地图 | 网站搜索 | | Top ↑
Copyright © 志伟教程资料网 Powered by zhiweinet 1.0
 本栏目提供:struts上传文件 教程