struts2单文件上传

1.配置web.xml(上一篇有)

2.配置struts.xml

Java代码
  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.i18n.encoding" value="UTF-8"></constant>  
  7.         <constant name="struts.configuration.xml.reload" value="true" />  
  8.         <constant name="struts.devMode" value="false" />  
  9.         <include file="com/struts/config/file.xml"></include>  
  10.     </struts>  

 3. FileAction  类

Java代码
  1. package com.file.action;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5.   
  6. import org.apache.commons.io.FileUtils;  
  7.   
  8. import com.opensymphony.xwork2.ActionSupport;  
  9.   
  10. public class FileAction extends ActionSupport{  
  11.   
  12.     private File uploadFile;  
  13.     private String uploadFileContentType;  
  14.     private String uploadFileFileName;  
  15.       
  16.     public String file(){  
  17.         System.out.println("11111111111111111111");  
  18.         String realPath = "e:\\现计/piture";  
  19.         File file  = new File(realPath);  
  20.         if(!file.exists()){  
  21.             file.mkdirs();  
  22.         }  
  23.         try {  
  24.             FileUtils.copyFile(uploadFile, new File(file, uploadFileFileName));  
  25.         } catch (IOException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();   
  28.             return "input";  
  29.         }  
  30.         return "success";  
  31.     }  
  32.   
  33.     /**********get 和 set方法************/  
  34.     public File getUploadFile() {  
  35.         return uploadFile;  
  36.     }  
  37.   
  38.     public void setUploadFile(File uploadFile) {  
  39.         this.uploadFile = uploadFile;  
  40.     }  
  41.   
  42.     public String getUploadFileContentType() {  
  43.         return uploadFileContentType;  
  44.     }  
  45.   
  46.     public void setUploadFileContentType(String uploadFileContentType) {  
  47.         this.uploadFileContentType = uploadFileContentType;  
  48.     }  
  49.   
  50.     public String getUploadFileFileName() {  
  51.         return uploadFileFileName;  
  52.     }  
  53.   
  54.     public void setUploadFileFileName(String uploadFileFileName) {  
  55.         this.uploadFileFileName = uploadFileFileName;  
  56.     }  
  57. }  

 4.配置 file.xml

Java代码
  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-multipart.maxSize" value="16777216"></constant>  
  7.   
  8.         <package name="testo" extends="struts-default">  
  9.             <action name="f_*" class="com.file.action.FileAction" method="{1}">  
  10.                 <result   name="success">/success.jsp</result>  
  11.                 <result name="input">/default.jsp</result>  
  12.             </action>  
  13.         </package>  
  14.     </struts>  

 5.jsp页面访问

Java代码
  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>文件上传</title>  
  8. </head>  
  9. <body>  
  10. 文件上传  
  11. <form action="f_file.action" method="post" enctype="multipart/form-data">  
  12.     <input type="file" name="uploadFile">  
  13.     <input  type="submit" value="上传">  
  14. </form>  
  15. </body>  
  16. </html> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值