国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

用Struts上傳多個(gè)文件的方法

2024-07-21 02:15:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

    最近在做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>

全文完!

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 双桥区| 建德市| 雷山县| 昌都县| 黄冈市| 霸州市| 平武县| 台北县| 临江市| 德昌县| 台江县| 榕江县| 赤城县| 灵寿县| 修文县| 兴海县| 遵义县| 淮南市| 兴和县| 中宁县| 清徐县| 红原县| 闻喜县| 含山县| 阳高县| 文安县| 全椒县| 内乡县| 洪雅县| 陆川县| 夏邑县| 永吉县| 额济纳旗| 勃利县| 旅游| 玛多县| 红桥区| 上犹县| 徐闻县| 丁青县| 宁强县|