Stsuts2实现文件的上传功能

Struts2实现文件上传功能需要的两个jar包,commons-fileupload 1.2.1和commons-io 1.4

Stsuts2实现文件的上传功能

手动添加两个jar包。


实现文件上传功能的Action类如下

package com.lyb.CommonsFileUpload.upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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;
    // 接受依赖注入的属性
    private String savePath;
    // 接受依赖注入的方法
    public void setSavePath(String value) {
        this.savePath = value;
    }
    private String getSavePath() throws Exception {
        return ServletActionContext.getRequest().getRealPath(savePath);
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public void setUpload(File upload) {
        this.upload = upload;
    }
    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }
    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }
    public String getTitle() {
        return (this.title);
    }
    public File getUpload() {
        return (this.upload);
    }
    public String getUploadContentType() {
        return (this.uploadContentType);
    }
    public String getUploadFileName() {
        return (this.uploadFileName);
    }
    @Override
    public String execute() throws Exception {
        // 以服务器的文件保存地址和原文件名建立上传文件输出流
        FileOutputStream fos = new FileOutputStream(getSavePath() + "//"+ 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);
        }
        
        //关闭输入输出流
        fis.close();
        
        fos.close();
        
        return SUCCESS;
    }
}

Sturts.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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <include file="example.xml"/>

    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>
 -->
    <!-- Add packages here -->
   <package name="upload" extends="struts-default">
        <action name="upload" class="com.lyb.CommonsFileUpload.upload.UploadAction">
            <interceptor-ref name="fileUpload"> 
              <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
                <param name="maximumSize">1024*1024</param> 
            </interceptor-ref> 
            <interceptor-ref name="defaultStack"/>   
            <!-- 保存路径savePath依赖注入 -->         
            <param name="savePath">/upload</param>
            <result name="input"> /upload.jsp</result> 
            <result>/success.jsp</result>  
        </action>
    </package> 
</struts>

上传测试页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>上传页面</title>
    
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">    
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
  
  <body>
   <div style="color.red">
   <s:fielderror></s:fielderror>
   </div>
   <center>
    <form action="upload.action" method="post" enctype="multipart/form-data"> 
     文件标题:<input type="text" name="title"><br />
     选择文件:<input type="file" name="upload"><br />
     <input type="submit" value="上传">
    </form>
   </center>
  </body>
</html>

上传成功页面。这里添加了图片标签,直观的显示上传的文件内容(图片),在上传目录中也可以找到该文件

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.jsp' starting page</title>
    
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">    
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
  
  <body>
     文件名:<s:property value="title"/><br />
     文件为:<img src="<s:property value="'upload/' + uploadFileName"/>"/><br> 
  </body>
</html>

调试结果如下:

Stsuts2实现文件的上传功能



Stsuts2实现文件的上传功能

你可能感兴趣的:(文件上传,struts2,动态配置)