2012-06-15 147 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

相关问题