【发布时间】:2015-05-16 16:54:57
【问题描述】:
我将图片上传到 ckeditor.db 中,配置如下:
CKEDITOR.editorConfig = function(config) {
var contextPath= $(.contextPath).html();
config.filebrowserImageBrowseUrl =contextPath+ '/ckeditor/getimage';
config.filebrowserImageUploadUrl = contextPath+'ckeditor/uploadimage';
config.filebrowserBrowseUrl = contextPath+'/ckeditor/getimage';
};
我使用这个 servlet 上传到数据库。
@SuppressWarnings("unchecked")
public class CKEditorUploadServlet extends HttpServlet {
private static final long serialVersionUID = -7570633768412575697L;
private static final Logger LOG = LoggerFactory
.getLogger(CKEditorUploadServlet.class);
private static final String ERROR_FILE_UPLOAD = "An error occurred to the file upload process.";
private static final String ERROR_NO_FILE_UPLOAD = "No file is present for upload process.";
private static final String ERROR_INVALID_CALLBACK = "Invalid callback.";
private static final String CKEDITOR_CONTENT_TYPE = "text/html; charset=UTF-8";
private static final String CKEDITOR_HEADER_NAME = "Cache-Control";
private static final String CKEDITOR_HEADER_VALUE = "no-cache";
private static final Pattern PATTERN = Pattern.compile("[\\w\\d]*");
private String errorMessage = "";
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
LOG.info("doGet çalışmaya başladı.");
doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
LOG.info("doPost çalışmaya başladı.");
final ApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(request.getSession()
.getServletContext());
final UploadedFileService uploadedFileService = (UploadedFileService) ctx
.getBean("UploadedFileService");
final UploadedFile uploadedFile = new UploadedFile();
final PrintWriter out = response.getWriter();
response.setContentType(CKEDITOR_CONTENT_TYPE);
response.setHeader(CKEDITOR_HEADER_NAME, CKEDITOR_HEADER_VALUE);
final FileItemFactory factory = new DiskFileItemFactory();
final ServletFileUpload upload = new ServletFileUpload(factory);
try {
final List items = upload.parseRequest(request);
if (!items.isEmpty() && items.get(0) != null) {
uploadedFile.setContent(((DiskFileItem) items.get(0)).get());
uploadedFile.setContentType(((DiskFileItem) items.get(0))
.getContentType());
uploadedFile.setName(((DiskFileItem) items.get(0)).getName());
uploadedFileService.saveUploadedFile(uploadedFile);
} else {
errorMessage = ERROR_NO_FILE_UPLOAD;
}
} catch (final FileUploadException e) {
errorMessage = ERROR_FILE_UPLOAD;
LOG.error(errorMessage, e);
}
// CKEditorFuncNum Is the location to display when the callback
String callback = request.getParameter("CKEditorFuncNum");
// verify if the callback contains only digits and letters in order to
// avoid vulnerability on parsing parameter
if (!PATTERN.matcher(callback).matches()) {
callback = "";
errorMessage = ERROR_INVALID_CALLBACK;
}
final String pathToFile = request.getContextPath()
+ "/ckeditor/getimage?imageId=" + uploadedFile.getId();
out.println("<script type='text/javascript'>// <![CDATA[window.parent.CKEDITOR.tools.callFunction("
+ callback + ",'" + pathToFile + "','" + errorMessage + "')");
out.println("// ]]></script>");
out.flush();
out.close();
}
}
我使用这个 servlet 从数据库中获取和查看图像。
public class CKEditorGetImageServlet extends HttpServlet {
private static final long serialVersionUID = -7570633768412575697L;
private static final Logger LOG = LoggerFactory
.getLogger(CKEditorGetImageServlet.class);
private static final String ERROR_FILE_DOWNLOAD = "An error occured when trying to get the image with id:";
private static final String IMAGE_PARAMETER_NAME = "imageId";
private static final long CACHE_AGE_MILISECONDS_TWO_WEEKS = 1209600000;
private static final String CKEDITOR_CONTENT_EXPIRE = "Expires";
private static final String CKEDITOR_CONTENT_TYPE = "Content-Type";
private static final String CKEDITOR_CONTENT_LENGTH = "Content-Length";
private static final String CKEDITOR_CONTENT_DISPOSITION = "Content-Disposition";
private static final String CKEDITOR_CONTENT_DISPOSITION_VALUE = "inline; filename=\"";
private static final String CKEDITOR_HEADER_NAME = "Cache-Control";
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Long imageId = null;
final ApplicationContext ctx = WebApplicationContextUtils
.getWebApplicationContext(request.getSession()
.getServletContext());
final UploadedFileService uploadedFileService = (UploadedFileService) ctx
.getBean("UploadedFileService");
final String parameterId = request.getParameter(IMAGE_PARAMETER_NAME);
try {
imageId = Long.valueOf(parameterId);
final UploadedFile uploadedFile = uploadedFileService
.getUploadedFileById(imageId);
if (uploadedFile != null && uploadedFile.getContent().length > 0) {
final byte[] rb = uploadedFile.getContent();
final long expiry = new Date().getTime()
+ CACHE_AGE_MILISECONDS_TWO_WEEKS;
response.setDateHeader(CKEDITOR_CONTENT_EXPIRE, expiry);
response.setHeader(CKEDITOR_HEADER_NAME, "max-age="
+ CACHE_AGE_MILISECONDS_TWO_WEEKS);
response.setHeader(CKEDITOR_CONTENT_TYPE,
uploadedFile.getContentType());
response.setHeader(CKEDITOR_CONTENT_LENGTH,
String.valueOf(rb.length));
response.setHeader(
CKEDITOR_CONTENT_DISPOSITION,
CKEDITOR_CONTENT_DISPOSITION_VALUE
+ uploadedFile.getName() + "\"");
response.getOutputStream().write(rb, 0, rb.length);
response.getOutputStream().flush();
response.getOutputStream().close();
}
} catch (final Exception e) {
response.getOutputStream().close();
LOG.error(ERROR_FILE_DOWNLOAD + parameterId, e);
}
}
}
我用ckeditor图像上传到db.OK。但是当我点击“图像信息”选项卡时,我应该手动写/(contextPath)/ckeditor/getimage?imageId=(数据库中的图像ID)(example.when将此 url:/contextPath/ckeditor/getimage?imageId=1 写入图像信息选项卡中的 url 输入,一切正常。但是我想在向服务器发送您的自定义图像时自动写入 url。我该怎么做?手册对用户没有用。如何自动设置为 url(图像信息选项卡)?提前谢谢..
我使用 primefaces-extensions ckeditor。
【问题讨论】:
标签: jsf ckeditor primefaces-extensions