首页 > 解决方案 > Android将文件上传到服务器导致OOM

问题描述

只是在这里需要一些帮助。我正在开发一个 Android 应用程序来帮助人们将文件备份到共享网络服务器(现在共享,可能很快会迁移到托管我自己的服务器)。

到目前为止,我已经设法让这个应用程序在我的手机(华为 P9 Lite,OS 6)上运行,并且在一个出现 OOM 错误的测试仪上遇到了问题。我认为它可能是设备,因为没有其他人有问题。另一个用户现在收到错误。最主要的是,它是零星的,不一致的。

根据我的研究和我有限的知识(这里的前德尔福开发人员,一切都为我们完成了 :) 我需要确保删除不再需要的引用。我看了这个页面,我认为我在代码中没有做错任何事情:https ://android.jlelse.eu/9-ways-to-avoid-memory-leaks-in-android-b6d81648e35e 我走了通过代码,我找不到可以优化的地方。

我已将以下内容添加到我的清单中:

 android:largeHeap="true"
    android:hardwareAccelerated="false"

任何人都可以在这里指导我。

我的问题的第 2 部分是,目前我连接到服务器并检查文件是否存在,如果不存在,我复制上传的文件。我认为这可能不是最有效的做事方式。是否值得单独检查文件是否存在,将其存储在变量中然后仅上传?

我考虑过的其他事情是:

将文件名存储在服务器上的 MySQL 数据库中,进行单独的查询,如果不存在,则上传。我考虑使用共享首选项,但不希望用户在重新安装时失去进度。

有什么想法吗?下面的代码:

public class UploadToServer extends Activity {

TextView messageText;
Button uploadButton;
Button testButton;
Button LoginButton;
Button NewHttpButton;
Button newLogButton;
Boolean haveperm = false;
int serverResponseCode = 0;
int itemcount = 0;
int totalfilescount = 0;
int uploadcount = 0;
boolean busy = false;


String upLoadServerUri = null;

/**********  File Path *************/
String uploadFilePath = Environment.getExternalStorageDirectory().toString() + "/My Folder"; //- evenutally will be a list that will be looped through
String uploadFileName = "hello.png";

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Bugsnag.start(this);
    setContentView(R.layout.activity_upload_to_server);

    uploadButton = (Button) findViewById(R.id.uploadButton);
    

    messageText = (TextView) findViewById(R.id.messageText);



            /************* Php script path ****************/
    upLoadServerUri = "pathtoserver and php file"; // php file checks if the file exists and copies if not
    uploadButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {


            if (!busy) {
                busy = true;

                if (ContextCompat.checkSelfPermission(UploadToServer.this,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                    // Permission is not granted
                    ActivityCompat
                            .requestPermissions(
                                    UploadToServer.this,
                                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                    100);
                } else {
                    haveperm = true;
                }

                if (haveperm) {
                    new Thread(new Runnable() {
                        public void run() {
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    messageText.setText("uploading started - please be patient as this may take a while");
                                }
                            });

                            File f;
                            
                            f = new File(uploadFilePath);
                            try {
                                
                                if (f.exists() && f.isDirectory()) {
                                    File file[] = f.listFiles();
                                    totalfilescount = f.listFiles().length;
                                    if (file != null) {
                                        itemcount = 0;
                                        for (int i = 0; i < file.length; i++) {
                                            uploadFileName = file[i].getName();
                                            uploadFile(uploadFilePath + "/" + uploadFileName);
                                        }
                                    }
                                }
                                busy = false;
                            } catch (NullPointerException e) {
                                throw new IllegalStateException("Not found:" + uploadFilePath, e);
                            }
                        }
                    }).start();
                    runOnUiThread(new Runnable() {
                        public void run() {
                            messageText.setText("Completed");
                        }
                    });
            } else {
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Still busy with this, please wait",
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    });
}

public static void displayDirectoryContents(File dir) {
    try {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.println("directory:" + file.getCanonicalPath());

                displayDirectoryContents(file);
            } else {
                //System.out.println("     file:" + file.getCanonicalPath());
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public int uploadFile(String sourceFileUri) {
    String fileName = 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);
    busy = true;
    if (!sourceFile.isFile()) {
        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");//30122020
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("ENCTYPE", "multipart/form-data");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            //conn.setRequestProperty("Content-Length", Integer.toString(fileInputStream.available())); //took this out as it was causing exceeded content-length limit
            // conn.setDoOutput(true);
            conn.setRequestProperty("uploaded_file", fileName);//20012021

            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);

            // 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();

            
            if (serverResponseCode == 200) {
                itemcount++;
                uploadcount++;


                runOnUiThread(new Runnable() {
                    public void run() {

                        messageText.setText("File " + String.valueOf(itemcount) + " of " + String.valueOf(totalfilescount) + " Files Uploaded successfully");

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

                        Toast.makeText(UploadToServer.this, "Failed" + String.valueOf(serverResponseCode),
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }//close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();

        } catch (MalformedURLException ex) {

            //dialog.dismiss();
            ex.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {
                    messageText.setText("MalformedURLException Exception : check script url.");
                    //   Toast.makeText(UploadToServer.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
                }
            });

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

        //  dialog.dismiss();
            e.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {
                    //  messageText.setText("Got Exception : see logcat ");
                    messageText.setText(e.getMessage());
                    //  Toast.makeText(UploadToServer.this, "Got Exception : see logcat ",
                    //        Toast.LENGTH_SHORT).show();
                }
            });
            Log.e("Exception", "Exception : "
                    + e.getMessage(), e);
        }
    //  dialog.dismiss();

        return serverResponseCode;

    } // End else block

}

}

php文件:

<?php

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

?>

标签: phpandroidmysqlfile-upload

解决方案


推荐阅读