ITPub博客

首页 > IT职业 > IT生活 > 利用Webwork的Inputstream和Outputstream进

利用Webwork的Inputstream和Outputstream进

IT生活 作者:mmmike 时间:2021-09-09 08:57:46 0 删除 编辑
http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">

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

最近在做一个管理系统,里面涉及到了文件上传;在网上找了好多的方法,不过很多都包含有过期的代码,很是郁闷。
努力了几天,终于用利用Webwork的Inputstream和Outputstream实现了文件的上传
下面具体代码:
1.前台jsp代码:
Created by IntelliJ IDEA.
User: Luckystar_N
Date: 2007-10-11
Time: 20:19:31
--%>



添加授课教案管理---Foreign Language Department

enctype="multipart/form-data"
validate="true" theme="xhtml">



          
教案管理
  • 请将文件压缩后上传,教案格式必须是 *.RAR或 *.ZIP格式,且不能为空!
  • 请将文件压缩后上传,教案格式必须是 **.RAR或 **.ZIP格式,且不能为空!



  • 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配置文件





    method="insertTeachingPlan">
    teachingPlanDAOProxy


    /admin/addTeachingPlan.jsp


    /admin/addTeachingPlan.jsp



    4.ibatis中SQL语句:

    INSERT INTO teachingplan
    (teachingPlanId,teachingName,lessonName,teachingPlanPath1,planInfo1,teachingPlanPath2,planInfo2,userId)
    VALUES
    (#teachingPlanId#,#teachingName#,#lessonName#,#teachingPlanPath1#,#planInfo1#,#teachingPlanPath2#,#planInfo2#,#userId#)
    ]]>

    SELECT LAST_INSERT_ID();

    到此时就可以实现文件上传了.补充一句在这里我要上传两个文件

    来源: http://guofengniu.iteye.com/blog/137752

    来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2872/viewspace-2807491/ ,如需转载,请注明出处,否则将追究法律责任。

    请登录后发表评论 登录
    全部评论

    注册时间:2019-08-19

    • 博文量
      77
    • 访问量
      550