当我使用php将db文件上传到服务器并恢复该文件时,Android Device Explorer中的数据库日期已更新,但内容未更新。我不明白为什么会这样。
时间已更新,但内容未更新,这是怎么回事?使用retrofit2进行上传/下载,结果日志成功,文件大小正确为“ 4kb”。
文件权限就像这样的“ -rw-rw ----”,这是我的代码,用于将下载的文件转储到原始文件中。另外,我一直在尝试使用reponse.body()。bytes()在onResponse中进行多种方式,但是任何方法都不起作用。
private boolean writeResponseBodyToDisk(ResponseBody body) {
try {
// todo change the file location/name according to your needs
File futureStudioIconFile = new File(file.getPath());
InputStream inputStream = null;
OutputStream outputStream = null;
try {
byte[] fileReader = new byte[4096];
long fileSize = body.contentLength();
long fileSizeDownloaded = 0;
inputStream = body.byteStream();
outputStream = new FileOutputStream(futureStudioIconFile);
while (true) {
int read = inputStream.read(fileReader);
if (read == -1) {
break;
}
outputStream.write(fileReader, 0, read);
fileSizeDownloaded += read;
Log.d("TAG", "file download: " + fileSizeDownloaded + " of " + fileSize);
}
outputStream.flush();
return true;
} catch (IOException e) {
return false;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
} catch (IOException e) {
return false;
}
}
这是有关下载的回复:
responseBodyCall = retrofitInterface.download("savelocation");
responseBodyCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d("Success", "success "+response.code());
Log.d("Success", "success "+response.message());
Log.d("Success", "downloaded");
boolean isSuccess = writeResponseBodyToDisk(response.body());
if(isSuccess) Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d("failure", "message = " + t.getMessage());
Log.d("failure", "cause = " + t.getCause());
}
});