4

我正在尝试将文件 fmarom android 上传到 Web 服务器,在服务器端使用 aspx,我正在使用我在以下位置找到的示例代码:http: //www.codicode.com/art/upload_files_from_android_to_a_w.aspx 我的问题是我收到此错误消息:java.net.ProtocolException:方法不支持请求正文:POST。

现在我正在寻找这个错误消息,我发现我在服务器端有问题,我的问题的解决方案是通过像这样编辑 Web.config 在服务器上启用 HTTP GET 和 HTTP POST。

<configuration>
    <system.web>
    <webServices>
        <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
        </protocols>
    </webServices>
    </system.web>
</configuration>

但在这一行仍然有相同的错误消息:

DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

这是我的课程代码:

HttpFileUpload.java:

package com.batyalon.tizko;



import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;

public class HttpFileUpload implements Runnable{
        URL connectURL;
        String responseString;
        String Title;
        String Description;
        byte[ ] dataToServer;
        FileInputStream fileInputStream = null;

        HttpFileUpload(String urlString, String vTitle, String vDesc){
                try{
                        connectURL = new URL(urlString);
                        Title= vTitle;
                        Description = vDesc;
                }catch(Exception ex){
                    Log.i("HttpFileUpload","URL Malformatted");
                }
        }

        void Send_Now(FileInputStream fStream){
                fileInputStream = fStream;
                Sending();
        }

        void Sending(){
                String iFileName = "ovicam_temp_vid.mp4";
                String lineEnd = "\r\n";
                String twoHyphens = "--";
                String boundary = "*****";
                String Tag="fSnd";
                try
                {
                        Log.e(Tag,"Starting Http File Sending to URL");

                        // Open a HTTP connection to the URL
                        HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();

                        // Allow Inputs
                        conn.setDoInput(true);

                        // Allow Outputs
                        conn.setDoOutput(true);

                        // Don't use a cached copy.
                        conn.setUseCaches(false);

                        // Use a post method.
                        conn.setRequestMethod("POST");

                        conn.setRequestProperty("Connection", "Keep-Alive");

                        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

                        //The line which gives the error:
                        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

                        dos.writeBytes(twoHyphens + boundary + lineEnd);
                        dos.writeBytes("Content-Disposition: form-data; name=\"title\""+ lineEnd);
                        dos.writeBytes(lineEnd);
                        dos.writeBytes(Title);
                        dos.writeBytes(lineEnd);
                        dos.writeBytes(twoHyphens + boundary + lineEnd);

                        dos.writeBytes("Content-Disposition: form-data; name=\"description\""+ lineEnd);
                        dos.writeBytes(lineEnd);
                        dos.writeBytes(Description);
                        dos.writeBytes(lineEnd);
                        dos.writeBytes(twoHyphens + boundary + lineEnd);

                        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + iFileName +"\"" + lineEnd);
                        dos.writeBytes(lineEnd);

                        Log.e(Tag,"Headers are written");

                        // create a buffer of maximum size
                        int bytesAvailable = fileInputStream.available();

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

                        // read file and write it into form...
                        int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                        while (bytesRead > 0)
                        {
                                dos.write(buffer, 0, bufferSize);
                                bytesAvailable = fileInputStream.available();
                                bufferSize = Math.min(bytesAvailable,maxBufferSize);
                                bytesRead = fileInputStream.read(buffer, 0,bufferSize);
                        }
                        dos.writeBytes(lineEnd);
                        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                        // close streams
                        fileInputStream.close();

                        dos.flush();

                        Log.e(Tag,"File Sent, Response: "+String.valueOf(conn.getResponseCode()));

                        InputStream is = conn.getInputStream();

                        // retrieve the response from server
                        int ch;

                        StringBuffer b =new StringBuffer();
                        while( ( ch = is.read() ) != -1 ){ b.append( (char)ch ); }
                        String s=b.toString();
                        Log.i("Response",s);
                        dos.close();
                }
                catch (MalformedURLException ex)
                {
                        Log.e(Tag, "URL error: " + ex.getMessage(), ex);
                }

                catch (IOException ioe)
                {
                        Log.e(Tag, "IO error: " + ioe.getMessage(), ioe);
                }
        }

        @Override
        public void run() {
                // TODO Auto-generated method stub
        }
}

我的 MainActivity 代码:

try {
                    // Set your file path here
                    FileInputStream fstrm = new FileInputStream(Environment.getExternalStorageDirectory().toString()+ File.separator  + "bina/shruttech/3_0.png");

                    // Set your server page url (and the file title/description)
                    HttpFileUpload hfu = new HttpFileUpload("http://XXXX:8080/fileup.aspx", "mytitle","mydescription");

                    hfu.Send_Now(fstrm);

                  } catch (FileNotFoundException e) {
                    // Error: File not found
                  }

我的 fileup.aspx 代码:

protected void Page_Init(object sender, EventArgs e)
{
  string vTitle = "";
  string vDesc = "";
  string FilePath = Server.MapPath("/files/cur_file.mp4");        

  if (!string.IsNullOrEmpty(Request.Form["title"]))
  {
    vTitle = Request.Form["title"];
  }
  if (!string.IsNullOrEmpty(Request.Form["description"]))
  {
    vDesc = Request.Form["description"];
  }

  HttpFileCollection MyFileCollection = Request.Files;
  if (MyFileCollection.Count > 0)
  {
    // Save the File
    MyFileCollection[0].SaveAs(FilePath);
  }
}

在此先感谢您的帮助...

4

1 回答 1

0

试试这个方法:upload-binary-data-http-post

于 2014-02-13T17:40:45.293 回答