我遇到了Tomcat Liferay文件上传的问题。
在代码中我得到一个空文件大小,导致Tomcat在尝试在Tomcat Temp文件夹中找到新上传的文件时返回null。
我的代码:
File[] files = uploadRequest.getFiles("fileupload");
for(File f : files) {
(f.length == 0) { /* This is always true (null file upload) */ }
FileUtil.copyFile(f, newfile); // This throws a null pointer exception
}
// Also happens with any other attempts at getting the file from the form, like this (below).
// None of these work with files < 1 kb
FileItem[] fileitems = uploadRequest.getMultipartParameterMap().get("fileupload");
如果文件大于1kb,那么它确实有效。真正的问题是当我实际尝试使用FileUtil.copyFile()实际移动文件时上面的代码错误 - 它抛出一个空指针异常,说原始文件(此时应该在temp文件夹中),是空的。
对于为什么会这种情况感到很困惑。以下是HTML:
<aui:form action="<%=uploadFileURL%>" enctype="multipart/form-data" method="POST">
<aui:input type="hidden" value="/" name="selected_dir_input"/>
<b>Selected Folder: </b>
<span id="selected_folder">/Home/User/Desktop/Some_Selected_file.ext</span>
<aui:input type="file" name="fileupload" multiple="true"/>
<aui:button name="Upload" value="Upload" type="submit" />
</aui:form>
由于
答案 0 :(得分:0)
看起来这解决了它:https://web.liferay.com/web/josef.sustacek/blog/-/blogs/uploadportletrequest-and-file-size-limits
// This can be used to get files smaller than 1kb
uploadRequest.getFile("param", true);