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

本文介绍如何使用Struts2框架实现文件上传功能,包括配置web.xml、struts.xml,编写Action处理类及JSP页面等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、导入struts包

2、配置web.xml

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appxmlns: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. <%@pagelanguage="java"contentType="text/html;charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@taglibprefix="s"uri="/struts-tags"%>
  4. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
  8. <title>文件上传</title>
  9. </head>
  10. <body>
  11. <divalign="center">
  12. <br/><br/>
  13. <imgalt="文件上传"src="images/upload.png">
  14. <s:formmethod="post"action="Upload"theme="simple"enctype="multipart/form-data">
  15. 文件名称:<s:textfieldname="title"></s:textfield>
  16. 文件地址:<s:filename="upload"></s:file>
  17. <s:submitvalue="上传"></s:submit>
  18. </s:form>
  19. </div>
  20. </body>
  21. </html>

4、编写Action

  1. packagecom.action;
  2. importjava.io.File;
  3. importjava.io.FileInputStream;
  4. importjava.io.FileOutputStream;
  5. importjavax.servlet.http.HttpServletRequest;
  6. importorg.apache.struts2.ServletActionContext;
  7. importcom.opensymphony.xwork2.ActionSupport;
  8. publicclassUploadActionextendsActionSupport{
  9. privateStringtitle;
  10. privateFileupload;
  11. privateStringuploadContentType;
  12. //封装上传文件名的属性
  13. privateStringuploadFileName;
  14. //直接在struts.xml文件中配置的属性
  15. publicStringexecute()throwsException{
  16. //以服务器的文件保存地址和原文件名建立上传文件输出流
  17. Stringpath=ServletActionContext.getServletContext().getRealPath("/load/");//获取服务器存放物理路径
  18. System.out.println(path+getUploadFileName());
  19. FileOutputStreamfos=newFileOutputStream(path+"\\"+getUploadFileName());
  20. FileInputStreamfis=newFileInputStream(getUpload());
  21. byte[]buffer=newbyte[1024];
  22. intlen=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. returnSUCCESS;
  29. }
  30. publicStringgetTitle(){
  31. returntitle;
  32. }
  33. publicvoidsetTitle(Stringtitle){
  34. this.title=title;
  35. }
  36. publicFilegetUpload(){
  37. returnupload;
  38. }
  39. publicvoidsetUpload(Fileupload){
  40. this.upload=upload;
  41. }
  42. publicStringgetUploadContentType(){
  43. returnuploadContentType;
  44. }
  45. publicvoidsetUploadContentType(StringuploadContentType){
  46. this.uploadContentType=uploadContentType;
  47. }
  48. publicStringgetUploadFileName(){
  49. returnuploadFileName;
  50. }
  51. publicvoidsetUploadFileName(StringuploadFileName){
  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. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEstrutsPUBLIC
  3. "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <constantname="struts.enable.DynamicMethodInvocation"value="true"/>
  7. <constantname="struts.devMode"value="true"/>
  8. <packagename="example"extends="struts-default">
  9. <actionname="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文件夹下面 可能有点慢

1、导入struts包

2、配置web.xml

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appxmlns: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. <%@pagelanguage="java"contentType="text/html;charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@taglibprefix="s"uri="/struts-tags"%>
  4. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
  8. <title>文件上传</title>
  9. </head>
  10. <body>
  11. <divalign="center">
  12. <br/><br/>
  13. <imgalt="文件上传"src="images/upload.png">
  14. <s:formmethod="post"action="Upload"theme="simple"enctype="multipart/form-data">
  15. 文件名称:<s:textfieldname="title"></s:textfield>
  16. 文件地址:<s:filename="upload"></s:file>
  17. <s:submitvalue="上传"></s:submit>
  18. </s:form>
  19. </div>
  20. </body>
  21. </html>

4、编写Action

  1. packagecom.action;
  2. importjava.io.File;
  3. importjava.io.FileInputStream;
  4. importjava.io.FileOutputStream;
  5. importjavax.servlet.http.HttpServletRequest;
  6. importorg.apache.struts2.ServletActionContext;
  7. importcom.opensymphony.xwork2.ActionSupport;
  8. publicclassUploadActionextendsActionSupport{
  9. privateStringtitle;
  10. privateFileupload;
  11. privateStringuploadContentType;
  12. //封装上传文件名的属性
  13. privateStringuploadFileName;
  14. //直接在struts.xml文件中配置的属性
  15. publicStringexecute()throwsException{
  16. //以服务器的文件保存地址和原文件名建立上传文件输出流
  17. Stringpath=ServletActionContext.getServletContext().getRealPath("/load/");//获取服务器存放物理路径
  18. System.out.println(path+getUploadFileName());
  19. FileOutputStreamfos=newFileOutputStream(path+"\\"+getUploadFileName());
  20. FileInputStreamfis=newFileInputStream(getUpload());
  21. byte[]buffer=newbyte[1024];
  22. intlen=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. returnSUCCESS;
  29. }
  30. publicStringgetTitle(){
  31. returntitle;
  32. }
  33. publicvoidsetTitle(Stringtitle){
  34. this.title=title;
  35. }
  36. publicFilegetUpload(){
  37. returnupload;
  38. }
  39. publicvoidsetUpload(Fileupload){
  40. this.upload=upload;
  41. }
  42. publicStringgetUploadContentType(){
  43. returnuploadContentType;
  44. }
  45. publicvoidsetUploadContentType(StringuploadContentType){
  46. this.uploadContentType=uploadContentType;
  47. }
  48. publicStringgetUploadFileName(){
  49. returnuploadFileName;
  50. }
  51. publicvoidsetUploadFileName(StringuploadFileName){
  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. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEstrutsPUBLIC
  3. "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <constantname="struts.enable.DynamicMethodInvocation"value="true"/>
  7. <constantname="struts.devMode"value="true"/>
  8. <packagename="example"extends="struts-default">
  9. <actionname="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文件夹下面 可能有点慢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值