I upload image to db in ckeditor.with below config:
CKEDITOR.editorConfig = function(config) {
var contextPath= $(.contextPath).html();
config.filebrowserImageBrowseUrl =contextPath+ '/ckeditor/getimage';
config.filebrowserImageUploadUrl = contextPath+'ckeditor/uploadimage';
config.filebrowserBrowseUrl = contextPath+'/ckeditor/getimage';
};
I use this servlet for upload to db.
@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();
}
}
and I use this servlet for get and view image from db.
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);
}
}
}
I upload with ckeditor image to db.OK.But when I click the 'image info' tab,I should write manually /(contextPath)/ckeditor/getimage?imageId=(image id in the db)(example.when the write this url:/contextPath/ckeditor/getimage?imageId=1 to url's input in image info tab,all is ok.But I want to write to url automatically when the send the server your custom image.How can I do that? manual is not useful for users.How can I set to url(image info tab) automatically?Thanks in advance..
I use primefaces-extensions ckeditor.