首页 > 开发 > 综合 > 正文

用Struts上传多个文件的方法

2024-07-21 02:15:14
字体:
来源:转载
供稿:网友

    最近在做struts项目时遇到了上传多个文件的问题。在网上查了不少资料,也没有找到用struts上传多个文件的例子。我经过几天的研究,实现了用struts上传多个文件的功能。现在贴出来让大家共享!

    一。建立actionform

package com.cnehu.struts.form;
import javax.servlet.http.httpservletrequest;

import org.apache.struts.action.actionerror;
import org.apache.struts.action.actionerrors;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionmapping;
import org.apache.struts.upload.formfile;
import org.apache.struts.upload.multipartrequesthandler;

/**
 * <p>
 * title:uploadform
 * </p>
 * <p>
 * copyright: copyright (c) 2005 techyang http://blog.csdn.net/techyang
 * </p>
 * @author techyang
 * @version 1.0
 */

public class uploadform extends actionform
{
    public static final string error_property_max_length_exceeded = "org.apache.struts.webapp.upload.maxlengthexceeded";

    protected formfile thefile;
    protected formfile thefile2;
    public formfile getthefile()
    {
        return thefile;
    }

    public void setthefile(formfile thefile)
    {
        this.thefile = thefile;
    }

    public actionerrors validate(actionmapping mapping,
            httpservletrequest request)
    {
        actionerrors errors = null;
        //has the maximum length been exceeded?
        boolean maxlengthexceeded = (boolean) request
                .getattribute(multipartrequesthandler.attribute_max_length_exceeded);
        if ((maxlengthexceeded != null) && (maxlengthexceeded.booleanvalue()))
        {
            errors = new actionerrors();
            errors.add(error_property_max_length_exceeded, new actionerror(
                    "maxlengthexceeded"));
        }
        return errors;

    }
    /**
     * @return returns the thefile2.
     */
    public formfile getthefile2()
    {
        return thefile2;
    }
    /**
     * @param thefile2 the thefile2 to set.
     */
    public void setthefile2(formfile thefile2)
    {
        this.thefile2 = thefile2;
    }
}


二。建立actionservlet

package com.cnehu.struts.action;

import java.io.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.formfile;

import com.cnehu.struts.form.uploadform;

/**
 * <p>
 * title:uploadaction
 * </p>
 * <p>
 * copyright: copyright (c) 2005 techyang http://blog.csdn.net/techyang
 * </p>
 * @author techyang
 * @version 1.0
 */

public class uploadaction extends action
{
    public actionforward execute(actionmapping mapping, actionform form,
            httpservletrequest request, httpservletresponse response)
            throws exception
    {
            string encoding = request.getcharacterencoding();
            if ((encoding != null) && (encoding.equalsignorecase("utf-8")))
            {
                response.setcontenttype("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312
            }
            uploadform theform = (uploadform) form;
            formfile file = theform.getthefile();//取得上传的文件
         
            formfile file2=theform.getthefile2();
            try
            {              
                /*
                 * 取当前系统路径d:/tomcat5/webapps/coka/ 其中coka 为当前context
                 */
                string filepath = this.getservlet().getservletcontext()
                .getrealpath("/");
                inputstream stream = file.getinputstream();//把文件读入
              
                bytearrayoutputstream baos = new bytearrayoutputstream();
              
                /*
                 * 建立一个上传文件的输出流 如果是linux系统请把uploadfiles后的"//"换成"/"
                 */
                outputstream bos = new fileoutputstream(filepath +
                        "uploadfiles//"+file.getfilename());
               
                //d:/tomcat5/webapps/coka/uploadfiles/dsc01508.jpg
              /*  system.out.println(filepath +
                        "uploadfiles//"+file.getfilename());
                system.out.println(filepath);*/
                request.setattribute("filename",filepath + "/"
                        + file.getfilename());
                int bytesread = 0;
                byte[] buffer = new byte[8192];
                while ((bytesread = stream.read(buffer, 0, 8192)) != -1)
                {
                    bos.write(buffer, 0, bytesread);//将文件写入服务器
                }          
                bos.close();
                stream.close();
               
                inputstream stream2 = file2.getinputstream();//把文件读入
                bytearrayoutputstream baos2 = new bytearrayoutputstream();
                outputstream bos2 =  new fileoutputstream(filepath +
                        "uploadfiles//"+file2.getfilename());//建立一个上传文件的输出流
                int bytesread2 = 0;
                byte[] buffer2 = new byte[8192];
                int i=0;
                while ((bytesread2 = stream2.read(buffer2, 0, 8192)) != -1)
                {
                    bos2.write(buffer2, 0, bytesread2);//将文件写入服务器
                }          
                bos2.close();
                stream2.close();
               
            } catch (exception e)
            {
                system.err.print(e);
            }
            return mapping.findforward("display");
         
    }
}


三。建立上传用的jsp文件 upload.jsp

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<html:html>
<head>
<title>用struts上传文件</title>
</head>
<body>
<html:form action="/uploadsaction" enctype="multipart/form-data">
<html:file property="thefile"/>
<html:file property="thefile2"/>

<html:submit/>
</html:form>
</body>
</html:html>

四。配置struts-config.xml文件

<?xml version="1.0" encoding="utf-8"?>
<!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.1//en" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
   <data-sources />
   <form-beans >
     <form-bean name="uploadsform" type="com.cnehu.struts.form.uploadform" />
   </form-beans>

   <global-exceptions />
   <global-forwards >
    
   </global-forwards>

   <action-mappings >
            
      <action name="uploadsform" type="com.cnehu.struts.action.uploadaction" path="/uploadsaction">
 <forward name="display" path="/display.jsp" />
 </action>
    </action-mappings>
         
</struts-config>

全文完!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表