1. 程式人生 > >spring學習遇到的問題和解決,eclipse使用2

spring學習遇到的問題和解決,eclipse使用2

7. 國際化,生成漢字的編碼

使用properties類的時候,也就是在國際化的時候需要漢字的ascii編碼,Java的jdk中有一個exe檔案可以使用。
在jdk檔案的bin目錄下有native2ascii.exe
在這裡插入圖片描述已經將它的快捷鍵發到了桌面的一個資料夾下,檔案中的1.txt中是漢字內容,等待被編譯成ascii。win+R開啟dos視窗。
到達工具位置:
cd C:\Users\lei02\Desktop\參考
將編譯的檔案存到temp.txt檔案中:
native2ascii 1.txt temp.txt
在這裡插入圖片描述完成

8. 錯誤:Syntax error, annotations are only available if source level is 1.5 or greater

意思就是版本太低,然而使用的是1.8,並不是1.5,這時候需要改一下編譯版本
在這裡插入圖片描述在這裡插入圖片描述更改右邊的版本資訊
在這裡插入圖片描述最後確認應用就好了

9. 異常

Could not publish server configuration for Tomcat v9.0 Server at localhost. Multiple Contexts have a path of “/chapter7_upload”.
意思就是在說是有多個上下文路徑,導致衝突
在這裡插入圖片描述解決方法,在啟動的時候,點選next把不想關的應用remove掉,就好了

10. 檔案上傳

兩個方面的,一個書上傳檔案的路徑,一個是上傳檔案的檔案型別。
這個控制器完成的工作是,把使用者上傳的頭像儲存下來,並使用使用者的使用者名稱和檔案的字尾名給上傳檔案命名。
值得注意的是,文中的request.getServletContext().getRealPahth(“/images”);的內容並不是固定的,因為eclipse允許使用者把web程式釋出到tomcat中釋出,也可以在eclipse內部發布。
點選server,保證所有使用tomcat的程式都關閉,可以在tomcat-server點選右鍵,執行clea
在這裡插入圖片描述

然後雙擊server,選擇web釋出方式。第二個選型就是在tomcat中釋出,也就是將工程讀取到webapp目錄下。
在這裡插入圖片描述也可以選擇OverView將裡面執行的工程結束掉。

@RequestMapping(value="/upload", method=RequestMethod.POST)
	public String upload(HttpServletRequest request,
			@RequestParam("file") MultipartFile file,
			Model model) {
		model.addAttribute("user", this.recorduser);
		//如果檔案不為空,寫入上傳路徑
		if(!file.isEmpty()) {
			//首先確定檔案的父類路徑
			//String path = "E:\\eclipse-workspace\\chapter7_LoginModel\\WebContent\\images\\";
			String path = request.getServletContext().getRealPath("/images");
			//獲取上傳檔案的原名,主要是用來獲得使用者的字尾名
			String filename = file.getOriginalFilename();
			//獲取使用者名稱
			String username = this.recorduser.getUsername();
			//使用使用者名稱和原始檔的字尾名,命名上傳的頭像
			filename = username + filename.substring(filename.indexOf("."));
			File filepath = new File(path, filename);
			//判斷路徑是否存在,如果不存在就建立一個
			if(!filepath.getParentFile().exists()) {
				filepath.mkdirs();
			}
			//將檔案儲存在一個目標檔案中
			try {
				file.transferTo(new File(path + File.separator + filename));
				//修改成功之後,應該將recordUser的頭像儲存路徑也修改
				this.recorduser.setImage_head(path + File.separator + filename);
				//檢視頭像儲存的位置
//System.out.println(path + File.separator + filename);
			}catch (IOException ex) {
				ex.printStackTrace();
				return "error";
			}
			//再重新跳轉之後,顯示的介面並沒有更新使用者的資訊

			return "changeInfoForm";
		}
		else
			return "error";
	}

對於檔案型別,可以使用攔截器進行攔截判斷。