gpt4 book ai didi

java - InputStream 错误的文件大小

转载 作者:行者123 更新时间:2023-11-29 21:01:44 30 4
gpt4 key购买 nike

我尝试在上传过程中实现进度条。图片(207 字节)上传到 FTP 服务器。问题是,类 ProgressInputStream 没有在控制台中打印正确的进度大小。这是我开始上传的方式:

File file = new File(path);

String firstRemoteFile = "" + file.getName() + IMEI;
InputStream inputStream = new FileInputStream(file);

System.out.println("Start uploading first file"+file.length());


ProgressInputStream progressInput = new ProgressInputStream("test",inputStream, file.length());

boolean done = ftpClient.storeFile(firstRemoteFile, progressInput);

进度输入流

public class ProgressInputStream extends InputStream {
private final long size;
public long progress;
private long lastUpdate = 0;
private final InputStream inputStream;
private final String name;
private boolean closed = false;

public ProgressInputStream(String name, InputStream inputStream, long size) {
this.size = size;
this.inputStream = inputStream;
this.name = name;
}

public ProgressInputStream(String name, FileContent content)
throws FileSystemException {
this.size = content.getSize();
this.name = name;
this.inputStream = content.getInputStream();
}

@Override
public void close() throws IOException {
super.close();
if (closed) throw new IOException("already closed");
closed = true;
}

@Override
public int read() throws IOException {
int count = inputStream.read();
if (count > 0)
progress += count;
lastUpdate = maybeUpdateDisplay(name, progress, lastUpdate, size);
return count;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int count = inputStream.read(b, off, len);
if (count > 0)
progress += count;
lastUpdate = maybeUpdateDisplay(name, progress, lastUpdate, size);
System.out.println("count"+count+"size"+size);
return count;

}


static long maybeUpdateDisplay(String name, long progress, long lastUpdate, long size) {

System.out.println("name "+ name+" progress "+ progress+" lastUpdate "+ lastUpdate+" "+ "sie "+ size);
return lastUpdate;
}}

日志

09-17 20:03:39.828: I/System.out(11346): name test  progress 13913 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14093 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14093 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14093 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14093 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14337 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14337 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14567 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14567 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14687 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14770 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14770 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14778 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14778 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14778 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14778 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14837 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14837 lastUpdate 0 sie 207
09-17 20:03:39.828: I/System.out(11346): name test progress 14837 lastUpdate 0 sie 207

我不知道为什么进度这么快。最后进度应该是 207。

我很感激任何建议(-:

最佳答案

像这样更改您的 ProgressInputStream:

public class ProgressInputStream extends InputStream {

public ProgressInputStream(String name, InputStream inputStream, long size) {
this.size = size;
this.inputStream = inputStream;
this.name = name;
}

public ProgressInputStream(String name, FileContent content)
throws FileSystemException {
this.size = content.getSize();
this.name = name;
this.inputStream = content.getInputStream();
}

@Override
public void close() throws IOException {
super.close();
if (closed) throw new IOException("already closed");
closed = true;
}

@Override
public int read() throws IOException {
int count = inputStream.read();

progress ++;
lastUpdate = maybeUpdateDisplay(name, progress, lastUpdate, size);

return count;
}
/*@Override
public int read(byte[] b, int off, int len) throws IOException {
int count = inputStream.read(b, off, len);

progress ++;
lastUpdate = maybeUpdateDisplay(name, progress, lastUpdate, size);
System.out.println("count"+count+"size"+size);

return count;

}*/


static long maybeUpdateDisplay(String name, long progress, long lastUpdate, long size) {
/* if (Config.isInUnitTests()) return lastUpdate;
if (size < B_IN_MB/10) return lastUpdate;
if (progress - lastUpdate > 1024 * 10) {
lastUpdate = progress;
int hashes = (int) (((double)progress / (double)size) * 40);
if (hashes > 40) hashes = 40;
String bar = StringUtils.repeat("#",
hashes);
bar = StringUtils.rightPad(bar, 40);
System.out.format("%s [%s] %.2fMB/%.2fMB\r",
name, bar, progress / B_IN_MB, size / B_IN_MB);
System.out.flush();
}*/


System.out.println("name "+ name+" progress "+ progress+" lastUpdate "+ lastUpdate+" "+ "sie "+ size);
return lastUpdate;
}}

关于java - InputStream 错误的文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25897609/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com