⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fckeditorconnectorservlet.java

📁 新技术论坛系统 v1.0 前后台管理的初始用户名 : admin 密码 123456
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		if (debug)
			System.out.println("--- BEGIN Connector DOPOST ---");

		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");

		String retVal = "0";
		String newName = "";
		
		// edit check user uploader file size
		int contextLength = request.getContentLength();
        int fileSize = (int)(((float)contextLength)/((float)(1024)));
        PrintWriter out = response.getWriter();
        
		User user = (User) HttpUtil.getAttribute(request.getSession(),
				Symbols.SESSION_USER);
		Role role = null;
		String userId = "0";
		if (user == null) {
			// 用户未登录(可能是Guest用户,也可能是由于浏览器原因)
			userId = HttpUtil.getCookieValue(request, Symbols.COOKIE_FCKEDITOR);
			if(userId==null){
				if(logger.isDebugEnabled()){
					logger.debug("Guest组用户进行上传操作...");
				}
				role = RoleSingleton.getInstance().getRole("0");
			}
			else{
				UserService userService = (UserService)BeanFactory.getInstance(getServletContext()).getBean("userService");
				user = userService.getUser(userId);
				role = RoleSingleton.getInstance().getRole(user.getRoles());
			}
		} else {
			userId = user.getId().toString();
			role = RoleSingleton.getInstance().getRole(user.getRoles());
		}
		
		if(logger.isDebugEnabled()){
			logger.debug("用户允许上传的文件大小 : " + role.getPermissionMap().get("uploadAttachmentSize"));
		}		
        
        if(fileSize<Integer.parseInt((String)role.getPermissionMap().get("uploadAttachmentSize"))){
		
			String commandStr = request.getParameter("Command");
			String typeStr = request.getParameter("Type");
			String currentFolderStr = request.getParameter("CurrentFolder");
	
			// edit begin +++++++++++++++++
			/*
			 * String currentPath=baseDir+typeStr+currentFolderStr; String
			 * currentDirPath=getServletContext().getRealPath(currentPath);
			 */
			String currentPath = baseDir + userId + "/" + typeStr
					+ currentFolderStr;
			String currentDirPath = getServletContext().getRealPath(currentPath);
			// create user dir
			FileUtil.makeDirectory(getServletContext().getRealPath("/"),
					currentPath);
			// edit end ++++++++++++++++
	
			if (debug)
				System.out.println(currentDirPath);
	
			if (!commandStr.equals("FileUpload"))
				retVal = "203";
			else {
				DiskFileUpload upload = new DiskFileUpload();
				try {
					List items = upload.parseRequest(request);
	
					Map fields = new HashMap();
	
					Iterator iter = items.iterator();
					while (iter.hasNext()) {
						FileItem item = (FileItem) iter.next();
						if (item.isFormField())
							fields.put(item.getFieldName(), item.getString());
						else
							fields.put(item.getFieldName(), item);
					}
					FileItem uplFile = (FileItem) fields.get("NewFile");
					String fileNameLong = uplFile.getName();
					fileNameLong = fileNameLong.replace('\\', '/');
					String[] pathParts = fileNameLong.split("/");
					String fileName = pathParts[pathParts.length - 1];
	
					String nameWithoutExt = getNameWithoutExtension(fileName);
					String ext = getExtension(fileName);
					File pathToSave = new File(currentDirPath, fileName);
					if(FckeditorUtil.extIsAllowed(allowedExtensions,deniedExtensions,typeStr,ext)) {
						int counter = 1;
						while (pathToSave.exists()) {
							newName = nameWithoutExt + "(" + counter + ")" + "." + ext;
							retVal = "201";
							pathToSave = new File(currentDirPath, newName);
							counter++;
						}
						uplFile.write(pathToSave);
						// 记录上传的信息
						if(logger.isInfoEnabled()){
							logger.info("记录用户上传的文件...");
						}
						AttachmentService attachmentService = (AttachmentService)BeanFactory.getInstance(getServletContext()).getBean("attachmentService");
						try{
							Attachment attachment = new Attachment();
							attachment.setOriginFilename(fileName);
							attachment.setFilename(fileName);
							attachment.setFilesize(1);
							attachment.setFiletype(typeStr);
							attachment.setFolder(currentPath);
							attachment.setComments(" ");
							attachment.setDateCreated(new Date());
							attachment.setUserId(Integer.parseInt(userId));
							attachmentService.createAttachment(attachment);
						}
						catch(ServiceException se){
							logger.error("添加附件到库失败");
							retVal="203";
						}					
					}
					else {
						retVal="202";
						if (debug) System.out.println("Invalid file type: " + ext);	
					}
				} catch (Exception ex) {
					retVal = "203";
				}
			}
        }
        else{
        	retVal = "204";
        }
		out.println("<script type=\"text/javascript\">");
		out.println("window.parent.frames['frmUpload'].OnUploadCompleted("
				+ retVal + ",'" + newName + "');");
		out.println("</script>");
		out.flush();
		out.close();

		if (debug)
			System.out.println("--- END DOPOST ---");

	}

	private void setCreateFolderResponse(String retValue, Node root,
			Document doc) {
		Element myEl = doc.createElement("Error");
		myEl.setAttribute("number", retValue);
		root.appendChild(myEl);
	}

	private void getFolders(File dir, Node root, Document doc) {
		Element folders = doc.createElement("Folders");
		root.appendChild(folders);
		File[] fileList = dir.listFiles();
		for (int i = 0; i < fileList.length; ++i) {
			if (fileList[i].isDirectory()) {
				Element myEl = doc.createElement("Folder");
				myEl.setAttribute("name", fileList[i].getName());
				folders.appendChild(myEl);
			}
		}
	}

	private void getFiles(File dir, Node root, Document doc) {
		Element files = doc.createElement("Files");
		root.appendChild(files);
		File[] fileList = dir.listFiles();
		for (int i = 0; i < fileList.length; ++i) {
			if (fileList[i].isFile()) {
				Element myEl = doc.createElement("File");
				myEl.setAttribute("name", fileList[i].getName());
				myEl.setAttribute("size", "" + fileList[i].length() / 1024);
				files.appendChild(myEl);
			}
		}
	}

	private Node CreateCommonXml(Document doc, String commandStr,
			String typeStr, String currentPath, String currentUrl) {

		Element root = doc.createElement("Connector");
		doc.appendChild(root);
		root.setAttribute("command", commandStr);
		root.setAttribute("resourceType", typeStr);

		Element myEl = doc.createElement("CurrentFolder");
		myEl.setAttribute("path", currentPath);
		myEl.setAttribute("url", currentUrl);
		root.appendChild(myEl);

		return root;

	}

	/**
	 * Helper function to convert the configuration string to an ArrayList.
	 */

	private ArrayList stringToArrayList(String str) {
		if (debug)
			System.out.println(str);
		String[] strArr = str.split("\\|");

		ArrayList tmp = new ArrayList();
		if (str.length() > 0) {
			for (int i = 0; i < strArr.length; ++i) {
				if (debug)
					System.out.println(i + " - " + strArr[i]);
				tmp.add(strArr[i].toLowerCase());
			}
		}
		return tmp;
	}

	/*
	 * This method was fixed after Kris Barnhoorn (kurioskronic) submitted SF
	 * bug #991489
	 */
	private static String getNameWithoutExtension(String fileName) {
		return fileName.substring(0, fileName.lastIndexOf("."));
	}

	/*
	 * This method was fixed after Kris Barnhoorn (kurioskronic) submitted SF
	 * bug #991489
	 */
	private String getExtension(String fileName) {
		return fileName.substring(fileName.lastIndexOf(".") + 1);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -