gpt4 book ai didi

java - 如何使用 wicket 将大文件上传到 google cloud

转载 作者:行者123 更新时间:2023-11-29 08:47:26 25 4
gpt4 key购买 nike

我尝试使用 wicket 在谷歌云上上传一个大文件。我使用 FileUploadField 和 UploadFile 方法。不过我只能上传小文件(小于 10kb)。如果我上传更大的文件,我会得到一个异常(java.security.AccessControlException:访问被拒绝)。我没有创建缓冲文件和写入的权限。

final FileUploadField FiletoUpload = new FileUploadField("uploadfile", new Model());
form.add(FiletoUpload);

form.add(new Button("upload") {

@Override
public void onSubmit() {

//here we upload
getRequestCycle().scheduleRequestHandlerAfterCurrent(new IRequestHandler() {

@Override
public void respond(IRequestCycle irc) {

FileUpload uploadedFile = FiletoUpload.getFileUpload();
HttpServletResponse httpResponse = (HttpServletResponse) irc.getResponse().getContainerResponse();

InputStream CORPUS = null;
try {
CORPUS = uploadedFile.getInputStream();

} catch (IOException ex) {
Logger.getLogger(Upload.class.getName()).log(Level.SEVERE, null, ex);
}

try {
doGet(null, httpResponse);
} catch (IOException ex) {
Logger.getLogger(Upload.class.getName()).log(Level.SEVERE, null, ex);
}
uploadedFile.closeStreams();
}

@Override
public void detach(IRequestCycle irc) {
}
});
}
});

我不使用 blobstore,我使用 com.google.appengine.tools.cloudstorage.*。我开个 channel 写在云端。

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
GcsService gcsservice = GcsServiceFactory.createGcsService();
GcsFilename uploadfile = new GcsFilename(BUCKETNAME, FILENAME);
GcsFileOptions optionsBuilder = new GcsFileOptions.Builder()
.mimeType("text/plain")
.acl("bucket-owner-full-control")
.build();

GcsOutputChannel writechannel = gcsservice.createOrReplace(uploadfile, optionsBuilder );

buffer = new StringBuffer();
int char_read = 0;
long i;
for(i=0; i< CORPUSsize; i++){
char_read = CORPUS.read(); //I read the corpus of the file
char mychar = (char) char_read;
buffer.append(mychar);
}

writechannel.write(ByteBuffer.wrap(buffer.toString().getBytes("UTF-8")));
writechannel.close();

}

有没有获取权限异常的情况下上传大文件的解决方案?

最佳答案

Wicket 使用 DiskFileItemFactory,它在 10240 字节阈值后将文件存储在磁盘上。

您必须更改 sizeThreshold 或使用 FileItemFactory 的替代实现。请参阅 MultipartServletWebRequestImpl 构造函数。

关于java - 如何使用 wicket 将大文件上传到 google cloud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24404679/

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