struts2文件上传

来源:互联网 发布:sas编程技术教程 编辑:程序博客网 时间:2024/06/07 21:26

一、上传页面:

<%@ page language="java"contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type"content="text/html; charset=GBK" />
  <title>
使用数组上传多个文件</title>
</head>
<body>
<s:fielderror/>
<form action="upload.action" method="post"enctype="multipart/form-data">
  
文件标题:<inputtype="text" name="title" /><br>
  
选择第一个文件:<inputtype="file" name="upload" /><br>
  
选择第二个文件:<inputtype="file" name="upload" /><br>
  
选择第三个文件:<inputtype="file" name="upload" /><br>
<input value="
上传" type="submit" />
</form>
</body>
</html>

                             


二、上传成功页面:

<%@ page language="java"contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>
上传成功</title>
</head>
<body>
上传成功!<br>
  
文件标题:<s:propertyvalue=" + title"/><br>
  
第一个文件为:<imgsrc="<s:property value="'upload/' +uploadFileName[0]"/>"/><br>
  
第二个文件为:<imgsrc="<s:property value="'upload/' +uploadFileName[1]"/>"/><br>
  
第三个文件为:<imgsrc="<s:property value="'upload/' +uploadFileName[2]"/>"/><br>

</body>
</html>



三、动作类

package lee;

import com.opensymphony.xwork2.Action;

importorg.apache.struts2.ServletActionContext;

import java.io.File;

import java.io.*;

import com.opensymphony.xwork2.ActionSupport;

/**

 *@author  yeeku.H.lee kongyeeku@163.com

 *@version  1.0

 *Copyright (C), 2005-2008, yeeku.H.Lee

 *This program is protected by copyright laws.

 *Program Name:

 *Date:

 */

public class UploadAction extendsActionSupport

{

        privateString 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);

    }

       

        publicvoid setTitle(String title) {

               this.title= title;

        }

        publicvoid setUpload(File[] upload) {

               this.upload= upload;

        }

        publicvoid setUploadContentType(String[] uploadContentType) {

               this.uploadContentType= uploadContentType;

        }

        publicvoid setUploadFileName(String[] uploadFileName) {

               this.uploadFileName= uploadFileName;

        }

        publicString getTitle() {

               return(this.title);

        }

        publicFile[] getUpload() {

               return(this.upload);

        }

        publicString[] getUploadContentType() {

               return(this.uploadContentType);

        }

        publicString[] getUploadFileName() {

               return(this.uploadFileName);

        }



       
@Override

   public String execute() throws Exception

        {

               File[]files = getUpload();

               for(int i = 0 ; i < files.length ; i++)

               {

                       //以服务器的文件保存地址和原文件名建立上传文件输出流

                       FileOutputStreamfos = new FileOutputStream(getSavePath() + "\\" +getUploadFileName()[i]);

                       FileInputStreamfis = new FileInputStream(files[i]);

                       byte[]buffer = new byte[1024];

                       int len = 0;

                       while((len = fis.read(buffer)) > 0)

                       {

                               fos.write(buffer, 0 , len);

                       }

               }

        fos.close();// 注意:流应当关闭。

        fis.close();

       return SUCCESS;

    }

}

四、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>

<constantname="struts.custom.i18n.resources"value="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>

<package name="upload"extends="struts-default">

<action name="upload" class="lee.UploadAction">
  <interceptor-ref name="fileUpload">
    <paramname="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/pjpeg</param>
  </interceptor-ref>
  <interceptor-ref name="defaultStack"/>
  <param name="savePath">/upload</param>
  <result name="input">/upload.jsp</result>
  <result>/succ.jsp</result>
</action>

</package>
</struts>

原创粉丝点击