struts中上传文件的代码(没有限制上传文件类型)

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. }  
/*
 * 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, 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. }  
/*
 * 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;}  
@import url(base.css);
#uploadD{width:600px; height:500px; border:1px solid #FFFFFF; margin-top:50px;}
#uploadTitD,#uploadConD{width:600px; height:30px; border:0px; background-color:#999999; line-height:2.5em; height:2.5em;}
#uploadConD{background-color:#666666;}

 

你可能感兴趣的:(struts,职场,休闲)