gpt4 book ai didi

java - 如何使用apache文件 uploader 在google app engine java中获取文件大小

转载 作者:行者123 更新时间:2023-11-30 11:36:10 25 4
gpt4 key购买 nike

我正在使用下面显示的代码将多部分文件上传到 Google 应用引擎。在 apache commons 文件 uploader 的帮助下,我成功地将文件上传到 Google 应用引擎。我想验证文件大小是否为 0 字节。为此,我验证了 Google 应用引擎和 Apache 公共(public)文件 uploader 以检查文件大小,但我失败了。我们有什么方法可以找到文件大小。请给我一个想法。

我的 Servlet

ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter;
iter = upload.getItemIterator(request);
while (iter.hasNext()) {
item = iter.next();
String fileName = item.getName();
String fieldName = item.getFieldName();
if (item.isFormField()) {
String fieldValue = Streams.asString(item.openStream());
}
InputStream is1 = item.openStream();
try {
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(mime, fileName);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
byte[] b1 = new byte[BUFFER_SIZE];
int readBytes1;
while ((readBytes1 = is1.read(b1)) != -1) {
writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));
}
writeChannel.closeFinally();

最佳答案

您需要将整个 is1 输入流读入字节缓冲区并查看其长度。您可以使用此代码段:

public static byte[] getBytes(InputStream is) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();

int len;
byte[] data = new byte[10000];
while ((len = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, len);
}

buffer.flush();
return buffer.toByteArray();
}

关于java - 如何使用apache文件 uploader 在google app engine java中获取文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14659667/

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