umeditor上传功能后端与Spring结合,不使用官方jar

后端上传图片处理的Springmvc代码如下

package com.yltoutiao.ent.admin.base.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.yltoutiao.ent.util.Cfg;

@Controller
public class UploadController {
 

	// 清空文件夹方法
	private boolean deleteDirectory(String sPath) {
		// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
		boolean flag = false;
		if (!sPath.endsWith(File.separator)) {
			sPath = sPath + File.separator;
		}
		File dirFile = new File(sPath);
		// 如果dir对应的文件不存在,或者不是一个目录,则退出
		if (!dirFile.exists() || !dirFile.isDirectory()) {
			return false;
		}
		flag = true;
		// 删除文件夹下的所有文件(包括子目录)
		File[] files = dirFile.listFiles();
		for (int i = 0; i < files.length; i++) {
			// 删除子文件
			if (files[i].isFile()) {
				flag = deleteFile(files[i].getAbsolutePath());
				if (!flag)
					break;
			} // 删除子目录
			else {
				flag = deleteDirectory(files[i].getAbsolutePath());
				if (!flag)
					break;
			}
		}
		return flag;
	}

	// 删除文件方法
	public boolean deleteFile(String sPath) {
		boolean flag = false;
		File file = new File(sPath);
		// 路径为文件且不为空则进行删除
		if (file.isFile() && file.exists()) {
			file.delete();
			flag = true;
		}
		return flag;
	}

	@RequestMapping(value = "/jsp/imageUp", method = RequestMethod.POST)
	public @ResponseBody void imageUpload(HttpServletRequest request,
			HttpServletResponse response) {
		try {

			String picPath = "";
			String newPath = "";
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

			// 获取文件
			MultipartFile file = multipartRequest.getFile("upfile");
			if (!file.isEmpty()) {

				String fileName = file.getOriginalFilename();
				String suffix = fileName.substring(fileName.lastIndexOf("."));
				// 对文件重命名
				SimpleDateFormat formatter = new SimpleDateFormat(
						"yyyyMMddHHmmss");
				Random ran = new Random();
				String newFileName = formatter.format(new Date())
						+ String.valueOf(ran.nextInt(100000)) + suffix;
				Integer fileSize = (int) file.getSize() / 1024;
				if (fileSize > 10240) {

				} else {
					File filePath = new File(Cfg.get("filePath") + "/article");
					newPath = Cfg.get("fileUrl") + "/article/" + newFileName;
					if (!filePath.exists()) {
						filePath.mkdirs();
					} else {
						deleteDirectory(filePath.getAbsolutePath());
					}
					file.transferTo(new File(filePath.getAbsolutePath() + "/"
							+ newFileName));
					picPath = filePath.getAbsolutePath() + "/" + newFileName;

				}

				String result = "{\"name\":\"" + newFileName
						+ "\", \"originalName\": \"" + fileName
						+ "\", \"size\": " + file.getSize()
						+ ", \"state\": \"SUCCESS\", \"type\": \"" + suffix
						+ "\", \"url\": \"" + newPath + "\"}";

				result = result.replaceAll("\\\\", "\\\\");

				response.getWriter().print(result);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


package com.yltoutiao.ent.util;

import java.util.Properties;

import org.springframework.core.io.support.PropertiesLoaderUtils;
/**
 * 获取配置参数
 * @author zhongtianyi
 *
 */
public final class Cfg {

	private Cfg() {
	}

	private static Properties props = null;

	static {
		try {
			props = PropertiesLoaderUtils.loadAllProperties("application.properties");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String get(String key) {
		return props.getProperty(key);
	}

	public static String get(String key, String defaultValue) {
		return props.getProperty(key, defaultValue);
	}
}

配置文件里如下:


fileUrl=http://localhost:8080/ent-admin/static/source
filePath=D:/tomcat-8.0.15/webapps/ent-admin/static/source


下面是在config.js里配置路径如下


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值