android 開發上傳圖片遇到返回 FileNotFoundException

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.