Uploading image using Struts

前端 未结 3 1492
梦如初夏
梦如初夏 2021-01-27 09:08

I just started practicing struts, so I\'m kinda new to this framework. So, what I\'m trying to do is upload an image using this JSP file :



        
3条回答
  •  灰色年华
    2021-01-27 09:57

    1. Change your input name to begin with a lowercase character:

      
      
    2. Then in Action you need to prepend the File variable's name to the contentType and FileName Strings, as follows:

      private File image;
      private String imageContentType;
      private String imageFileName;
      
      /* GETTERS AND SETTERS FOR ALL OF THEM */
      

    You may also being interested in how to configure the maximum size for a single file (and for the entire request), allow only certain kind of files to be uploaded, or upload multiple files at once.

    EDIT

    You didn't post your struts.xml and web.xml configuration, but the line of the stacktrace

    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)

    unveils that you are using the wrong Filter.

    Also the File Upload Interceptor seems to be configured to run twice... and this usually happens when configuring it the wrong way, like

    
    
        2097152
        
            image/png,image/gif,image/jpeg,image/pjpeg
                    
    
    
    

    instead of

    
    
        2097152
        
            image/png,image/gif,image/jpeg,image/pjpeg
                    
    
    

    Again, check carefully your configuration both in web.xml and struts.xml, it will work automatically.

提交回复
热议问题