java - 文件未上传到 Android 服务器上

标签 java android

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

加载文件:

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: SERVER: OK

最佳答案

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

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

关于java - 文件未上传到 Android 服务器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61409767/

相关文章:

java - 等效静态和非静态方法的速度差异很大

java - 转换为和从印度教日历

java - Spring Batch 作业调度程序

java - 重命名 xml 文档中的子节点

java - 如何将多条数据保存为一个包

java - 到 glassfish 的纯 java 应用端口

java - 需要有关抽屉导航过渡的帮助

android - 如何在 exoplayer 中显示播放/暂停通知?

java - Android Studio 在生成 JavaDoc 时无法读取包

android - 如何使用 toast 显示数组