java - 写入过程中无法从服务器读取

标签 java android

我正在使用异步任务将文件写入服务器。这通常需要大约 5 秒。同时需要从服务器读取数据以填充 UI 中的列表。这可能吗?

目前,在上一个写入完成之前,我会看到一个空白屏幕。我的读取调用也是异步的。

写入服务器的代码:

@Override
    protected String doInBackground(String... params) {
        int count = 0;
        try {
            String sourceFileUri = params[0];
            String mp4FilePath = params[1];
            String trimName = sourceFileUri.substring(0, sourceFileUri.length() - 4);

            File mp4_file = new File(mp4FilePath);
            String fileName_Time = mp4_file.getName();
            String trimTime = fileName_Time.substring(4);
            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 {
                    int progress = 0;
                    String upLoadServerUri = "http://example.com/abc/efgh.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=\"" + trimName + "_" + trimTime + "\"" + 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);
                        System.out.println("barbar "+bytesRead);
                        System.out.println("barbar "+bufferSize);
                        System.out.println("barbar "+bytesAvailable);
                        progress += ((bytesAvailable+5)/(bytesRead+1));
                        System.out.println("barbar prog "+progress);

                        // update progress bar
                        publishProgress((progress*10));
                    }

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

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

                    if (serverResponseCode == 200) {

                    }

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

                } catch (Exception e) {
                    // dialog.dismiss();
                    e.printStackTrace();
                }

            } // End else block

        } catch (Exception ex) {
            // dialog.dismiss();

            ex.printStackTrace();
        }
        return "Executed";
    }

从服务器读取的代码:

BufferedReader reader = null;
        try {
            URL url = new URL(stringUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            StringBuilder sb = new StringBuilder();

            InputStreamReader is = new InputStreamReader(connection.getInputStream());

            reader = new BufferedReader(is);

            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }

            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (reader!=null)
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

最佳答案

如果您所指的“写入”是对文件的完全重写,那么允许这样做从根本上来说是不安全的。考虑对保存在普通文件中的任何内容使用数据库技术。数据库技术旨在对应用程序所使用的数据 block 进行更细粒度的粒度化,从而实现更好的并发性。

关于java - 写入过程中无法从服务器读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51239365/

相关文章:

java - 如何在 swt 树编辑器中更改标签的颜色

java - 将时间戳从远程sql服务器获取到本地mysql的数据上

java - 如何在Java中使用Canvas画一个圆

android.view.InflateException : Binary XML file line #20: Error inflating class fragment

java - CN1 : Scrolling not working on MultiButton

java - 是否有 Java 代码约定的更新版本

android - 如果它是 android 无障碍服务的用户名字段,有没有办法检查来源?

android - 在 ExpandableListView 中获取 subview 内容

android - JSON - 与 Android 应用程序一起使用的单个文件

Android AudioTrack 无法初始化