gpt4 book ai didi

java - Wicket FileUploadField 和大型 zip 上传

转载 作者:行者123 更新时间:2023-11-29 06:06:04 28 4
gpt4 key购买 nike

我正在使用 wicket 将大型 (~2Gb+) zip 文件提交到 Web 应用程序,以处理我正在使用 java.util.zip.* 类的 zip 文件,我需要能够随机读取zip 文件中的条目。所以我的代码是这样的:

class MyForm extends Form {
private FileUploadField fileField;

MyForm(String id) {
super(id);
fileField = new FileUploadField("upload");
add(fileField);
}

@Override
protected void onSubmit() {
FileUpload fileUpload = fileField.getFileUpload();
File file = fileUpload.writeToTempFile();
ZipFile zipFile = new ZipFile(file);
// Do more stuff
}
}

由于上传量很大,wicket 在解析请求时将其放入一个临时文件中,但随后 writeToTempFile() 将其复制到另一个临时文件中,因此我现在在磁盘上有该文件的两个副本。这会浪费磁盘空间、磁盘 IO 并增加请求处理时间。

我不能使用 ZipFileInputStream,因为我需要以随机顺序访问文件。有没有办法阻止 wicket 复制磁盘上的文件?

最佳答案

基于@biziclop 的回答,我编写了这个类:

public class RawFileUploadField extends FileUploadField {

private static final long serialVersionUID = 1L;

public RawFileUploadField(String id) {
super(id);
}

/**
* Attempts to get the file that is on disk if it exists and if it doesn't
* then just write the file to a temp location.
* @return The file or <code>null</code> if no file.
* @throws IOException
*/
public File getFile() throws IOException {
// Get request
final Request request = getRequest();

// If we successfully installed a multipart request
if (request instanceof IMultipartWebRequest)
{
// Get the item for the path
FileItem item = ((IMultipartWebRequest)request).getFile(getInputName());
if (item instanceof DiskFileItem) {
File location = ((DiskFileItem)item).getStoreLocation();
if (location != null) {
return location;
}
}
}
// Fallback
FileUpload upload = getFileUpload();
return (upload != null)?upload.writeToTempFile():null;
}

}

关于java - Wicket FileUploadField 和大型 zip 上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8493819/

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