第三方控件

用commons-fileupload向数据库中添加图片

<%@page import="java.util.*,java.io.*,org.apache.commons.fileupload.*"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="java.util.Date"%>
<%@page import="com.pb.news.entity.News"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%@include file="../common/common.jsp"%>
<%
request.setCharacterEncoding("utf-8");
String fieldName="";
boolean flag=false;
String uploadFileName="";
boolean bupload=false;

News news =new News();
news.setCreateDate(new Date());
boolean isMultipart=ServletFileUpload.isMultipartContent(request);
//获取文件夹的位置保存上传后的文件
String uploadFilePath=request.getSession().getServletContext().getRealPath("/upload/");
if(isMultipart==true){
    //FileItemFactory 用于构建FileItem实例的工厂
    FileItemFactory factory=new DiskFileItemFactory();
    //ServletFileUpload类是Apache 文件上传组件中,用于处理文件上传的一个核心类。
    //它的作用是以List形式返回每一个被封装成FileItem类型的表单元素集合。
    
    ServletFileUpload upload=new ServletFileUpload(factory);
    try{
    List<FileItem> items=upload.parseRequest(request);
    //获得集合的迭代器
    Iterator<FileItem> iter=items.iterator();
    while(iter.hasNext()){
        FileItem item=(FileItem)iter.next();
        //判断字段是否是普通字段
        if(item.isFormField()){
            fieldName=item.getFieldName();
            if(fieldName.equals("title")){
                news.setTitle(item.getString("utf-8"));
            }else if(fieldName.equals("summary")){
                news.setSummary(item.getString("utf-8"));
            }else if(fieldName.equals("newscontent")){
                news.setContent(item.getString("utf-8"));
            }else if(fieldName.equals("picPath")){
                news.setPicPath(item.getString("utf-8"));
            }else if(fieldName.equals("author")){
                news.setAuthor(item.getString("utf-8"));
            }else if(fieldName.equals("categoryId")){
                news.setCategoryid(Integer.parseInt(item.getString()));            
            }
        }else{
            String fileName=item.getName();
                 if(!fileName.equals("")&&fileName!=null)
                 {
                File fullFile=new File(item.getName());
                File saveFile=new File(uploadFilePath,fullFile.getName());
                item.write(saveFile);
                uploadFileName=fullFile.getName();
                news.setPicPath(uploadFileName);
                bupload=true;
                }
            
            }
    }
        }catch(Exception e){
    e.printStackTrace();    
        }
}
    
//调用方法

flag=newsService.addNews(news);
if(flag){
    %>
<jsp:forward page="admin.jsp" />
<%
}else{
    %>
<jsp:forward page="newsDetailList.jsp" />
<%
}
%>
View添加图片

CKEditor
使用CKEditor的步骤

1。下载CKEditor

2.解压并复制CKEditor到项目中

3.在页面中加入CKEditor

3-1.

<Script type="text/javascript" src="<%=request.getContextPath()%/ckeditor/ckeditor.js>"
</Script>

3-2.在"内容"textarea标签中加入class=“ckeditor”

通过config.js配置CKEditor

CKEDITOR.editorConfig=funtion(config)
{
      config.language='zh-cn';//配置语言。zh-cn代表中文
      config.uiColor='#AASC6E';//背景颜色
    config.width='auto';//宽度
    config.height='300px';//高度
    config.skin='office2003';//皮肤i:v2,kama,office2003
}    

CKEdito文件夹

_samples :存放官方的Demo

lang;存放多国语言文件

skins:皮肤文件

4.使用CKEditor编辑内容

5.提交CKEditor编辑的内容。

CKEditor

posted on 2015-06-24 16:22  Longevity  阅读(180)  评论(0编辑  收藏  举报