gpt4 book ai didi

java - 为什么谷歌云存储文件的大小小于使用谷歌应用引擎 Java 上传的文件大小

转载 作者:行者123 更新时间:2023-11-29 03:38:48 25 4
gpt4 key购买 nike

将文件上传到 Google Cloud Storage 时,存储文件的大小与原始上传文件不同。存储文件的大小远小于原始大小。我的上传servlet如下图,

public class UploadServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private StorageService storage = new StorageService();
private static int BUFFER_SIZE =1024 * 1024* 10;
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
ServletFileUpload upload = new ServletFileUpload();
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
if (!isMultipart) {
resp.getWriter().println("File cannot be uploaded !");
return;
}
FileItemIterator iter;
try {
iter = upload.getItemIterator(req);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String fileName = item.getName();
String fieldName = item.getFieldName();
String mime = item.getContentType();
if (fieldName.equals("file")) { // the name of input field in html
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));
storage.init(fileName, mime);
storage.storeFile(b1, readBytes1);
}
writeChannel.closeFinally();
String blobKey = fileService.getBlobKey(file).getKeyString();
Entity input = new Entity("Input");
input.setProperty("Input File", blobKey);
datastore.put(input);
is1.close();
storage.destroy();
} catch (Exception e) {
e.printStackTrace(resp.getWriter());
}
}
}
} catch (FileUploadException e) {
// log error here
}
}
}

我的存储代码如下所示,

public class StorageService {
public static final String BUCKET_NAME = "MyBucket";
private FileWriteChannel writeChannel = null;
FileService fileService = FileServiceFactory.getFileService();
private BufferedOutputStream bos = null;
private static final Logger log = Logger.getLogger(StorageService.class.getName());
private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
public void init(String fileName, String mime) throws Exception {
System.out.println("Storage service:init() method: file name:"+fileName+" and mime:"+mime);
log.info("Storage service:init() method: file name:"+fileName+" and mime:"+mime);
GSFileOptionsBuilder builder = new GSFileOptionsBuilder()
.setAcl("public_read")
.setBucket(BUCKET_NAME)
.setKey(fileName)
.setMimeType(mime);
AppEngineFile writableFile = fileService.createNewGSFile(builder.build());
boolean lock = true;
writeChannel = fileService.openWriteChannel(writableFile, lock);
bos = new BufferedOutputStream(Channels.newOutputStream(writeChannel));
}
public void storeFile(byte[] b, int readSize) throws Exception {
bos.write(b,0,readSize);
System.out.println(readSize);
bos.flush();
}
public void destroy() throws Exception {
log.info("Storage service: destroy() method");
bos.close();
writeChannel.closeFinally();
}
public BlobKey getBlobkey (String filename) {
BlobKey bk = blobstoreService.createGsBlobKey("/gs/MyBucket/"+filename);
return bk;
}
}

当我上传一个 2 MB 的文件时,它只存储 155 个字节。我不明白哪里出了问题。

请给我一个想法,

感谢您的帮助。

最佳答案

在您的 UploadServlet 类中,您为每个文件调用 storage.init(...) 多次,始终重新创建文件。这不应该发生:

while ((readBytes1 = is1.read(b1)) != -1) {
writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));
storage.init(fileName, mime); // <- HERE
storage.storeFile(b1, readBytes1);
}

您需要将 init 语句移出循环,放在它的前面。

关于java - 为什么谷歌云存储文件的大小小于使用谷歌应用引擎 Java 上传的文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14139127/

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