最近在做struts項(xiàng)目時(shí)遇到了上傳多個(gè)文件的問(wèn)題。在網(wǎng)上查了不少資料,也沒(méi)有找到用struts上傳多個(gè)文件的例子。我經(jīng)過(guò)幾天的研究,實(shí)現(xiàn)了用struts上傳多個(gè)文件的功能?,F(xiàn)在貼出來(lái)讓大家共享!
一。建立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");//如果沒(méi)有指定編碼,編碼格式為gb2312
            }
            uploadform theform = (uploadform) form;
            formfile file = theform.getthefile();//取得上傳的文件
          
            formfile file2=theform.getthefile2();
            try
            {               
                /*
                 * 取當(dāng)前系統(tǒng)路徑d:/tomcat5/webapps/coka/ 其中coka 為當(dāng)前context
                 */
                string filepath = this.getservlet().getservletcontext()
                .getrealpath("/");
                inputstream stream = file.getinputstream();//把文件讀入
               
                bytearrayoutputstream baos = new bytearrayoutputstream();
               
                /*
                 * 建立一個(gè)上傳文件的輸出流 如果是linux系統(tǒng)請(qǐng)把uploadfiles后的"http://"換成"/"
                 */
                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);//將文件寫(xiě)入服務(wù)器
                }           
                bos.close();
                stream.close();
                
                inputstream stream2 = file2.getinputstream();//把文件讀入
                bytearrayoutputstream baos2 = new bytearrayoutputstream();
                outputstream bos2 =  new fileoutputstream(filepath +
                        "uploadfiles//"+file2.getfilename());//建立一個(gè)上傳文件的輸出流
                int bytesread2 = 0;
                byte[] buffer2 = new byte[8192];
                int i=0;
                while ((bytesread2 = stream2.read(buffer2, 0, 8192)) != -1)
                {
                    bos2.write(buffer2, 0, bytesread2);//將文件寫(xiě)入服務(wù)器
                }           
                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>
全文完!
新聞熱點(diǎn)
疑難解答
圖片精選