2012-06-15 145 views
-1

我一直在Android開發應用程序一段時間,現在我需要從這個應用程序上傳圖像到服務器。事情是,我真的不知道如何在後端(這是服務器端)做到這一點。我讀過很多關於通過httpmime庫和使用multipart發送圖像,但沒有太多關於接收圖像的服務器端服務。Android - 從應用程序上傳圖像到IIS服務器

我需要使用像MVC3或WCF的東西,因爲我們在這裏的服務器是IIS,我還沒有找到很多關於如何使用此模型來做到這一點。因此,任何有關如何使用此模型實現該功能的教程或指南都是我所需要的,任何幫助也都將得到真正的讚賞。

+0

稍等片刻。你在談論ASP.NET MVC 3和WCF數據服務。至少你已經在你的問題中使用了這些標籤。哪些應該從客戶端(Android)應用程序接收圖像?一旦收到服務器上的圖像,你想怎麼做? ASP.NET MVC 3應用程序和WCF數據服務之間的關係是什麼? –

+0

好吧,我可以使用任何這些,而不是在同一時間,也是一個休息的Web服務,我忘了提及。是不是我想要同時使用它們,只是使用它們中的任何一個,因爲這些是我們在這裏需要的。還要補充一點,我想要做的就是接收從Android設備上傳的圖片並將其保存到服務器上的一個文件夾中,只要 – DeX03

回答

1

你可以發表你的文件低谷HTTP POST

public String sendFilePost(String urlServer, String pathToOurFile){ 
        HttpURLConnection connection = null; 
        DataOutputStream outputStream = null; 
        DataInputStream inputStream = null; 
        String lineEnd = "\r\n"; 
        String twoHyphens = "--"; 
        String boundary = "*****"; 
        int bytesRead, bytesAvailable, bufferSize; 
        byte[] buffer; 
        int maxBufferSize = 1*1024*1024; 
        try 
        { 
        FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile)); 
        URL url = new URL(urlServer); 
        connection = (HttpURLConnection) url.openConnection(); 
        connection.setDoInput(true); 
        connection.setDoOutput(true); 
        connection.setUseCaches(false); 
        connection.setRequestMethod("POST"); 
        connection.setRequestProperty("Connection", "Keep-Alive"); 
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

        outputStream = new DataOutputStream(connection.getOutputStream()); 
        outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
        outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd); 
        outputStream.writeBytes(lineEnd); 

        bytesAvailable = fileInputStream.available(); 
        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        buffer = new byte[bufferSize]; 

        // Read file 
        bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

        while (bytesRead > 0) 
        { 
        outputStream.write(buffer, 0, bufferSize); 
        bytesAvailable = fileInputStream.available(); 
        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
        } 

        outputStream.writeBytes(lineEnd); 
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

        // Responses from the server (code and message) 
        String serverResponseMessage = connection.getResponseMessage(); 
        InputStream is = connection.getInputStream(); 
        BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
        String line; 
        StringBuffer response = new StringBuffer(); 
        while((line = rd.readLine()) != null) { 
         response.append(line); 
        } 
        String rrrr = response.toString(); 
        rd.close(); 
        fileInputStream.close(); 
        outputStream.flush(); 
        outputStream.close(); 
        return rrrr; 
        } 
        catch (Exception ex) 
        { 
         return "Something went wrong!"; 
        } 
       } 
+0

如果這個android端?最重要的是o不知道如何去做另一面 – DeX03

相關問題