利用Webwork的Inputstream和Outputstream进行文件上传
最近在做一个管理系统,里面涉及到了文件上传;在网上找了好多的方法,不过很多都包含有过期的代码,很是郁闷。
努力了几天,终于用利用Webwork的Inputstream和Outputstream实现了文件的上传
下面具体代码:
1.前台jsp代码:
Created by IntelliJ IDEA.
User: Luckystar_N
Date: 2007-10-11
Time: 20:19:31
--%>
| 教案管理 | |||||||||
|
|
|||||||||
|
|
|||||||||
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配置文件
/admin/addTeachingPlan.jsp
/admin/addTeachingPlan.jsp
INSERT INTO teachingplan
(teachingPlanId,teachingName,lessonName,teachingPlanPath1,planInfo1,teachingPlanPath2,planInfo2,userId)
VALUES
(#teachingPlanId#,#teachingName#,#lessonName#,#teachingPlanPath1#,#planInfo1#,#teachingPlanPath2#,#planInfo2#,#userId#)
]]>
SELECT LAST_INSERT_ID();
到此时就可以实现文件上传了.补充一句在这里我要上传两个文件
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2872/viewspace-2807491/ ,如需转载,请注明出处,否则将追究法律责任。