0

所以,我无法摆脱这个问题:我需要将一个XML文件和一个.jpg文件从我的 android 应用程序(API 8)上传到 HTTP 服务器(带有 IIS 7.5 的 win 2008 服务器)。我已经按照之前搜索的建议启用了PUT动词和卸载和 webdav 方法。WebDav另外,我不确定我是否在服务器端做这件事,因为我无法得到任何回复。

这是我的代码

        URL fileurl = new URL("Server Upload Path");

        HttpURLConnection urlConnection = (HttpURLConnection) fileurl
                .openConnection();
        urlConnection.setRequestMethod("PUT");
        urlConnection.setDoOutput(true);
        urlConnection.connect();

        OutputStream os = urlConnection.getOutputStream();

        File upFile = new File("My Local File");
                    //I'm sure the file exists
        FileInputStream fis = new FileInputStream(upFile);
        BufferedInputStream bfis = new BufferedInputStream(fis);
        byte[] buffer = new byte[1024];
        int bufferLength = 0;

        // now, read through the input buffer and write the contents to the
        // file
        while ((bufferLength = bfis.read(buffer)) > 0) {
            os.write(buffer, 0, bufferLength);

        }

抱歉,如果我忘记了一些您可能需要帮助的信息。我IIS也是安卓新手。

4

1 回答 1

-1

为什么不尝试标准的多部分文件上传请求(基于 POST 而不是 PUT):

     final static String MULTIPART_BOUNDARY = "------------------563i2ndDfv2rTHiSsdfsdbouNdArYfORhxcvxcvefj3q2f";

 public static void sendFileToServer(String url, File logFiles) {
    HttpURLConnection connection = null;
    OutputStream os = null;
    DataInputStream is = null;
try {
    StringBuilder fullUrl = new StringBuilder(url);
    connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + MULTIPART_BOUNDARY);
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.connect();
    os = new BufferedOutputStream(connection.getOutputStream());
    if(os != null) {
          os.write(("--" + MULTIPART_BOUNDARY + EOL).getBytes());
          os.write(String.format("Content-Disposition:form-data;name=\"UploadedFile\";filename=\"%s\"\r\nContent-Type: application/x-zip-compressed\r\n\r\n", UPLOADED_FILE_NAME).getBytes());

          // Upload file(s) data here and send

          os.write((EOL + "--" + MULTIPART_BOUNDARY + "--" + EOL + EOL).getBytes());
          os.flush();
          os.close();
          os = null;
       }
       if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                // Process server response
       }
   } catch (MalformedURLException e) {


   } catch (IOException e) {


   } catch (Exception e1) {

   } finally {
    try {
        if(os != null) {
            os.close();
        }
    } catch (IOException e) {
        Log.e(TAG, "sendFileToServer exception: close OutputStream", e);
    }
    try {
        if(is != null) {
            is.close();
        }
    } catch (IOException e) {
        Log.e(TAG, "sendFileToServer exception: close InputStream", e);
    }
    if(connection != null) {
        connection.disconnect();
    }
   }
}
于 2013-01-17T17:27:26.223 回答