`
guofengniu
  • 浏览: 54533 次
  • 性别: Icon_minigender_1
  • 来自: 哈尔滨
社区版块
存档分类
最新评论

利用Webwork的Inputstream和Outputstream进行文件上传

阅读更多
最近在做一个管理系统,里面涉及到了文件上传;在网上找了好多的方法,不过很多都包含有过期的代码,很是郁闷。
努力了几天,终于用利用Webwork的Inputstream和Outputstream实现了文件的上传
下面具体代码:
1.前台jsp代码:
<%--
  Created by IntelliJ IDEA.
  User: Luckystar_N
  Date: 2007-10-11
  Time: 20:19:31
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/inc/tagLib.jsp" %>
<html>
<head><title>添加授课教案管理---Foreign Language Department</title></head>
<body>
<ww:form id="teachingPlan" method="post" namespace="/admin" action="insertTeachingPlan.action"
         enctype="multipart/form-data"
         validate="true" theme="xhtml">
    <table class="formTable" cellpadding="3" cellspacing="1">
        <tr class="title">
            <td colspan="6"><strong>教案管理</strong></td>
        </tr>
       
              <tr>
            <ww:file
                    label="教案上传(1)"
                    name="teachingPlanPath1"
                    id="file1"
                    onchange="javascript:uploadTeachingPlan(1)"
                    />
        </tr>
        <tr>
            <td colspan="10">
                <li>请将文件压缩后上传,教案格式必须是 *.RAR或 *.ZIP格式,且不能为空!</li>
            </td>
        </tr>
        <tr>
            <ww:textarea
                    tooltip="教案说明要控制在一百字以内!"
                    label="教案说明(1)"
                    cols="87"
                    rows="5"
                    name="teachingPlan.planInfo1"
                    labelposition="col5"
                    value=""/>
        </tr>
        <tr>
            <ww:file
                    label="教案上传(2)"
                    name="teachingPlanPath2"
                    value=""/>
        </tr>
        <tr>
            <td colspan="10">
                <li>请将文件压缩后上传,教案格式必须是 **.RAR或 **.ZIP格式,且不能为空!</li>
            </td>
        </tr>
        <tr>
            <ww:textarea
                    tooltip="教案说明要控制在一百字以内!"
                    label="教案说明(2)"
                    cols="87"
                    rows="5"
                    name="teachingPlan.planInfo2"
                    labelposition="col5"
                    value=""/>
        </tr>
        <tr>
            <ww:submit value="添加" labelposition="col6"/>
        </tr>
    </table>
    <ww:actionerror/>
    <ww:fielderror/>
    <ww:actionmessage/>
</ww:form>

</body>
</html>

2.action代码:
public class TeachingPlanAction extends BaseTeachingPlanAction {

    public void upFile(File file, String name, String type) throws Exception {
        super.execute();
        if (file != null) {
            FileOutputStream outputStream1 = new FileOutputStream("d:/" + name);
            FileOutputStream outputStream2 = new FileOutputStream("d:/" + name);
            FileInputStream inputStream = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) > 0) {
                outputStream1.write(buffer, 0, len);
                outputStream2.write(buffer, 0, len);
            }
            inputStream.close();
            outputStream1.close();
            outputStream2.close();
        } else {
        }
    }


    public String insertTeachingPlan() throws Exception {
        super.execute();
        upFile(teachingPlanPath1, teachingPlanPath1FileName, teachingPlanPath1ContentType);
        upFile(teachingPlanPath2, teachingPlanPath2FileName, teachingPlanPath2ContentType);
        String path1 = "";
        String path2 = "";

        if (teachingPlanPath1 != null) {
            path1 = teachingPlanPath1.getParent() + teachingPlanPath1FileName;
        }
        if (teachingPlanPath2 != null) {
            path2 = teachingPlanPath2.getParent() + teachingPlanPath2FileName;
        }
        teachingPlan.setTeachingPlanPath1(path1);
        teachingPlan.setTeachingPlanPath2(path2);
        this.teachingPlan.userId = teachingPlan.teachingPlanId;
        this.teachingPlanDAO.insertTeachingPlan(teachingPlan);
        addActionMessage("添加成功");
        return SUCCESS;
    }

3.TeachingPlan-xwork.xml配置文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
<xwork>
    <include file="com/common/Base-xwork.xml"/>
    <package name="teachingPlan" extends="JpkAdminHomeWork" namespace="/admin">

<action name="insertTeachingPlan" class="com.modules.teachingPlan.action.TeachingPlanAction"
                method="insertTeachingPlan">
            <external-ref name="teachingPlanDAO">teachingPlanDAOProxy</external-ref>
            <interceptor-ref name="validationWorkflowStack"/>
            <result name="success" type="dispatcher">
                <param name="location">/admin/addTeachingPlan.jsp</param>
            </result>
            <result name="input" type="dispatcher">
                <param name="location">/admin/addTeachingPlan.jsp</param>
            </result>
        </action>
</package>
</xwork>
4.ibatis中SQL语句:
<insert id="insertTeachingPlan" parameterClass="teachingPlan">
        <![CDATA[
            INSERT INTO teachingplan
            (teachingPlanId,teachingName,lessonName,teachingPlanPath1,planInfo1,teachingPlanPath2,planInfo2,userId)
            VALUES
            (#teachingPlanId#,#teachingName#,#lessonName#,#teachingPlanPath1#,#planInfo1#,#teachingPlanPath2#,#planInfo2#,#userId#)
          ]]>
        <selectKey resultClass="int" keyProperty="teachingPlanId">
            SELECT LAST_INSERT_ID();
        </selectKey>
    </insert>
到此时就可以实现文件上传了.补充一句在这里我要上传两个文件
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics