大家好,又见面了,我是你们的朋友全栈君。
CKFinder官网下载地址提供了PHP,ASP, Java等不同下载版本。
1)config.xml放到WEB-INF下,
2)其他文件放到Webcontent下,
3)配置web.xml,
xml
<!--CKFinder -->
<servlet>
<servlet-name>ConnectorServlet</servlet-name>
<servlet-class>com.ckfinder.connector.ConnectorServlet</servlet-class>
<init-param>
<param-name>XMLConfig</param-name>
<param-value>/WEB-INF/ckfinder.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ConnectorServlet</servlet-name>
<url-pattern><!--根据ckfinder文件所在文件夹决定,我的ckfinder放到webcontent下的view文件夹下-->
/view/ckfinder/core/connector/java/connector.java
</url-pattern>
</servlet-mapping>
<filter>
<filter-name>FileUploadFilter</filter-name>
<filter-class>com.ckfinder.connector.FileUploadFilter</filter-class>
<init-param>
<param-name>sessionCookieName</param-name>
<param-value>JSESSIONID</param-value>
</init-param>
<init-param>
<param-name>sessionParameterName</param-name>
<param-value>jsessionid</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>FileUploadFilter</filter-name>
<url-pattern><!--根据ckfinder文件所在文件夹决定,我的ckfinder放到webcontent下的view文件夹下-->
/view/ckfinder/core/connector/java/connector.java
</url-pattern>
</filter-mapping>
<!-- Userfiles download -->
<servlet>
<servlet-name>UserfilesDownloadServlet</servlet-name><!--文件下载需要对应后台的Servlet-->
<servlet-class>com.ckfinder.UserfilesDownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserfilesDownloadServlet</servlet-name><!--此处由config.xml的baseURl决定路径-->
<url-pattern>/userfiles/*</url-pattern>
</servlet-mapping>
4) 配置config.xml
xml
<config>
<enabled>true</enabled>
<baseDir></baseDir>
<baseURL>/userfiles/</baseURL>
<licenseKey></licenseKey>
<licenseName></licenseName>
<imgWidth>1024</imgWidth>
<imgHeight>768</imgHeight>
<imgQuality>80</imgQuality>
<uriEncoding>UTF-8</uriEncoding>
<forceASCII>false</forceASCII>
<disallowUnsafeCharacters>false</disallowUnsafeCharacters>
<userRoleSessionVar>CKFinder_UserRole</userRoleSessionVar>
<checkDoubleExtension>true</checkDoubleExtension>
<checkSizeAfterScaling>true</checkSizeAfterScaling>
<secureImageUploads>true</secureImageUploads>
<htmlExtensions>html,htm,xml,js</htmlExtensions>
<hideFolders>
<folder>.svn</folder>
<folder>CVS</folder>
</hideFolders>
<hideFiles>
<file>.*</file>
</hideFiles>
<defaultResourceTypes></defaultResourceTypes>
<types>
<type name="Files"><!--java-util是我的项目工程名-->
<url>/java-util%BASE_URL%files/</url>
<directory>%BASE_DIR%files</directory>
<maxSize>5M</maxSize>
<allowedExtensions>7z,aiff,asf,avi,bmp,csv,doc,docx,fla,flv,gif,gz,gzip,jpeg,jpg,mid,mov,mp3,mp4,mpc,mpeg,mpg,ods,odt,pdf,png,ppt,pptx,pxd,qt,ram,rar,rm,rmi,rmvb,rtf,sdc,sitd,swf,sxc,sxw,tar,tgz,tif,tiff,txt,vsd,wav,wma,wmv,xls,xlsx,zip
</allowedExtensions>
<deniedExtensions></deniedExtensions>
</type>
<type name="Images">
<url>%BASE_URL%images/</url>
<directory>%BASE_DIR%images</directory>
<maxSize>0</maxSize>
<allowedExtensions>bmp,gif,jpeg,jpg,png</allowedExtensions>
<deniedExtensions></deniedExtensions>
</type>
<type name="Flash">
<url>%BASE_URL%flash/</url>
<directory>%BASE_DIR%flash</directory>
<maxSize>0</maxSize>
<allowedExtensions>swf,flv</allowedExtensions>
<deniedExtensions></deniedExtensions>
</type>
</types>
<accessControls>
<accessControl>
<role>*</role>
<resourceType>*</resourceType>
<folder>/</folder>
<folderView>true</folderView>
<folderCreate>true</folderCreate>
<folderRename>true</folderRename>
<folderDelete>true</folderDelete>
<fileView>true</fileView>
<fileUpload>true</fileUpload>
<fileRename>true</fileRename>
<fileDelete>true</fileDelete>
</accessControl>
</accessControls>
<thumbs>
<enabled>true</enabled>
<url>%BASE_URL%_thumbs/</url>
<directory>%BASE_DIR%_thumbs</directory>
<directAccess>false</directAccess>
<maxHeight>100</maxHeight>
<maxWidth>200</maxWidth>
<quality>80</quality>
</thumbs>
<plugins>
<plugin>
<name>imageresize</name>
<class>com.ckfinder.connector.plugins.ImageResize</class>
<params>
<param name="smallThumb" value="90x90"></param>
<param name="mediumThumb" value="120x120"></param>
<param name="largeThumb" value="180x180"></param>
</params>
</plugin>
<plugin>
<name>fileeditor</name>
<class>com.ckfinder.connector.plugins.FileEditor</class>
<params></params>
</plugin>
</plugins>
<basePathBuilderImpl>com.ckfinder.connector.configuration.ConfigurationPathBuilder</basePathBuilderImpl>
</config>
5)后台下载文件需要的Servlet
java
package com.ckfinder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.util.UriUtils;
import com.ckfinder.connector.ServletContextFactory;
/** * 查看CK上传的图片 * @author ThinkGem * @version 2014-06-25 */
public class UserfilesDownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Logger logger = LoggerFactory.getLogger(getClass());
private String USERFILES_BASE_URL = "/userfiles/";
public void fileOutputStream(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String filepath = req.getRequestURI();
int index = filepath.indexOf(USERFILES_BASE_URL);
if(index >= 0) {
filepath = filepath.substring(index + USERFILES_BASE_URL.length());
}
try {
filepath = UriUtils.decode(filepath, "UTF-8");
} catch (UnsupportedEncodingException e1) {
logger.error(String.format("解释文件路径失败,URL地址为%s", filepath), e1);
}
File file = new File(getUserfilesBaseDir() + USERFILES_BASE_URL + filepath);
try {
FileCopyUtils.copy(new FileInputStream(file), resp.getOutputStream());
resp.setHeader("Content-Type", "application/octet-stream");
return;
} catch (FileNotFoundException e) {
req.setAttribute("exception", new FileNotFoundException("请求的文件不存在"));
req.getRequestDispatcher("/WEB-INF/views/error/404.jsp").forward(req, resp);
}
}
private String getUserfilesBaseDir() {
String path = "";
try {
path = ServletContextFactory.getServletContext().getRealPath("/");
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
fileOutputStream(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
fileOutputStream(req, resp);
}
}
6) jar包引入 方法一:pom.xml
xml
<!-- CKFinder begin -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.2</version>
</dependency>
<dependency>
<groupId>com.ckfinder</groupId>
<artifactId>apache-ant-zip</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.ckfinder</groupId>
<artifactId>ckfinder</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.ckfinder</groupId>
<artifactId>ckfinderplugin-fileeditor</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.ckfinder</groupId>
<artifactId>ckfinderplugin-imageresize</artifactId>
<version>2.3</version>
</dependency>
<!-- CKFinder end -->
方法二: URL链接,jar包下载
运行ckfinder文件夹下–>_samples–>standalone.html文件,出现下面界面。
OK!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/203653.html原文链接:https://javaforall.cn