Struts2 Fileupload 在动作类中给出空文件
我正在尝试使用 struts2 fileUpload 拦截器在我的 Web 应用程序中实现文件上传过程.以下是我在
I am trying to implement the file upload process in my web application using struts2 fileUpload interceptor. below is my code in
index.jsp
<tags:form action="fileUpload" method="post" enctype="multipart/form-data">
<tags:file name="fileUpload" label="Choose File"/>
<tags:submit value="Upload"/>
</tags:form>
struts.xml
<action name="fileUpload" class="com.hibernate.action.FileUploadAction">
<interceptor-ref name="fileUploadStack"/>
<interceptor-ref name="fileUpload">
<param name="maximumSize">1024000</param>
<param name="allowedTypes">application/pdf</param>
</interceptor-ref>
<result name="success">/viewChapters.jsp</result>
</action>
FileUploadAction.java
FileUploadAction.java
public class FileUploadAction extends ActionSupport
{
private File fileUpload;
private String contentType;
private String fileName;
private String destPath;
/// setter and getter methods
public String execute()
{
destPath="C:\WebPortal_testing";
try
{
System.out.println("Source File Name:"+fileUpload);
System.out.println("Destination File Name:"+fileName);
File destFile= new File(destPath,fileName);
FileUtils.copyFile(fileUpload, destFile);
}
catch(IOException exception)
{
exception.printStackTrace();
return ERROR;
}
return SUCCESS;
}
当我在 index.jsp 页面中选择一个 pdf 文件并单击上传按钮时,它会为操作类的 fileUpload 字段提供空值.
when I select a pdf file in the index.jsp page and click on upload button it is giving null value to the fileUpload field of the action class.
我正在调试模式下执行应用程序并给出了这个
I am executing the application in debug mode and gave this
System.out.println("Source File Name:"+fileUpload);
检查它返回的内容,我得到空值.
to check what it is returning and I am getting null.
推荐答案
1.拦截器配置错误
FileUploadStack 是:
<!-- Sample file upload stack -->
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
</interceptor-stack>
那么你真正定义的是:
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
<interceptor-ref name="fileUpload">
<param name="maximumSize">1024000</param>
<param name="allowedTypes">application/pdf</param>
</interceptor-ref>
使用
- 文件上传拦截器的两倍
- 仅将您对 maximumSize 和 allowedTypes 的限制应用于第二个.
做吧
<interceptor-ref name="fileUploadStack">
<param name="fileUpload.maximumSize">1024000</param>
<param name="fileUpload.allowedTypes">application/pdf</param>
</interceptor-ref>
<小时>
<强>2.文件属性错误
内容类型和文件名属性必须以文件属性名开头.
Content type and file name attributes must start with the File attribute name.
在你的情况下:
private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;
您可以在 this question 上找到完整示例.
You can find a full example on this question.
3.您正在打印文件而不是文件名
System.out.println("Source File Name:"+fileUpload);
那是文件,而不是文件名,顺便说一句,文件名是在另一个变量中传递的.
That is the file, not the filename, and btw the filename is passed in the other variable.
修复此问题并重试.另请注意,当全世界都在使用 <s:
时,使用 <s:
.
Fix this and retry. Also note that is not safe to use <tags:
as prefix when the whole world is using <s:
. There's no gain in doing that, only complications. Just use <s:
.
相关文章