struts 上传文件 Dynavalidatorform 实例
一、相关jar包
一个空struts工程的jar包:
另上传文件的两个jar包:
二、页面
1、上传页面upload.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="upload.do" method="post" enctype="multipart/form-data"> 11 <input type="file" name="upload_file" value="" id="upload_file" style="height:20px;"> 12 <input type="submit" id="submitButton" value="提交"> 13 </form> 14 </body> 15 </html>
注:a. form表单要有属性 enctype="multipart/form-data"。
b. 上传文件的选择文件input的name属性name="upload_file" 要与下面的form-bean下form-property的name值一致。
2、struts-config.xml
1 <?xml version="1.0" encoding="utf-8" ?> 2 3 <!DOCTYPE struts-config PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 5 "http://struts.apache.org/dtds/struts-config_1_3.dtd"> 6 <struts-config> 7 <form-beans> 8 <form-bean name="upload" 9 type="org.apache.struts.validator.DynaValidatorForm"> 10 <form-property name="upload_file" type="org.apache.struts.upload.FormFile" /> 11 </form-bean> 12 </form-beans> 13 14 <action-mappings> 15 <action path="/upload" type="com.UploadAction" name="upload" 16 scope="request"> 17 <forward name="sucess" path="/sucess.jsp"></forward> 18 </action> 19 </action-mappings> 20 21 <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> 22 <set-property property="pathnames" 23 value="/org/apache/struts/validator/validator-rules.xml, 24 /WEB-INF/validation.xml" /> 25 </plug-in> 26 27 </struts-config>
注:第8行 form-bean的name属性值要与第15行action的name值一致。
3、UploadAction.java
1 package com; 2 3 import java.io.BufferedOutputStream; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.InputStream; 9 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import org.apache.struts.action.Action; 14 import org.apache.struts.action.ActionForm; 15 import org.apache.struts.action.ActionForward; 16 import org.apache.struts.action.ActionMapping; 17 import org.apache.struts.action.DynaActionForm; 18 import org.apache.struts.upload.FormFile; 19 20 public class UploadAction extends Action { 21 22 public ActionForward execute(ActionMapping mapping, ActionForm form, 23 HttpServletRequest request, HttpServletResponse response) { 24 25 DynaActionForm dynaForm = (DynaActionForm) form; 26 ////这个upload_file对应配置form-property 和jsp页面里选择文件input 27 FormFile file = (FormFile) dynaForm.get("upload_file"); 28 29 String loalPath = null; 30 String filePath = null; 31 if (file != null && !file.toString().trim().equals("")) { 32 loalPath = file.getFileName(); 33 try { 34 InputStream in = file.getInputStream(); 35 int length = 0; 36 String path = this.getServlet().getServletContext().getRealPath("/upload")+ "\\" + loalPath; 37 File outFile = new File(path); 38 if (!outFile.getParentFile().exists()) 39 outFile.getParentFile().mkdirs(); 40 if (outFile.exists()) { 41 outFile.delete(); 42 } 43 BufferedOutputStream out = new BufferedOutputStream( 44 new FileOutputStream(outFile)); 45 byte[] buffer = new byte[4096]; 46 while ((length = in.read(buffer)) != -1) { 47 out.write(buffer, 0, length); 48 } 49 in.close(); 50 out.close(); 51 System.out.println(filePath); 52 } catch (FileNotFoundException e) { 53 // TODO Auto-generated catch block 54 e.printStackTrace(); 55 } catch (IOException e) { 56 // TODO Auto-generated catch block 57 e.printStackTrace(); 58 } 59 } 60 return mapping.findForward("sucess"); 61 } 62 63 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。