【问题标题】:Android: Upload html file to http serverAndroid:将html文件上传到http服务器
【发布时间】:2014-02-12 19:53:04
【问题描述】:


我目前正在尝试使用 android 将 HTML 文件上传到我的灯服务器。 我真的不知道如何让文件上传到服务器。我正在做的是:以编程方式编写 HTML 文件,然后将其上传到网络服务器。
我知道这是可能的。

我已经开始写文件了。我的服务器上还有一个上传脚本:

<?php
$target_path  = "./";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
 echo "The file ".  basename( $_FILES['uploadedfile']['name']).
 " has been uploaded";
} else{
 echo "There was an error uploading the file, please try again!";
}
?>

我已经阅读了所有这些关于如何做到这一点的教程,但我就是不明白。

【问题讨论】:

  • 你的 Android Java 代码呢?
  • 没有java代码,根本找不到合适的代码,网上找的例子不行(stackoverflow.com/questions/7018883/android-file-upload),我自己也写不出来
  • 在你的android代码上传,你在上传HTML文件时确定它是多部分实体吗?
  • 如果您使用 LAMP 服务器,我认为它不是公开的,您是否已正确配置通信?首先尝试发布一个字符串,如果它有效,那么您可以尝试使用文件。
  • 以防万一您可以检查一下以了解如何操作:github.com/kahelnadiablo/android_uploader

标签: php android html http upload


【解决方案1】:

您可以将其用作 android 代码:

public int uploadFile(String sourceFileUri) {

    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File sourceFile = new File(sourceFileUri);

    if (!sourceFile.isFile()) {
        Log.e("uploadFile", "Source File not exist :" + uploadFilePath + "" + uploadFileName);
        return 0;
    }
    else
    {
        try {
            // open a URL connection to the Servlet
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            URL url = new URL(upLoadServerUri);

            // Open a HTTP  connection to  the URL
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true); // Allow Inputs
            conn.setDoOutput(true); // Allow Outputs
            conn.setUseCaches(false); // Don't use a Cached Copy
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("ENCTYPE", "multipart/form-data");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            conn.setRequestProperty("uploadedfile", sourceFileUri);

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

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


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

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

            // read file and write it into form...
            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);

            }

            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();
            String serverResponseMessage = conn.getResponseMessage();

            Log.i("uploadFile", "HTTP Response is : "+ serverResponseMessage + ": " + serverResponseCode);

            if(serverResponseCode == 200){
                Log.e("uploadFile", "File Uploaded.");
                // Congrats!
            }

            //close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();

        } catch (Exception e) {
            Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e);
        }
        return serverResponseCode;

    } // End else block
}

【讨论】:

    猜你喜欢
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多