struts2文件上传

现在说一下昨天做的记录。忘记说毕设是做一个网站,类似视频博客吧,还有一个是视频通话的功能,不晓得可不可以做的完,其他就是一些杂七杂八的功能,目前就做好了博客的一些功能。记录的东西都不是很细,只是关键的部分(自认为)。


昨天毕设做了两件事情,一个是完成了分页剩下的工作,还有就是上传视频(从网页端上传到服务器区域,上传的具体地点是服务器网站编译后的目录,即类似这个目录:D:\Software\javaEE\EclipseWorkSpace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\VideoCall\videos,这个目录是我的目录,我是直接在eclipse中直接运行的,所以就上传到了这个目录)。


1.本来分页的最后是想用struts2的标签的:
<s:if test="%{UserUtil.currentPage== 0}">
        <a title="下一页" href="nextPage.action?page=<%= UserUtil.currentPage+1 %>"
             id="blog-lite-next-page-btn"> 下一页a </a>
</s:if>
但是不知道怎么回事,一直没有预期的效果, 所以就直接用Java代码了的:
<%if(UserUtil.currentPage==0){ %>
       <a title="下一页" href="nextPage.action?page=<%= UserUtil.currentPage+1 %>"
            id="blog-lite-next-page-btn"> 下一页a </a>
<%}。。。。。。。。。
这个是前天遗留的问题。


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>发现这个标签很强大, 它向action传递了三个东西,一个是文件本身,一个文件名, 还有就是文件类型,即
private File xxx;
private String xxxFileName;
private String xxxContentType;
这三个参数。文件名的传递很给力,如果是用file. getname()这个函数得到的名字不是上传文件原本的名字。
以下是关键部位的具体实现,网上很多,也是参考网上的代码的。

@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. getServletContext()
                .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( getFirstPart(fileName));
        video.setUserId(user.getId());
        hibernateTemplate. saveOrUpdate(video);// 这个是spring的
       
        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;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值