標籤:android style blog http io color os sp java
目的:帶有參數上傳圖片
環境:用戶端開發 android studio ,伺服器端:visual studio 2012 (c# asp.net)
************************************************************************
上傳圖片的時候遇到返回FileNotFoundException,但是一直在用戶端報錯
HttpURLConnection中connection.getInputStream()報錯誤FileNotFoundException
java.io.FileNotFoundException: http://192.168.11.6/appHandler/UploadHandler.ashx (一直以為是找不到這個檔案)
所以也一直在尋找用戶端的錯誤,換來換去,還是沒有解決,都快瘋掉了,放了一段時間,今天又重新來補充這一塊的開發,慢慢調試,才發現如果在伺服器端只接受參數,不處理圖片與檔案這一塊,則會正常返回伺服器訊息,這才知道是伺服器端出現問題了,下面把出問題的代碼貼出來:
public string UploadVoiceFile(HttpContext context) { string FileName, FilePath; HttpPostedFile ItemImageFile = context.Request.Files["Filedata"]; FileName = ItemImageFile.FileName.Substring(ItemImageFile.FileName.LastIndexOf(".")); //FileName = Guid.NewGuid().ToString().Substring(24) + DateTime.Now.ToShortDateString().Replace("/", "_") + file.FileName.Substring(file.FileName.LastIndexOf(".")); string FilePathStr = "/upload/voice/" + DateTime.Now.ToShortDateString().Replace("/", "") + "/"; FilePath = HttpContext.Current.Server.MapPath(@FilePathStr); if (ItemImageFile != null) { //如果沒有該目錄則建立該上傳目錄 if (!Directory.Exists(FilePath)) //這一塊就出現問題 { Directory.CreateDirectory(FilePath); } if (ItemImageFile.ContentLength > 2097152) // 圖片大小不能超過2M { return "{\"result\": \"-1\",\"msg\":\"圖片大小不能超過2M!\"}"; } ItemImageFile.SaveAs(FilePath + FileName); return "{\"result\": \"1\",\"msg\":\"" + FilePathStr + FileName + "\"}"; } return "{\"result\": \"-1\",\"msg\":\"檔案資料為空白!\"}"; }
是建立不了目錄,也不能正常儲存檔案,所以拋出FileNotFoundException,也就傳回到用戶端了,以為是找不到檔案:http://192.168.11.6/appHandler/UploadHandler.ashx
網上也有朋友遇到過,說是重建立了一個工程就OK了,我這裡通過伺服器本地調試也是可以的,所以原因出現在 IIS檔案許可權這一塊
找到要上傳的檔案目錄,右鍵->安全 -》 添加 -> Everyone 使用者,許可權:完全控制 ,搞定!
汗顏啊,這一個小小的問題浪費了幾天的開發時間,寫在這裡留作以後筆記,也供大家參考!
android 開發上傳圖片遇到返回 FileNotFoundException