【问题标题】:Upload file to server Android将文件上传到服务器 Android
【发布时间】:2016-04-01 01:16:41
【问题描述】:

我想将一些 mp3 文件上传到我的本地主机服务器,但是我不确定我哪里出错了。我认为是php 文件,但这是我的php 文件:

<?php
    $file_path = "/storage/emulated/0/sample.txt";
    $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
        echo "success";
    } else{
        echo "fail";
    }
 ?>

这是我的java 代码:

public class UploadFile extends Activity {
    int serverResponseCode = 0;
    String serverResponseMessage;
    String uploadFileName;
    String uploadFilePath;
    String upLoadServerUri = null;
    ProgressBar loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.uploads);
        uploadFilePath = "/storage/emulated/0/";
        uploadFileName = "sample.txt";

        upLoadServerUri = "http://10.0.3.2/files/uploadfile.php";
        new Thread(new Runnable() {

            public void run() {
                uploadFile(uploadFilePath + "" + uploadFileName);
            }
        }).start();
    }

    public void uploadFile(String sourceFileUri) {
        String fileName = sourceFileUri;
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int code;

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;

        File sourceFile = new File(sourceFileUri);

        if (!sourceFile.isFile()) {
            code = 0;
            this.runOnUiThread(new Runnable() {
                public void run() {

                }
            });
        }
        else {
            try {
                FileInputStream fileInputStream = new FileInputStream(sourceFile);
                URL url = new URL(upLoadServerUri);

                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("uploaded_file", fileName);
                dos = new DataOutputStream(conn.getOutputStream());
                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name='uploaded_file';filename='"+fileName+"'"+lineEnd);
                dos.writeBytes(lineEnd);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];
                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);
                serverResponseCode = conn.getResponseCode();
                code = serverResponseCode;
                if (code==200) {
                    this.runOnUiThread(new Runnable() {
                        public void run() {

                        }
                    });
                }
                else {
                    this.runOnUiThread(new Runnable() {
                        public void run() {

                        }
                    });
                }
                serverResponseMessage = conn.getResponseMessage();
                fileInputStream.close();
                dos.flush();
                dos.close();
            }
            catch (MalformedURLException ex) {
                this.runOnUiThread(new Runnable() {
                    public void run() {

                    }
                });
            }
            catch (Exception e) {
                this.runOnUiThread(new Runnable() {
                    public void run() {

                    }
                });
            }
        }
    }
}

我在网上获得了这段代码并为我的目的对其进行了修改 - 但它不起作用,但正如我所提到的,它可能是 php 文件

任何帮助都意义重大(即使是解决这个问题也能让我正常吃饭和睡觉)

谢谢

【问题讨论】:

  • 不要只是告诉我们它不起作用..实际上要说出正在发生的事情。你尝试过什么来解决这个问题?当你这样做时发生了什么?
  • 它的 php 文件。我认为它没有做任何事情。如在php文件中如何发送文件?
  • 我们不是魔术师!我真的不知道你的代码有什么问题,而且我们知道调试有多糟糕。首先注释掉你的大部分代码,然后只用ping你的服务器看看你是否得到响应。然后按照自己的方式进行文件上传。

标签: php android rest file-upload httpurlconnection


【解决方案1】:

我认为你应该尝试使用 json 解析器,将你的 PHP 脚本设置为输出 json,以获得成功和失败。您需要将图像文件名字符串从您的 java 代码解析到 Web 服务,并获取它以执行操作。 您还可以使用 $_GET 从您的 java 代码中获取您的文件

【讨论】:

    【解决方案2】:

    使用以下 php 代码更改您的代码,它可以完美运行。

    <?php
    $file_path = "uploads/";
        $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
        if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
            echo "success";
        } else{
            echo "fail";
        }
     ?>
    

    我已经测试过它可以完美运行

    【讨论】:

    • 嗨伙计,本地服务器中的文件保存在哪里?比如在哪个目录?
    • 对不起,现在更新,在你的php代码目录上传文件夹中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 2014-10-27
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多