Struts2 Fileupload在动作类中给出空文件 [英] Struts2 Fileupload giving null file in the action class

查看:131
本文介绍了Struts2 Fileupload在动作类中给出空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的web应用程序中使用struts2 fileUpload拦截器来实现文件上传过程。下面是我在
$ b

index.jsp中的代码

 < tags:form action =fileUploadmethod =postenctype =multipart / form-data> 
< tags:file name =fileUploadlabel =选择文件/>
< tags:submit value =上传/>
< / tags:form>

struts.xml

 < action name =fileUploadclass =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

  public class FileUploadAction extends ActionSupport 
{
private file fileUpload;
private String contentType;
private String fileName;
private String destPath;
/// setter和getter方法
public String execute()
{
destPath =C:\\WebPortal_testing;
尝试
{
System.out.println(Source File Name:+ fileUpload);
System.out.println(Destination File Name:+ fileName);

文件destFile =新文件(destPath,fileName);
FileUtils.copyFile(fileUpload,destFile);

catch(IOException异常)
{
exception.printStackTrace();
返回错误;
}
返回SUCCESS;

当我在index.jsp页面选择一个pdf文件并点击上传按钮,它将赋予操作类的fileUpload字段空值。



我正在以调试模式执行应用程序,并给了这个

  System.out.println(Source File Name:+ fileUpload); 

检查它正在返回的内容,

解决方案

拦截器配置错误

FileUploadStack 是:


$ b

 < ;! - 示例文件上传堆栈 - > 
< 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>使用







  • 两次fileUpload拦截器

  • 对maximumSize和allowedTypes的限制只能在第二个上使用。



$ b

< interceptor-ref name =fileUploadStack>
< param name =fileUpload.maximumSize> 1024000< / param>
< param name =fileUpload.allowedTypes> application / pdf< / param>
< / interceptor-ref>






2。文件属性是错误的

内容类型和文件名属性必须以File属性名称开始。



在你的情况下:
$ b

  private file fileUpload; 
private String fileUploadContentType;
private String fileUploadFileName;

您可以在这个问题




你正在打印的文件,而不是文件名


$ b

System.out.println( 源文件名:+ fileUpload);

这是文件,而不是文件名,并且btw文件名在另一个变量中传递。 / p>




修复此问题并重试。还要注意,当整个世界正在使用< s:<标记:时,使用< 。这样做没有任何好处,只有复杂性。只要使用< s:


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

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;
 }

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. Interceptor configuration is wrong

FileUploadStack is:

<!-- Sample file upload stack -->
<interceptor-stack name="fileUploadStack">
    <interceptor-ref name="fileUpload"/>
    <interceptor-ref name="basicStack"/>
</interceptor-stack>

then what you're really defining is:

    <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>

Using

  • two times the fileUpload interceptor
  • applying your limitations on maximumSize and allowedTypes only to the second.

Just do

<interceptor-ref name="fileUploadStack">
    <param name="fileUpload.maximumSize">1024000</param>
    <param name="fileUpload.allowedTypes">application/pdf</param>
</interceptor-ref>


2. File attributes are wrong

Content type and file name attributes must start with the File attribute name.

In your case:

private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;

You can find a full example on this question.


3. You are printing the File instead of the filename

System.out.println("Source File Name:"+fileUpload);

That is the file, not the filename, and btw the filename is passed in the other variable.


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:.

这篇关于Struts2 Fileupload在动作类中给出空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆