最近要實現在ckedit上傳圖片,而且要上傳保存到數據庫,在網站找了不少資料,基本上都是保存到服務器的文件夾,但那些資料對我也起了很多作用。下面我把我的實現記錄下來,作為備忘。(CKEditor 在jsp中實現文件上傳的完整例子)
http://blog.163.com/prevBlogPerma.do?host=ytrtfhj@126&srl=890531092010226023136&mode=prev
ckeditor非常強大,但官網上它的圖片/文件上傳使用了ckfinder,比較麻煩。我首先是在圖片鏈接輸入框的右邊加個按鈕,點擊事件鏈接到已有的上傳模塊,上傳后把圖片的下載url(這個花了我不少時間)傳遞給左邊的輸入框即可。步驟如下:
打開plugins\image\dialogs\image.js,在鏈接輸入框代碼結尾也就是 m.lang.image.urlMissing)},后面加上這些代碼:
- {type:'button',id:'myUpload',align:'center',label:'新上傳 ',onClick:function(){var thisDialog = this.getDialog();var txtUrlObj = thisDialog.getContentElement('info', 'txtUrl'); var txtUrlId = txtUrlObj.getInputElement().$.id;
- var retFile = window.open("info2j.zrx?path=info/info_uploadImage&infotypeid="+top.document.getElementById('infotypeid').value+"&txtUrlId="+txtUrlId, "", "height=300, width=400"); if(retFile != null){this.getDialog().setValueOf('info','txtUrl',retFile);}}},
{type:'button',id:'myUpload',align:'center',label:'新上傳 ',onClick:function(){var thisDialog = this.getDialog();var txtUrlObj = thisDialog.getContentElement('info', 'txtUrl'); var txtUrlId = txtUrlObj.getInputElement().$.id; 為了獲得圖片URL文本框的id,并可以傳回頁面;我這里由于jsp頁面都是放在WEB-INF目錄下,所以頁面跳轉是借助action來實現的。所謂我是先把id傳到info2j.action,然后再從info_uploadImage.jsp頁面拿,
var retFile = window.open("info2j.action?path=info/info_uploadImage&infotypeid="+top.document.getElementById('infotypeid').value+"&txtUrlId="+txtUrlId, "", "height=300, width=400");
跳轉到info_uploadImage.jsp頁面,并把圖片URL文本框的id傳過去
下面是info_uploadImage.jsp的簡單代碼
- <%@ page language="java" contentType="text/html; charset=UTF-8"%>
- <%@ taglib uri="/struts-tags" prefix="s"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <html>
- <head>
-
- <title>上傳圖片</title>
- <script type="text/javascript">
-
- function checkFormat(filePath){
- var i = filePath.lastIndexOf(".");
- var len = filePath.length;
- var str = filePath.substring(i+1,len);
- var extName = "JPG,GIF,PNG,BMP";
-
- if(extName.indexOf(str.toUpperCase()) < 0){
- alert("圖片格式不正確");
- return false;
- }
- return true;
-
- }
-
- /*
- 圖標預覽,兼容ie,firefox
- */
- function fileChange(o){
- if(checkFormat(o.value)){
- if(window.ActiveXObject){
- document.getElementById("uploadImg").width = 100;
- document.getElementById("uploadImg").height = 100;
- document.getElementById("uploadImg").src = o.value;
- }else{
- document.getElementById("uploadImg").width = 100;
- document.getElementById("uploadImg").height = 100;
- document.getElementById("uploadImg").src = o.files[0].getAsDataURL();
- }
-
- }
- }
-
- /* 給圖片text附上圖片地址*/
- function finish(generatedId){
- //獲得圖片text的id
- var imageUrl = document.getElementById("txtUrlId").value;
-
- alert("<s:text name='info.upload.success' />!");
-
- window.opener.document.getElementById(imageUrl).value = "info_downloadImage.zrx?imageId="+generatedId;
- window.opener.document.getElementById("previewImage").setAttribute("src","info_downloadImage.zrx?imageId="+generatedId);
- window.close();
- }
-
- </script>
-
- </head>
-
- <body>
-
-
- <s:form id="uploadForm" name="uploadForm" method="post" action="info_upLoadImage.zrx" theme="simple"
- enctype="multipart/form-data" target="frame">
- <s:hidden id="infotypeid" name="infotypeid"></s:hidden>
- <s:hidden id="txtUrlId" name="txtUrlId"></s:hidden>
- <table align="center">
- <tr>
- <td colspan="2"><s:file id="image" name="image" onchange="return fileChange(this)" /></td>
- </tr>
- <tr>
- <td><input type="submit" value="上傳" onclick="upload()"/></td>
- <td><input type="button" value="取消" onclick="javascript:window.close();"/></td>
- </tr>
- </table>
- <div align="center">
- <img src="" width="0" height="0" id="uploadImg"/>
- </div>
-
- </s:form>
-
-
- <iframe name="frame" style="display:none" src="_self">
-
- </iframe>
- </body>
- </html>
下面是上傳圖片和下載圖片方法
-
-
-
-
-
-
-
-
- public String upLoadImage() throws Exception{
-
- if (null != image) {
-
-
- infoImage = new InfoContentImage();
-
- String imageName = imageFileName;
- String imageType = imageContentType;
- Long length = image.length();
-
-
- infoImage.setInfotypeid(new Long(infotypeid));
- infoImage.setImagename(imageName);
- infoImage.setImagetype(imageType);
- infoImage.setImagesize(length);
- infoImage.setDate(new Date());
-
-
-
- try {
- InputStream is = new BufferedInputStream(new FileInputStream(image));
-
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- int len = 0;
- byte[] content = new byte[is.available()];
-
- while (-1 != (len = is.read(content))) {
- os.write(content);
- }
- infoImage.setImagecontent(os.toByteArray());
- is.close();
- os.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- Long generatedId = this.infoService.saveInfoContentImage(infoImage);
-
- this.writeMessage("<script type='text/javascript'>parent.finish('"+ generatedId +"');</script>");
- return null;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public String downloadImage() throws Exception{
-
- HttpServletResponse response = this.getResponse();
- response.setCharacterEncoding("UTF-8");
- response.setContentType("image/*");
-
-
- OutputStream output = response.getOutputStream();
-
- infoImage = infoService.getInfoContentImageById(imageId);
- byte[] b = infoImage.getImagecontent();
- output.write(b);
-
- return null;
- }
修改打開上傳窗口可以打開多個和打開的窗口不居中的原因,修改原來的image.js
- {type:'button',id:'myUpload',align:'center',label:'新上傳 ',onClick:function(){var thisDialog = this.getDialog();
- var txtUrlObj = thisDialog.getContentElement('info', 'txtUrl'); var txtUrlId = txtUrlObj.getInputElement().$.id;
- var retFile = window.open("info2j.zrx?path=info/info_uploadImage&infotypeid="+top.document.getElementById('infotypeid').value+"&txtUrlId="+txtUrlId, "上傳圖片", "height=300, width=400,top="+(screen.height-300)/2+",left="+(screen.width-400)/2); if(retFile != null){this.getDialog().setValueOf('info','txtUrl',retFile);}}},
本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請
點擊舉報。