-1

我已经在 Android 中开发了一段时间的应用程序,现在我需要从这个应用程序将图像上传到服务器。问题是我真的不知道如何在后端(这是服务器端)执行此操作。我已经阅读了很多关于通过httpmime库发送图像和使用multipart的信息,但没有太多关于接收图像的服务器端服务。

我需要使用 MVC3 或 WCF 之类的东西,因为我们这里的服务器是 IIS,我还没有找到太多关于如何使用这个模型来做的事情。因此,我需要任何有关如何使用此模型完成此任务的教程或指南,任何帮助也将不胜感激。

4

1 回答 1

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!";
                   }
               }
于 2012-06-15T22:20:50.367 回答