天天看点

struts实现upload文件上传(步骤很清楚,一目了然)

1、导入struts包

struts实现upload文件上传(步骤很清楚,一目了然)

2、配置web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3. xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5. id="WebApp_ID" version="2.5">  
  6. <display-name>NewJava</display-name>  
  7. <filter>  
  8.   <filter-name>struts2</filter-name>  
  9.   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  10. </filter>  
  11. <filter-mapping>  
  12.   <filter-name>struts2</filter-name>  
  13.   <url-pattern>/*</url-pattern>  
  14. </filter-mapping>  
  15. <welcome-file-list>  
  16.   <welcome-file>index.jsp</welcome-file>  
  17. </welcome-file-list>  
  18. </web-app>  

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">

<display-name>NewJava</display-name>

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

3、上传JSP页面

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3.     <%@ taglib prefix="s" uri="/struts-tags" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>文件上传</title>  
  9. </head>  
  10. <body>  
  11. <div align="center">  
  12. <br/><br/>  
  13. <img alt="文件上传" src="images/upload.png">  
  14. <s:form method="post" action="Upload" theme="simple" enctype="multipart/form-data">   
  15. 文件名称:<s:textfield name="title"></s:textfield>  
  16. 文件地址:<s:file name="upload"></s:file>  
  17. <s:submit value="上传"></s:submit>  
  18. </s:form>  
  19. </div>  
  20. </body>  
  21. </html>  

4、编写Action

  1. package com.action;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import org.apache.struts2.ServletActionContext;  
  7. import com.opensymphony.xwork2.ActionSupport;  
  8. public class UploadAction extends ActionSupport {  
  9. private String title;  
  10. private File upload;  
  11. private String uploadContentType;  
  12. // 封装上传文件名的属性   
  13. private String uploadFileName;  
  14. // 直接在struts.xml文件中配置的属性   
  15. public String execute() throws Exception {  
  16.   //以服务器的文件保存地址和原文件名建立上传文件输出流    
  17. String path = ServletActionContext.getServletContext().getRealPath ("/load/");//获取服务器存放物理路径  
  18. System.out.println(path+ getUploadFileName());  
  19. FileOutputStream fos = new FileOutputStream(path+"\\"+ getUploadFileName());  
  20. FileInputStream fis = new FileInputStream(getUpload());   
  21. byte[] buffer = new byte[1024];   
  22. int len = 0;   
  23. while ((len = fis.read(buffer)) > 0)    
  24. {  fos.write(buffer , 0 , len);    
  25. }   
  26. System.out.println("上传成功");  
  27. System.out.println(path+"\\"+ getUploadFileName());  
  28.   return SUCCESS;  
  29. }  
  30. public String getTitle() {  
  31.   return title;  
  32. }  
  33. public void setTitle(String title) {  
  34.   this.title = title;  
  35. }  
  36. public File getUpload() {  
  37.   return upload;  
  38. }  
  39. public void setUpload(File upload) {  
  40.   this.upload = upload;  
  41. }  
  42. public String getUploadContentType() {  
  43.   return uploadContentType;  
  44. }  
  45. public void setUploadContentType(String uploadContentType) {  
  46.   this.uploadContentType = uploadContentType;  
  47. }  
  48. public String getUploadFileName() {  
  49.   return uploadFileName;  
  50. }  
  51. public void setUploadFileName(String uploadFileName) {  
  52.   this.uploadFileName = uploadFileName;  
  53. }  
  54. }  

package com.action;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {

private String title;

private File upload;

private String uploadContentType;

// 封装上传文件名的属性

private String uploadFileName;

// 直接在struts.xml文件中配置的属性

public String execute() throws Exception {

//以服务器的文件保存地址和原文件名建立上传文件输出流

String path = ServletActionContext.getServletContext().getRealPath ("/load/");//获取服务器存放物理路径

System.out.println(path+ getUploadFileName());

FileOutputStream fos = new FileOutputStream(path+"\\"+ getUploadFileName());

FileInputStream fis = new FileInputStream(getUpload());

byte[] buffer = new byte[1024];

int len = 0;

while ((len = fis.read(buffer)) > 0)

{ fos.write(buffer , 0 , len);

}

System.out.println("上传成功");

System.out.println(path+"\\"+ getUploadFileName());

return SUCCESS;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public File getUpload() {

return upload;

}

public void setUpload(File upload) {

this.upload = upload;

}

public String getUploadContentType() {

return uploadContentType;

}

public void setUploadContentType(String uploadContentType) {

this.uploadContentType = uploadContentType;

}

public String getUploadFileName() {

return uploadFileName;

}

public void setUploadFileName(String uploadFileName) {

this.uploadFileName = uploadFileName;

}

}

5、配置struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.      "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6. <constant name="struts.enable.DynamicMethodInvocation" value="true" />  
  7. <constant name="struts.devMode" value="true" />  
  8. <package name="example" extends="struts-default">  
  9.   <action name="Upload" class="com.action.UploadAction">  
  10.    <result >/download.jsp</result>  
  11.   </action>  
  12. </package>  
  13. </struts>  

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="true" />

<constant name="struts.devMode" value="true" />

<package name="example" extends="struts-default">

<action name="Upload" class="com.action.UploadAction">

<result >/download.jsp</result>

</action>

</package>

</struts>

6、看看目录结构 运行吧  文件上传到load文件夹下面  可能有点慢

struts实现upload文件上传(步骤很清楚,一目了然)