javafile-uploadstruts2ckeditorstruts2-jquery

How to intercept an image file uploading with CKEditor in Struts 2


I have a CKEditor on different pages of the site, so I settled up the uploads to true and all the configuration stuff for it to work, and the image upload worked just fine for "Send it to the Server" tab. But from here I want to intercept or interact with upload function to be able to upload images not to a folder, but into database with some flags like "page" where is image uploaded, "category", "cropping" and some another manipulations with them.

So how could it be done to intercept this uploads?

I was curious and I found that the default action for uploads is

/%project_name%/ckeditor/upload.html?CKEditor=textarea_1457018535&CKEditorFuncNum=2&langCode=en 

and the action for this uploads is CkeditorUpload.java, but I don't realize how can I use them further.


Solution

  • Well, if you'd like to override it you should start overriding the method execute from the action class above.

    Also override the configuration by replacing the class reference in the action configuration. The point is where the original implementation creates FileOutputStream should be replaced to ByteArrayOutputStream. Then let it write to the byte array stream and getBytes() from it will give you byte[] data. Save that data to the database.

    You may also read this answer will help you to do it.

    This tutorial will explain you how to do it with Hibernate.

    Then the code where the action writes HTML change the disk path to the action path /uploads mapping to the action which retrieves the image from the database. Example of the ImageAction you can find here.

    This answer will also be helpful.