最近在做一个管理系统,里面涉及到了文件上传;在网上找了好多的方法,不过很多都包含有过期的代码,很是郁闷。
努力了几天,终于用利用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> 到此时就可以实现文件上传了.补充一句在这里我要上传两个文件
分享到:
相关推荐
webwork中上传文件,webwork文件上传.doc
WebWork 文件上传 多个文件上传 范例
项目里需要实现webwork 上传与下载文件操作, 自己摸索过程中出现了很多问题,一一解决, 记录下来本准备做参考,现贡献给大家, 记得有点乱,耐心看完,一定会有帮助的。
简单的使用webwork框架实现了文件上传的功能 主要是针对EXCEL文件
fileupload+dwr2+webwork2实现带进度条上传文件
webwork框架xwork.xml、web.xml这两个文件让你可以随意过滤自己想要过滤的某些namespace命名空间下的.action、.jsp、*。
关键是正对最新版webwork,在action中进行操作,说得很细致,有源代码实例。也将异常情况处理列出来了。
webwork 2.2.5框架资源,大家自由学习.学习框架
这个是我查看了大量的网上文档后,利用webwork的上传机制写的一个上传到例子。因为网上很多都转来转去的文档。能不能不运行,还不一定。改起来很费时,所以我把我写的上传上去。力争做到用最少的代码和简单配置来...
NULL 博文链接:https://xiaoqiang21.iteye.com/blog/763674
WebWork 当然也提供了很友好的拦截器来实现对文件的上传,让我们可以专注与业务逻辑的设计和实现,在实现上传和下载时顺便关注了下框架上传下载的实现。 1. 包装 Request 请求 •每次客户端请求 Action 时,都会...
我自己写的。上次的只有上传没有下载。这次的添加了下载的功能。我自己备份用的。有需要可以参考。保证能够运行
webwork 控制反转小例子,配置文件的写法
webwork 2.2.4开发指南
WEBWORK的配置
webwork上传文件pdf文件说明文档
WebWork2.0讲解说明,ppt文件。
webwork教程webwork教程webwork教程webwork教程webwork教程webwork教程webwork教程webwork教程webwork教程webwork教程webwork教程webwork教程
WEBWORK的源文件压缩包WEBWORK的源文件压缩包WEBWORK的源文件压缩包WEBWORK的源文件压缩包WEBWORK的源文件压缩包WEBWORK的源文件压缩包WEBWORK的源文件压缩包WEBWORK的源文件压缩包WEBWORK的源文件压缩包
详细的介绍了webwork的使用机制和标签的归类使用