【问题标题】:File does not upload on server android文件未在服务器 android 上上传
【发布时间】:2020-08-08 02:37:00
【问题描述】:

我尝试了许多方法如何在服务器上加载图像,但没有一种显示任何错误。没有任何帮助。图片不会上传到服务器。图像的文件路径是正确的。我也尝试更改服务器上的文件路径,它也没有帮助我并添加了访问文件的权限,这没有帮助。

加载文件:

public class UploadFiles {
    private String sourceFileUri = "";

    public UploadFiles(final String sourceFileUri){
        this.sourceFileUri = sourceFileUri;
        new UploadFileAsync().execute("");
    }

    private class UploadFileAsync extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            try {
                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()) {

                    try {
                        String upLoadServerUri = "http://myserver/gpstracker/api/v1/users/uploadfile.php";

                        // 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("bill", sourceFileUri);

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

                        dos.writeBytes(twoHyphens + boundary + lineEnd);
                        dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
                                + sourceFileUri + "\"" + 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);

                        }

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

                        int serverResponseCode = conn.getResponseCode();

                        String serverResponseMessage = conn
                                .getResponseMessage();
                        System.out.println("SERVER: " + serverResponseMessage);
                        if (serverResponseCode == 200) {
                        }

                        fileInputStream.close();
                        dos.flush();
                        dos.close();

                    } catch (Exception e) {

                        e.printStackTrace();

                    }
                } // End else block


            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return "Executed";
        }
    }

}

php 文件

<?php

 if (is_uploaded_file($_FILES['bill']['tmp_name'])) {
    $uploads_dir = '/var/www/html/gpstracker/api/v1/users/images';
    $tmp_name = $_FILES['bill']['tmp_name'];
    $pic_name = $_FILES['bill']['name'];
    move_uploaded_file($tmp_name, $uploads_dir.$pic_name);
  }
    else{
     echo "File not uploaded successfully.";
  }  
?>

服务器响应:

System.out:服务器:好的

【问题讨论】:

    标签: java android


    【解决方案1】:

    服务器回复 200 并不代表上传成功。尝试输出 php echo。见Get PHP Echo in Android

    编辑:您对该目录有权限吗?见PHP move_uploaded_file() error?

    【讨论】:

    • I/System.out:文件未上传成功。
    • 非常感谢!我没有权限))
    猜你喜欢
    • 1970-01-01
    • 2012-04-23
    • 2015-02-27
    • 2012-02-06
    • 2014-10-27
    • 1970-01-01
    • 2013-06-20
    • 2019-02-28
    • 2012-12-08
    相关资源
    最近更新 更多