struts2文件上传

struts.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <include file="cn/itcast/upload/struts_upload.xml"></include>
</struts>


struts_upload.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="upload" namespace="/upload" extends="struts-default">
        <action name="uploadAction_saveFile" class="cn.itcast.upload.UploadAction" method="saveFile">
            <result name="success">/upload/success.jsp</result>
        </action>
    </package>
</struts>


UploadAction.java:

package cn.itcast.upload;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
    /*
     * 上传文件存储的临时文件
     * D:\\Study\\java\\apache-tomcat-7.0.55\\work\\Catalina\\localhost\\itcast1105_struts2\\upload_641bb224_5ae0_4d9c_8d69_bb3b85b8a098_00000000.tmp
     */
    private File uploadImage;
    /*
     * 上传文件的真实名称
     */
    private String uploadImageContentType;
    private String uploadImageFileName;
    public File getUploadImage() {
        return uploadImage;
    }
    public void setUploadImage(File uploadImage) {
        this.uploadImage = uploadImage;
    }
    public String getUploadImageContentType() {
        return uploadImageContentType;
    }
    public void setUploadImageContentType(String uploadImageContentType) {
        this.uploadImageContentType = uploadImageContentType;
    }
    public String getUploadImageFileName() {
        return uploadImageFileName;
    }
    public void setUploadImageFileName(String uploadImageFileName) {
        this.uploadImageFileName = uploadImageFileName;
    }
    
    public String saveFile() {
        System.out.println("UploadAction ***** savdfile()");
        ServletContext sc = ServletActionContext.getServletContext();
        String path=sc.getRealPath("/fileupload");
        File file = new File(path,uploadImageFileName);
        try {
            FileUtils.copyFile(uploadImage, file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        uploadImage.delete(); //清空缓存   
        return "success";
    }
}

upload.jsp:

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags"   prefix="s"%>
<html>
  <head>
    <title>My JSP ‘index.jsp‘ starting page</title>
    </head>
  <body>
    <form action="${pageContext.request.contextPath}/upload/uploadAction_saveFile.action"  
          name="form1"  method="post"  enctype="multipart/form-data" >
             
             上传文件名称:<input type="file" name="uploadImage">
           <input type="submit" value="上传">
    </form>
  </body>
</html>

在当前项目下就会生成上传的文件

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。