KindEditor 4.x 文件上传与文件空间 java语言演示程序

官方没有给Java的Demo,这次用到看这文档与PHP的Demo改了下,记录以备后面使用,没有文件排序的功能,在图片空间选择的地址不是完整的URL,可在KindEditor的js文件中把url = url.substr(host.length);注释:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.io.FilenameUtils;

import com.jfinal.core.Path;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.TimeKit;
import com.jfinal.upload.UploadFile;

/**
 * 编辑器上传文件控制器
 * @ClassName: KindEditorUploadController 
 */
@Path(value = "/admin/editor")
public class KindEditorUploadController extends AdminParentController{
	/**
	 * 上传接口
	 */
	public void upload() {
		String dir = get("dir");//image flash media file
		String folder = dir+"/"+TimeKit.format(new Date(), "yyyy");
		UploadFile uploadFile = getFile("imgFile", folder);
		set("error", 0).set("url", getRequest().getScheme()+"://"+getRequest().getServerName()+"/upload/"+folder+"/"+uploadFile.getFileName());
		renderJson();
	}
	
	/**
	 * 文件空间
	 */
	public void filePath() {
		Map<String, Object> res = new HashMap<>();
        //获取图片文件夹中的数据
        File file = new File(PathKit.getWebRootPath()+"/upload/"+get("dir")+"/"+get("path",""));
        File[] files = file.listFiles();
        //相对于根目录的上一级目录
        int positon = get("path","").length() > 0 && get("path").substring(0, get("path").length() - 1).indexOf("/") != -1 
        		? get("path").substring(0, get("path").length() - 1).lastIndexOf("/") : 0;
        
        res.put("moveup_dir_path", get("path","").substring(0, positon)+"/");
        //相对于根目录的当前目录
        res.put("current_dir_path", get("path",""));
        //设置数量
        res.put("total_count", files.length);
        //设置url
        String url = getRequest().getScheme()+"://"+getRequest().getServerName()+"/upload/"+get("dir")+"/"+get("path","");
        System.out.println(url);
        res.put("current_url", url);
        //设置集合人数据
        List<Object> list = new ArrayList<>();
        for (File f : files) {
        	Map<String, Object> map = new HashMap<>();
        	if(f.isDirectory()) {
        		map.put("is_dir",true);
                map.put("is_photo",false);
                map.put("has_file",f.list().length>0);
            }else {
        		map.put("is_dir",false);
                map.put("is_photo",isPhoto(f));
                map.put("has_file",false);
            }
        	 map.put("datetime",getFileTime(file.toPath()));
        	 map.put("filetype", FilenameUtils.getExtension(f.getName()));
        	 map.put("filesize",f.length());
        	 map.put("filename",f.getName());
        	 list.add(map);
        }
        res.put("file_list", list);
        renderJson(res);
	}
	
	/**
	 * 获取文件创建时间
	 * @param path
	 * @return
	 */
	private String getFileTime(java.nio.file.Path path) {
		BasicFileAttributes attributes = null;
		try {
			attributes = Files.readAttributes(path, BasicFileAttributes.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Instant instant = attributes.creationTime().toInstant();
        // 更新时间
        // Instant instant = attr.lastModifiedTime().toInstant();
        // 上次访问时间
        // nstant instant = attr.lastAccessTime().toInstant();
        String format = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.systemDefault()).format(instant);
        return format;
	}
	
	/**
	 * 判断文件是否是图片
	 * @param file
	 * @return
	 */
	private boolean isPhoto(File file) {
		FileInputStream in = null;
		try {
			in = new FileInputStream(file);
			BufferedImage image = ImageIO.read(in);
			return image != null;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return false;
		
	}
}

猜你喜欢

转载自blog.csdn.net/ren365880/article/details/129045922
今日推荐