Java Struts2文件上传实例

Java Struts2文件上传实例

 

项目结构:

src
      struts.xml
   com
       hellostruts2
           action
                  UploadFileAction.java
                   
WebContent
    upload
           error.jsp
           success.jsp
           upload.jsp
           
    WEB-INF
           web.xml
        lib
                commons-fileupload-1.3.1.jar
                commons-io-2.2.jar
                commons-lang-2.4.jar
                commons-lang3-3.2.jar
                commons-logging-1.1.3.jar
                freemarker-2.3.22.jar
                javassist-3.11.0.GA.jar
                ognl-3.0.14.jar
                struts2-core-2.3.28.1.jar
                xwork-core-2.3.28.1.jar

web.xml

<?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/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id="WebApp_ID" version="3.0">
   
   <display-name>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   <filter>
      <filter-name>struts2CleanupFilter</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
   </filter>
   <filter>
      <filter-name>struts2Filter</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
   </filter>

    <filter-mapping>
      <filter-name>struts2CleanupFilter</filter-name>
      <url-pattern>/*</url-pattern>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>FORWARD</dispatcher>
   </filter-mapping>
   <filter-mapping>
      <filter-name>struts2Filter</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

 

struts.xml

<?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.devMode" value="true" />

    <constant name="struts.multipart.maxSize" value="1000000" />
    
    
    <package name="uploadstruts" extends="struts-default">
        <action name="upload" class="com.hellostruts2.action.UploadFileAction" method="execute">
            <result name="success">/upload/success.jsp</result>
            <result name="error">/upload/error.jsp</result>
        </action>
    </package>
    
</struts>

 

UploadFileAction.Java

package com.hellostruts2.action;

import java.io.*;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class UploadFileAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    private File myFile;
    private String myFileContentType;
    private String myFileFileName;
    private String destPath;
    
    public String execute(){
        /* Copy file to a safe location */
        destPath = "C:/apache-tomcat-8.0.23/work/";
        try{
            System.out.println("Src File name: " + myFile);
            System.out.println("Dst File name: " + myFileFileName);
                  
            File destFile  = new File(destPath, myFileFileName);
            FileUtils.copyFile(myFile, destFile);
  
        }catch(IOException e){
            e.printStackTrace();
            return ERROR;
      }
      return SUCCESS;
   }
    
    public File getMyFile() {
        return myFile;
    }
    public void setMyFile(File myFile) {
        this.myFile = myFile;
    }
    public String getMyFileContentType() {
        return myFileContentType;
    }
    public void setMyFileContentType(String myFileContentType) {
        this.myFileContentType = myFileContentType;
    }
    public String getMyFileFileName() {
        return myFileFileName;
    }
    public void setMyFileFileName(String myFileFileName) {
        this.myFileFileName = myFileFileName;
    }
}
View Code

 

upload.jsp

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 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=ISO-8859-1">
 8 <title>File Upload</title>
 9 </head>
10 <body>
11     <form action="upload" method="post" enctype="multipart/form-data">
12          <label for="myFile">Upload your file</label>
13          <input type="file" name="myFile" />
14          <input type="submit" value="Upload"/>
15       </form>
16 </body>
17 </html>
View Code

success.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>File Upload Success</title>
 9 </head>
10 <body>
11     You have successfully uploaded <s:property value="myFileFileName"/>
12 </body>
13 </html>
View Code

error.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>File Upload Error</title>
 9 </head>
10 <body>
11     There has been an error in uploading the file.
12 </body>
13 </html>
View Code

 

posted @ 2016-04-29 15:09  undefined?  阅读(365)  评论(0编辑  收藏  举报