struts2文件上传

来源:互联网 发布:百度调价软件 编辑:程序博客网 时间:2024/06/07 01:24

声明:本例是将上传的文件存放到服务器上


一:创建struts.properties文件,用来定义上传文件的总大小

struts.properties代码如下:

struts.multipart.maxSize=62914560


二:上传页面

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@taglib prefix="s" uri="/struts-tags"%><html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">        <title>上传文件</title> <script type="text/javascript">      //判断上传的文件是否为空                function jiaoyan(){                  var file1=document.getElementById("file1").value                var file2=document.getElementById("file2").value                                if(file1=="" && file2==""){                alert("对不起,上传d文件不能为空!");                }else{                document.getElementById("frm").action="<%=path%>/upload.action"; //前面path所得到的路径                document.getElementById("frm").submit();                }            }      </script></head>    <body>    <!-- 上传文件表单定义 -->    <s:form id="frm" id="frm" method="post" enctype="multipart/form-data">        <tr>    <!-- 上传文件标签定义 -->    <td>上传文件:<s:file name="file" id="file1"></s:file></td>    </tr>    <tr>    <td>再次上传文件:<s:file name="file" id="file2"></s:file></td>    </tr>    <tr>    <td align="left"><input type="button" value="提交" onclick="jiaoyan()"></td>    </tr>    </s:form>    </body></html>


三:Action类

package com.onload;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.List;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;//文件上传Actionpublic class photoOnload extends ActionSupport {    //上传文件存放路径    private final static String UPLOADDIR = "/upload";    //上传文件集合    private List<File> file;    //上传文件名集合    private List<String> fileFileName;    //上传文件内容类型集合    private List<String> fileContentType;    public List<File> getFile() {        return file;    }    public void setFile(List<File> file) {        this.file = file;    }    public List<String> getFileFileName() {        return fileFileName;    }    public void setFileFileName(List<String> fileFileName) {        this.fileFileName = fileFileName;    }    public List<String> getFileContentType() {        return fileContentType;    }    public void setFileContentType(List<String> fileContentType) {        this.fileContentType = fileContentType;    }    public String execute() throws Exception {        for (int i = 0; i < file.size(); i++) {            //循环上传每个文件            uploadFile(i);        }        return "success";    }        //执行上传功能    private void uploadFile(int i) throws FileNotFoundException, IOException {        try {            InputStream in = new FileInputStream(file.get(i));//            long len=file.get(i).length();//            System.out.println("len----------------->"+len);            //            int len1=file.size();//得到文件上传的个数//            System.out.println("which------------>"+len1);            //            if(len1==2){//如果上传的文件的个数是两个求两个文件大小的和//            long len2=file.get(0).length();//            long len3=file.get(1).length();//            long sum=len2+len3;//                System.out.println("sum----------------->"+sum);//            }                        String fileName=fileFileName.toString();            int len=fileName.length();                        String substrFilename=fileName.substring(1,len-1);                        System.out.println("fileName------------>"+fileName);            System.out.println("subStrfilename-------------->"+substrFilename);            System.out.println("fileNameLen------------------------>"+len);                        String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR);                        System.out.println("path-------------------->"+dir);            File uploadFile = new File(dir, this.getFileFileName().get(i));                        OutputStream out = new FileOutputStream(uploadFile);                        byte[] buffer = new byte[1024 * 1024];            int length;            while ((length = in.read(buffer)) > 0) {                out.write(buffer, 0, length);            }            in.close();            out.close();        } catch (FileNotFoundException ex) {            ex.printStackTrace();        } catch (IOException ex) {            ex.printStackTrace();        }    }}
四.struts.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts>    <!-- 系统常量定义,定义上传文件字符集编码 -->    <constant name="struts.i18n.encoding" value="gb2312"></constant>    <!-- 系统常量定义,定义上传文件临时存放路径 -->    <constant name="struts.multipart.saveDir" value="f:\wangzihu\"></constant>    <!-- Action所在包定义 -->    <package name="mypackage" extends="struts-default">        <!-- Action名字,类以及导航页面定义 -->        <!-- 通过Action类处理才导航的的Action定义 -->        <action name="upload" class="com.onload.photoOnload">            <result name="input">/index.jsp</result>            <result name="success">/result.jsp</result>        </action>    </package></struts>

五.结果页面

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@taglib prefix="s" uri="/struts-tags"%><html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">        <title>上传结果</title>    </head>    <body>        <!-- 显示上传成功文件名 -->上传文件成功,上传的文件是:        <s:property value="fileFileName" />        <a href="index.jsp">首页</a>    </body></html>



原创粉丝点击