1. 程式人生 > >1、使用富文字編輯器實現檔案的上傳和下載

1、使用富文字編輯器實現檔案的上傳和下載

1、環境配置                                                                                            

需要用到的檔案包:

         ckeditor_3.6.2

         ckeditor_3.6.2

         ckeditor-java-core-3.5.3


1.首先把ckeditor、ckfinder匯入到WebContent目錄下面(注意把樣例_sameple刪除掉)。

2.然後引入jar包:把ckfinder_java_2.4.3/ckfinder/CKFinderJava-2.4.3/WEB-INF/lib目錄下的jar包全部引入到專案,還需要引入一個jar包:在ckeditor-java-core-3.5.3下的ckeditor-java-core-3.5.3.jar包引入。

3.再引入ckfinder_java_2.4.3/ckfinder/CKFinderJava-2.4.3/WEB-INF下的config.xml放到專案的WEB-INF下面。修改裡面的:

<enabled>true</enabled>

    <baseURL>/struts2UpAndDown/userfiles/</baseURL>:這是上傳檔案的儲存路徑

4.把ckfinder_java_2.4.3/ckfinder/CKFinderJava-2.4.3/WEB-INF下的web.xml裡面的servlet配置複製到專案的中web.xml檔案裡面。但在專案中的struts2的配置的對映路徑不能為/*而改為*.action或*.do。或者定義一個過濾器。

5.修改config.js下的專案路徑:

config.filebrowserBrowseUrl =  '/專案名/ckfinder/ckfinder.html' ;  
config.filebrowserImageBrowseUrl =  '/專案名/ckfinder/ckfinder.html?type=Images' ;   
config.filebrowserFlashBrowseUrl =  '/專案名/ckfinder/ckfinder.html?type=Flash' ;  
config.filebrowserUploadUrl =  '/專案名/ckfinder/core/connector/java/connector.java?command=QuickUpload&type=Files' ;  
config.filebrowserImageUploadUrl =  '/專案名/ckfinder/core/connector/java/connector.java?command=QuickUpload&type=Images' ;  
config.filebrowserFlashUploadUrl =  '/專案名/ckfinder/core/connector/java/connector.java?command=QuickUpload&type=Flash' ;  
config.filebrowserWindowWidth = '1000';  
config.filebrowserWindowHeight = '700';  
config.language =  "zh-cn" ;  

這樣富文字編輯器的環境配置完成。


由於富文字編輯器功能太多,可以破解減少功能:


         -------------------------------------------------關於破解------------------------------------------

         圖片預覽文字替換cheditor/config.js

         config.image_previewText=””;

         ---------------------------------------------------------------------

         ckfinder/ckfinder.js

         瀏覽器左下角資訊不展示  下面程式碼註釋

         if(!D)this.dV().getChild(0).appendHtml(A||B||y!=4?t:u+”\x3c\142\x3e”..)

         --------------------------------------------------------------------------

         隱藏版權資訊:

         找到<h4class=’message_content’>

         改為<h4style=’display:none;’class=’message_content’>

2、實現上傳                                                                                                

在頁面中引用:

<script type="text/javascript" src="<%=request.getContextPath() %>/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/ckfinder/ckfinder.js"></script>
 </head>
  <body>
    	<form action="cktest.action" method="post">
    		<textarea rows="10" cols="80" id="editor" name="editor"class="ckeditor">請輸入...</textarea>
    		<input type="submit" value="儲存">	
    	</form>
  </body>

上傳:其實就是在Action裡面定義一個與name相同的名字再set/get就可以了

public class CkAction extends ActionSupport {
	//定義一個接收富文字資訊的字串
	private String editor;
	public String getEditor() {
		return editor;
	}
	public void setEditor(String editor) {
		this.editor = editor;
	}
	@Override
	public String execute() throws Exception {
		System.out.println("富文字資訊" + editor);
//		<a href="/ckscxz/userfiles/files/1421332943720.txt">//這是上傳的附件
		//實現附件的下載
		//擷取以<a標籤開頭的位置
		int beginIndex = editor.indexOf("<a");
		while(beginIndex != -1){
			int endIndex = editor.indexOf(">",beginIndex) + 1;
			String beginStr = editor.substring(0,beginIndex);
			String endStr = editor.substring(endIndex);
			String str = editor.substring(beginIndex, endIndex);
			//擷取需要下載的檔名稱
			String filename = str.substring(str.lastIndexOf("/")+1, str.lastIndexOf("\""));
			//需要下載的附件名稱
			String replace = "<a href=download.action?filename=" + filename + " /a>";
			editor = beginStr + replace + endStr;
			beginIndex = editor.indexOf("<a",endIndex);
		}
		return SUCCESS;
	}
}

3、展示頁面:show.jsp                                                                             

${editor }

4、實現附件下載:                                                                                     

public class FileDownloadAction extends ActionSupport {
	public String inputPath;
	public String filename;
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	public String getInputPath() {
		return inputPath;
	}
	public void setInputPath(String inputPath) {
		this.inputPath = inputPath;
	}
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	public InputStream getInputStream() throws IOException{
		String path = ServletActionContext.getServletContext().getRealPath("/userfiles/files");
		String filepath = path + "\\" + filename;
		File file = new File(filepath);
		return FileUtils.openInputStream(file);
//		return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
	}
	
	public String getDownloadFileName(){
		String downloadFileName ="";
		try {
			downloadFileName = URLEncoder.encode(filename,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return downloadFileName;
	}
}