您的位置:首页 > 编程语言 > Java开发

struts2基于模型驱动的文件上传(上传视频为例子)

2017-06-10 16:16 295 查看
package cn.hbmy.tas.entity;

import java.io.File;

public class Video {
private Integer id;     //id
private File video;
private String videoFileName;   //表示  上传的视频的资料名字
private String videoContentType;   //文件的后缀名
private User user;     //上传的人相关联	起来
private String  savePath;   //表示存储的路径

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public File getVideo() {
return video;
}
public void setVideo(File video) {
this.video = video;
}
public String getVideoFileName() {
return videoFileName;
}
public void setVideoFileName(String videoFileName) {
this.videoFileName = videoFileName;
}
public String getVideoContentType() {
return videoContentType;
}
public void setVideoContentType(String videoContentType) {
this.videoContentType = videoContentType;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
这是实体类的创建
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 引入js标签库 -->
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>上传视频</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script>
<script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script>
<script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" />
<script type="text/javascript">
</script>
</head>
<body>
<s:form action="video_add.action" enctype="multipart/form-data" method="post">
请选择视频文件:<s:file name="video"></s:file><br/>
<s:submit value="上传"></s:submit>
<s:fielderror></s:fielderror>
</s:form>
</body>
</html>上面就是上传页面的jsp文件,注意表单的提交!
public String add() throws Exception {
// 封装带对象中 保存数据库 有时候 我们也可以使用model 但是要设置为封装的属性
//得到文件的真实的路径
//文件上传成功了;
ServletContext application = ServletActionContext.getServletContext();
String directory = application.getInitParameter("rootDirectory");
String saveFilePath = directory;
// File file = new File(saveFilePath+model.getUser().getUserName());
File file = new File(saveFilePath);
// 如果指定的路径没有就创建
if (!file.exists()) {
file.mkdirs();
}
File target = new File(saveFilePath,model.getVideoFileName());
FileUtils.copyFile(model.getVideo(),target);
model.setSavePath(saveFilePath);
//这样我们就保存了 数据的路径 我们查找的时候,根据这个路径 找到对应视频资源到的id,从而就可以实现下载的功能了
videoService.save(model);
return "toList";
}上面就是VideoAction类里面的add()添加视频的方法
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd" >

<struts>
<!-- 指定国际化资源文件的baseName为uploadFile -->
<constant name="struts.custom.i18n.resources" value="uploadFile"></constant>
<!-- 配置为开发模式 -->
<constant name="struts.devMode" value="true"/>
<!-- 配置编码 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 把扩展名配置为action -->
<constant value="action" name="struts.action.extension"/>
<!-- 把主题配置为simple -->
<constant value="simple" name="struts.ui.theme"/>
<!-- 设置文件上传的大小  视频文件上传最大为100M-->
<constant name="struts.multipart.maxSize" value="104857600"></constant>
<!-- 指定视频上传的类型, -->
<package name="demo" extends="struts-default" namespace="/">
<action name="video_*" class="videoAction" method="{1}">
<!-- 我们在这儿配置拦截器,只允许视频后缀名的文件才能够上传 -->
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedExtensions">avi,rmvb,rm,asf,divx,mpg,mpeg,mpe,wmv,mp4,mkv,vob</param>
</interceptor-ref>
<result name="list">/System_Video/list.jsp</result>
<result name="addUI">/System_Video/addUI.jsp</result>
<result name="input">/System_Video/addUI.jsp</result>
<result name="toList" type="redirectAction">video_list</result>
</action>
</package>
</struts>
上面的就是struts.xml的文件中的配置,都有解释,大家参考一下。
<!--显示数据列表-->
<s:iterator value="videoList">
<tr class="TableDetail1 template">
<td>${videoFileName} </td>
<td>${user.userName} </td>
<td>
<s:a action="user_editUI?id=%{id}">播放</s:a>

<s:a action="user_initPassword?id=%{id}" onclick="return window.confirm('您确定要下载吗?')">下载</s:a>

<s:a action="video_delete?id=%{id}" onclick="return delConfirm()">删除</s:a>
</td>
</tr>
</s:iterator>
上面的就是显示的jsp页面,由于我在写一个管理系统,这个里面的代码量比较的大,我就粘贴部分核心的代码!
struts.messages.error.file.too.large=\u4E0A\u4F20\u6587\u4EF6{1}\u8FC7\u5927
struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u7684\u6587\u4EF6\u53EA\u80FD\u662F\u89C6\u9891\u6587\u4EF6
struts.messages.error.uploading=\u4E0A\u4F20\u8FC7\u7A0B\u4E2D\u51FA\u73B0\u672A\u77E5\u7684\u9519\u8BEF

这一部分就是设置文件上传时候出现的错误的返回的信息!

这个代码不全,仅供大家参考!后面我会将视频的在线播放以及下载发出来。大家有什么建议也可以给我说,毕竟我是第一次自己独立的写一个小型的项目!

因为好多人问我,我就把代码分享出来,大家一起学习!
链接:https://pan.baidu.com/s/1a5LkDC3T8-5xuU-fHjR3qQ 密码:w72
4000
6
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息