现在说一下昨天做的记录。忘记说毕设是做一个网站,类似视频博客吧,还有一个是视频通话的功能,不晓得可不可以做的完,其他就是一些杂七杂八的功能,目前就做好了博客的一些功能。记录的东西都不是很细,只是关键的部分(自认为)。
昨天毕设做了两件事情,一个是完成了分页剩下的工作,还有就是上传视频(从网页端上传到服务器区域,上传的具体地点是服务器网站编译后的目录,即类似这个目录:D:\Software\javaEE\EclipseWorkSpace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\VideoCall\videos,这个目录是我的目录,我是直接在eclipse中直接运行的,所以就上传到了这个目录)。
1.本来分页的最后是想用struts2的标签的:
<s:if test="%{UserUtil.currentPage==
<a title="下一页" href="nextPage.action?page=<%=
id="blog-lite-next-page-btn">
</s:if>
但是不知道怎么回事,一直没有预期的效果,
<%if(UserUtil.currentPage==0){ %>
<a title="下一页" href="nextPage.action?page=<%=
id="blog-lite-next-page-btn">
<%}。。。。。。。。。
这个是前天遗留的问题。
2.现在说说上传文件。
<s:form action="upload.action" method="post" enctype="multipart/form-data">
<s:file name="upload" label="上传文件"></s:file>
<s:textfield name="fileCaption" label="备注"></s:textfield>
<s:submit value="上传"></s:submit>
</s:form>
这个是jsp文件,有个struts2标签是<s:file name="xxx"></s:file>发现这个标签很强大,
private File xxx;
private String xxxFileName;
private String xxxContentType;
这三个参数。文件名的传递很给力,如果是用file.
以下是关键部位的具体实现,网上很多,也是参考网上的代码的。
@Override
public boolean UpLoadFile(File file, User user,String fileName/*这个名字就是从action中传过来的*/
// TODO Auto-generated method stub
long time = new Date().getTime();
String destFileName= time + getExtention(fileName);
File destFile = new File(ServletActionContext.
.getRealPath("/videos") + "/" + destFileName);//这个是获取路径
Video video = new Video();//这个是我自定义的类
video.setComment("");
video.setDeleted(false);
video.setLocked(false);
video.setPath("videos/"+time);
video.setUpLoadTime(new Date());
video.setVideoName(
video.setUserId(user.getId());
hibernateTemplate.
return copyToLocal(file,destFile);
}
@Override
public boolean copyToLocal(File src, File dest) {
// TODO Auto-generated method stub
boolean result = false;
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src),
UserUtil.BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dest),
UserUtil.BUFFER_SIZE);
byte[] buffer = new byte[UserUtil.BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
result = true;
} catch (Exception e) {
e.printStackTrace();
result = false;
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}