Android 下载后Pdf文件变为空
下载后,文件变为白色 .Android 下载后Pdf文件变为空,android,file,pdf,retrofit2,download,Android,File,Pdf,Retrofit2,Download,下载后,文件变为白色 . 下载代码: public Single<File> download(String url, long formId) { return restService.downloadFile(url) .subscribeOn(Schedulers.io()) .map(responseBodyResponse -> { String filename = Str
下载代码:
public Single<File> download(String url, long formId) {
return restService.downloadFile(url)
.subscribeOn(Schedulers.io())
.map(responseBodyResponse -> {
String filename = String.valueOf(formId);
long timeInMillis = Calendar.getInstance().getTimeInMillis();
filename = filename.concat("_").concat(String.valueOf(timeInMillis)).concat(PDF);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsoluteFile(), filename);
BufferedSink sink = Okio.buffer(Okio.sink(file));
// you can access body of response
if (responseBodyResponse.body() != null) {
BufferedSource bufferedSource = responseBodyResponse.body().source();
sink.writeAll(bufferedSource);
sink.close();
}
return file;
});
}
公共单一下载(字符串url,长格式ID){
返回restService.downloadFile(url)
.subscribeOn(Schedulers.io())
.map(responseBodyResponse->{
字符串文件名=String.valueOf(formId);
long-timeInMillis=Calendar.getInstance().getTimeInMillis();
filename=filename.concat(“”).concat(String.valueOf(timeInMillis)).concat(PDF);
File File=新文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY\u下载).getAbsoluteFile(),文件名);
BufferedSink sink=Okio.buffer(Okio.sink(文件));
//您可以访问响应主体
if(responseBodyResponse.body()!=null){
BufferedSource BufferedSource=responseBodyResponse.body().source();
sink.writeAll(缓冲源);
sink.close();
}
返回文件;
});
}
我做错了什么?这种方法对我有效
private final int MEGABYTE = 1024 * 1024;
public Single<File> download(String fileUrl, long formId) {
return Single.create(emitter -> {
try {
String filename = String.valueOf(formId);
long timeInMillis = Calendar.getInstance().getTimeInMillis();
filename = filename.concat("_").concat(String.valueOf(timeInMillis)).concat(PDF);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsoluteFile(), filename);
URL url = new URL(fileUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(file);
byte[] buffer = new byte[MEGABYTE];
int bufferLength;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
emitter.onSuccess(file);
} catch (FileNotFoundException e) {
emitter.onError(e);
e.printStackTrace();
} catch (MalformedURLException e) {
emitter.onError(e);
e.printStackTrace();
} catch (IOException e) {
emitter.onError(e);
e.printStackTrace();
}
});
}
private final int-MEGABYTE=1024*1024;
公共单一下载(字符串文件URL,长格式ID){
返回单个。创建(发射器->{
试一试{
字符串文件名=String.valueOf(formId);
long-timeInMillis=Calendar.getInstance().getTimeInMillis();
filename=filename.concat(“”).concat(String.valueOf(timeInMillis)).concat(PDF);
File File=新文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY\u下载).getAbsoluteFile(),文件名);
URL URL=新URL(文件URL);
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.connect();
InputStream InputStream=urlConnection.getInputStream();
FileOutputStream FileOutputStream=新的FileOutputStream(文件);
字节[]缓冲区=新字节[MB];
int缓冲长度;
而((bufferLength=inputStream.read(buffer))>0){
写入(缓冲区,0,缓冲区长度);
}
fileOutputStream.close();
emitter.onSuccess(文件);
}catch(filenotfounde异常){
发射体。onError(e);
e、 printStackTrace();
}捕获(格式错误){
发射体。onError(e);
e、 printStackTrace();
}捕获(IOE异常){
发射体。onError(e);
e、 printStackTrace();
}
});
}
查看输出文件的大小以及与原始文件的一致性