首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

chx 学习jForum笔记十六 实现附件移植,添加附件时按钮无效的有关问题解决

2012-12-21 
chx 学习jForum笔记十六 实现附件移植,添加附件时按钮无效的问题解决背景?原论坛附件存放于d:\res\cib

chx 学习jForum笔记十六 实现附件移植,添加附件时按钮无效的问题解决

==背景==

?

原论坛附件存放于d:\res\cibforum\500整\id\

?

====修改程序使之符合原规范====

修改\view\forum\common\AttachmentCommon.java中的makeStoreFilename(AttachmentInfo attInfo)

private String makeStoreFilename(AttachmentInfo attInfo) { Calendar cal = new GregorianCalendar(); cal.setTimeInMillis(System.currentTimeMillis()); cal.get(Calendar.YEAR); int year = Calendar.getInstance().get(Calendar.YEAR); int month = Calendar.getInstance().get(Calendar.MONTH) + 1; int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); StringBuffer dir = new StringBuffer(256); dir.append(year).append('/').append(month).append('/').append(day).append('/');
?

改为

private String makeStoreFilename(AttachmentInfo attInfo,int postid) { StringBuffer dir = new StringBuffer(256); int id500 = postid-(postid%500)+500; dir.append(id500).append('/').append(postid).append('/');

?从上面的情况可以看出,原先JFORUM将附件存放于年/月/日/目录下,现修改为与POST的ID相关的目录下。

?

 return dir.append(MD5.crypt(attInfo.getRealFilename() + System.currentTimeMillis())) .append('_').append(SessionFacade.getUserSession().getUserId()).append('.') .append(attInfo.getExtension().getExtension()).append('_').toString();
?


改为

 return dir.append(attInfo.getRealFilename()).append('_') .append(SessionFacade.getUserSession().getUserId()).append('.') .append(attInfo.getExtension().getExtension())    .append('_').toString();

?从上面的情况可以看出,原先JFORUM将附件名为对文件名及当前时间进行MD5加密后生成的字符串加用户ID,现修改为真实文件名加用户ID。



向public void insertAttachments(final Post post)中增加

 AttachmentInfo info = attachment.getInfo(); String savePath = this.makeStoreFilename(info,attachment.getPostId()); if (!info.getPhysicalFilename().equals(savePath)){   info.setPhysicalFilename(savePath); }

为了改变info的实际保存地址,在预处理时,postid=0,需在此进行调整

将public void preProcess()中的

 String savePath = this.makeStoreFilename(info);

?

改为

String savePath = this.makeStoreFilename(info,userId);
?


对SystemGlobals.properties进行设置

attachments.store.dir = d:/res/cibforum
?

?

====添加附件时按钮无效的问题解决====

进入发帖的界面时,IE会报“网页有错误”

在发布帖子的时候,发现点击“附件”按钮没有效果。

这个问题也查了两天的时间,后来同事高手感觉是jquery没运行。看源码才发现jquery指向了外网,所以内网上会有问题。

看程序的源文件,其中带有jquery,指向程序源文件中的位置问题即可解决。
查找原文件中的所有"jquery"字样的语句。
将其中的
?src="http://code.jquery.com/jquery-1.4.2.min.js"
改为
?src="${contextPath}/javascript/jquery-1.4.2.min.js"

====建立原论坛的附件库====

由于原论坛的帖子已经全部导入了新库,但jforum对附件有相应的表需要新增记录。以下就在管理界面中增加一个同步按钮,用于对原论坛的贴子进行分析,有附件的就查一下物理磁盘上是否有相应文件,如果有的话,就向jforum的附件表中新增相应的记录。


1、修改attachments_config.htm文件,在
?<input type="submit" value="${I18n.getMessage("Update")}" />&nbsp;&nbsp;
前添加语句

 <input type="button" value="trans files" name="button"  onclick="document.location = '${JForumContext.encodeURL("/${moduleName}/transfiles")}';" />&nbsp;&nbsp;

用于在管理界面中的“附件”中增加一个按钮。



2、在urlPattern.properties文件中,添加

 adminAttachments.transfiles.0 =

?这次吸取前次教训,向urlPattern.properties文件中注册一个action。



3、在generic_queries.sql中增加

 PostModel.getMaxId = SELECT MAX(post_id) FROM jforum_posts;

?取得所有POST的最大ID号,用于循环条件。



4、在AttachmentsAction.java中增加

    public void transfiles(){        Post post ;        StringBuffer resulttext;        int codeIndex;        int codeEndIndex;        int nextStartPos;        int maxId=0;        String nonCodeResult;        String codeResult;        PostDAO pm = DataAccessDriver.getInstance().newPostDAO();        PreparedStatement p = null;        ResultSet rs = null;        try {            p = JForumExecutionContext.getConnection()                .prepareStatement(SystemGlobals.getSql("PostModel.getMaxId"));            rs = p.executeQuery();            if (rs.next()){               //取最大ID号               maxId = rs.getInt(1);            }        } catch (SQLException e) {                throw new DatabaseException(e);            }            finally {                DbUtils.close(rs, p);            }                 for (int postId=0; postId < maxId ; postId++){            //取POST            post =  pm.selectById(postId);            if (post.getId()>0){                //分析POST中的内容                codeIndex = post.getText().indexOf("[link=");                codeEndIndex = codeIndex > -1 ? post.getText().indexOf("[/link]") : -1;                if (codeIndex > -1 && codeEndIndex > -1 && codeEndIndex > codeIndex) {                    nextStartPos = 0; //起始位置                    resulttext = new StringBuffer(post.getText().length()); //替换成新内容                    while (codeIndex > -1 && codeEndIndex > -1 && codeEndIndex > codeIndex) {                        codeEndIndex += "[/link]".length(); //结束位置                        nonCodeResult = post.getText().substring(nextStartPos, codeIndex); //非附件内容                        //附件处理                        codeResult = ChxNew_parseCode(post.getText().substring(codeIndex, codeEndIndex),post);                        System.out.println(codeResult); //附件内容替换                        resulttext.append(nonCodeResult).append(codeResult);                        nextStartPos = codeEndIndex;                        codeIndex = post.getText().indexOf("[link=", codeEndIndex);                        codeEndIndex = codeIndex > -1 ? post.getText().indexOf("[/link]", codeIndex) : -1;                    }                    if (nextStartPos > -1) {   //尾部处理                        nonCodeResult = post.getText().substring(nextStartPos);                        resulttext.append(nonCodeResult);                    }                    post.setText(resulttext.toString());                    post.hasAttachments(true);                    pm.update(post);//保存post中的文本                }            }        }        this.list();//返回原界面    }

?以上内容为取每条POST,并分析其中的内容。将类似于[link=xxxx.xxx]xxxx[/link]的字符串交给下面的子函数处理。并用返回内容替换原有内容。

?

  /*    分析link中的内容,    向附件表添加相应的记录     */    private static String ChxNew_parseCode(final String text,final Post post)    {        //text 应为类似于 [link=xxx.xls]xxxx[/link]的字符串        int index = text.indexOf("[link=");        int index2 = text.indexOf("]");        int lastIndex = text.indexOf("[/link]", index2);        if (index > -1 && index2 > index && lastIndex > index2) {           String filename = text.substring(index+"[link=".length(),index2);           String description = text.substring(index2+1,lastIndex);           if (AttachmentCommon.insertAttachment(filename,description,post))               return "〔附件:"+description+"〕";        }        return text;    }

以上内容就是分析字符串,获取文件名及文件描述,交给下面的子函数进行处理,如果处理成功就返回一个替换原内容的字符串,如果失败则返回原字符串(即不替换原有内容)。

?

4.在AttachmentCommon.java中增加

    static public boolean insertAttachment(final String filename,final String description,final Post post){        final int postid = post.getId();        final int id500 = postid-(postid%500)+500;        final String PhysicalFilename=SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_STORE_DIR) + "/" + id500 + "/"+postid+"/"+filename;        final File file = new File(PhysicalFilename);        if (file.exists()){//判断文件是否存在于磁盘上。            try{            final int userId = post.getUserId();            Attachment attachment = new Attachment();            attachment.setUserId(userId);            attachment.setPostId(postid);            AttachmentInfo info = new AttachmentInfo();            info.setFilesize(file.length());            info.setComment(description);            info.setMimetype(new MimetypesFileTypeMap().getContentType(file));            info.setRealFilename(filename);            info.setUploadTimeInMillis(post.getTime().getTime());            String extension = filename.substring(filename.lastIndexOf('.') + 1);            AttachmentDAO attachmentDao=DataAccessDriver.getInstance().newAttachmentDAO();            AttachmentExtension ext = attachmentDao.selectExtension(extension.toLowerCase());            if (ext.isUnknown()) {                ext.setExtension(extension);            }            info.setExtension(ext);            info.setPhysicalFilename(PhysicalFilename);            attachment.setInfo(info);            attachmentDao.addAttachment(attachment);//调用程序原生的添加附件功能。            return true;            }            catch (Exception e){                System.out.println("error!"+e);                return false;            }        }        else {            return false;        }    }

?以上是根据参数判断文件是否存在,如果存在则生成相应记录加入附件表及附件信息表。

?

热点排行