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

1、导入struts包

 

 

 

 

 

 

 

 

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>  


 

 

 

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


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.   
  6. <struts>  
  7. <constant name="struts.enable.DynamicMethodInvocation" value="true" />  
  8. <constant name="struts.devMode" value="true" />  
  9.   
  10. <package name="example" extends="struts-default">  
  11.   
  12.   <action name="Upload" class="com.action.UploadAction">  
  13.    <result >/download.jsp</result>  
  14.   </action>  
  15. </package>  
  16. </struts>  


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值