java - 正在写入时无法从服务器读取
问题描述
我正在使用异步任务将文件写入服务器。这通常需要大约 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();
}
解决方案
如果您所指的“写入”是对文件的完全重写,那么允许这样做从根本上是不安全的。考虑将数据库技术用于保存在普通文件中的任何内容。数据库技术的设计目的是在应用程序使用的数据块中进行更细粒度的粒度化,从而实现更好的并发性。
推荐阅读
- openshift - 是否可以仅在 Openshift Master 上禁用 CNI?
- angular - 无法在 Mangaeden API Angular 6 中获取图像 url
- .htaccess - .htaccess Url 重写和重定向
- oracle - PL/SQL Developer 测试脚本在执行模式下失败,在调试模式下通过
- html - XPath 选择不只有一个特定子节点的节点?
- css - 关于背景和 calc() 的一个小问题
- angular - 订阅方法的错误回调未在 angular4 中触发
- python - 凯撒密码中的单个字符不匹配:Python
- php - Content disappear when I turn Wordpress debug off
- cassandra - What about change cassandra LeveledCompactionStrategy fanout option to 100 or even bigger?