KinderEditor上傳圖片方式:將上傳到tomcat中間件的webapps文件夾中改成上傳到存儲服務器上

1.首先將保存圖片的路徑進行修改html

public class Test extends AAA{
	@Override
	protected String admin() {
		HttpServletResponse resp = ServletActionContext.getResponse();
		HttpServletRequest req = ServletActionContext.getRequest();
		// 設置Response響應的編碼
		resp.setContentType("text/html; charset=UTF-8");

		// 獲取response、request對象
		ActionContext ac = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) ac
				.get(ServletActionContext.HTTP_RESPONSE);
		HttpServletRequest request = (HttpServletRequest) ac
				.get(ServletActionContext.HTTP_REQUEST);

		PrintWriter out = null; // 輸出流
		try {
			out = response.getWriter();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
                //原路徑
                /*String savePath = ServletActionContext.getServletContext().getRealPath(
				"/")
				+ "file/";*/
                //修改成
		String savePath = "/file/images/";

		// 文件保存目錄URL
                //原路徑
		//String saveUrl = request.getContextPath() + "/attached/";

		//現路徑
		String saveUrl = "servlet/GetSlidePictureServlet?filepath=/file/images/";
		// 定義容許上傳的文件擴展名
		HashMap<String, String> extMap = new HashMap<String, String>();
		extMap.put("image", "gif,jpg,jpeg,png,bmp");
		extMap.put("flash", "swf,flv");
		extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
		extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");

		// 最大文件大小
		long maxSize = 1000000;

		response.setContentType("text/html; charset=UTF-8");

		if (!ServletFileUpload.isMultipartContent(request)) {
			out.println(getError("請選擇文件。"));
			return null;
		}
		// 檢查目錄
		File uploadDir = new File(savePath);
		if (!uploadDir.isDirectory()) {
			out.println(getError("上傳目錄不存在。"));
			return null;
		}
		// 檢查目錄寫權限
		if (!uploadDir.canWrite()) {
			out.println(getError("上傳目錄沒有寫權限。"));
			return null;
		}

		String dirName = request.getParameter("dir");
		if (dirName == null) {
			dirName = "image";
		}
		if (!extMap.containsKey(dirName)) {
			out.println(getError("目錄名不正確。"));
			return null;
		}
		// 建立文件夾
		savePath += dirName + "/";
		saveUrl += dirName + "/";
		File saveDirFile = new File(savePath);
		if (!saveDirFile.exists()) {
			saveDirFile.mkdirs();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String ymd = sdf.format(new Date());
		savePath += ymd + "/";
		saveUrl += ymd + "/";
		File dirFile = new File(savePath);
		if (!dirFile.exists()) {
			dirFile.mkdirs();
		}

		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("UTF-8");
		MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) request;
		// 得到上傳的文件名
		String fileName = wrapper.getFileNames("uploadFile")[0];// imgFile,imgFile,imgFile
		// 得到文件過濾器
		File file = wrapper.getFiles("uploadFile")[0];

		// 檢查擴展名
		String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1)
				.toLowerCase();
		if (!Arrays.<String> asList(extMap.get(dirName).split(",")).contains(
				fileExt)) {
			out.println(getError("上傳文件擴展名是不容許的擴展名。\n只容許" + extMap.get(dirName)
					+ "格式。"));
			return null;
		}
		// 檢查文件大小
		if (file.length() > maxSize) {
			out.println(getError("上傳文件大小超過限制。"));
			return null;
		}

		// 重構上傳圖片的名稱
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String newImgName = df.format(new Date()) + "_"
				+ new Random().nextInt(1000) + "." + fileExt;
		byte[] buffer = new byte[1024];
		// 獲取文件輸出流
		FileOutputStream fos;
		// 獲取內存中當前文件輸入流
		InputStream in;
		try {
			fos = new FileOutputStream(savePath + "/" + newImgName);
			in = new FileInputStream(file);
			int num = 0;
			while ((num = in.read(buffer)) > 0) {
				fos.write(buffer, 0, num);
			}
			in.close();
			fos.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 發送給 kindeditor
		JSONObject obj = new JSONObject();
		obj.put("error", 0);
		obj.put("url", saveUrl + "/" + newImgName);
		out.println(obj.toJSONString());
		return null;
	}

	private String getError(String message) {
		JSONObject obj = new JSONObject();
		obj.put("error", 1);
		obj.put("message", message);
		return obj.toJSONString();
	}

	public static String getFilePostName() {
		return filePostName;
	}

	public static void setFilePostName(String filePostName) {
		KindEditorUpload.filePostName = filePostName;
	}
}

file/image/這個路徑是跟中間件在同一根目錄下java

這樣修改完成以後他就會保存到與中間件同一根目錄下的file/image文件夾下app

並可以成功顯示出來dom

注意:還須要將kinderEditor保存圖片的方式修改一下:將絕對路徑改成相對路徑。ide

相關文章
相關標籤/搜索