1

I'm using Infragistics IgniteUI igUpload on my webpage to upload files to our Apache Tomcat server, and while the files are uploaded, I can't get access to them in my servlet code. Maybe someone here has an idea of whats happening.

When igUpload sends the file through Ajax, I see a nicely formed POST request with Headers :

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length  24804
Content-Type    multipart/form-data;  boundary=---------------------------91823184012919
Cookie  JSESSIONID=BB4D29EEA9C703CA529EA48E74413A72
Host    localhost:8080
Referer http://localhost:8080/MyApp.html
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0

and the users file in the Post section with this 'header' :

-----------------------------91823184012919 Content-Disposition: form-data;
 name="uploadDoc_0__frm_if"; filename="UsersFile.xls" Content-Type: application/vnd.ms-excel

My servlet then gets invoked, but when I use this (code snippet from FileUpload doc):

                DiskFileItemFactory factory = new DiskFileItemFactory();
                // Configure a repository (to ensure a secure temp location is used)
                ServletContext servletContext = this.getServletConfig().getServletContext();
                File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");                   
                factory.setRepository(repository);                                       
                // Create a new file upload handler                   
                ServletFileUpload upload = new ServletFileUpload(factory);                                       
                // Parse the request                   
                List<FileItem> items = upload.parseRequest(request);

The "items" list is ALWAYS empty. However, in Eclipse debug I can SEE buried deep in the Request object a DiskFileItem that points to the users file, which was stored in a temporary directory. From this post File upload with ServletFileUpload's parseRequest? it seems like struts2 is taking the file before my servlet gets a chance to. So I looked into struts2 FileUploadInterceptor and didn't find much. I tied to overwrite it with my own Interceptor and it never gets called. I got the FileUpload source so I could set breakpoints and it is never called during this fileUpload process. The first code I see executing is my servlet and by that point the file is already processed.

So WHAT is actually eating/uploading/storing my file, and how do I either stop it, or work with it so I can actually use the file that was uploaded?

Thanks for any insight.

================================EDIT ==================================

I added the struts.xml constant from @Andrea Ligios answer below for my servlet and that didn't work. Then I checked web.xml file and it DID have those filter lines except it was

org.apache.struts2.dispatcher.FilterDispatcher

However I noticed that my struts version is 2.3.4, which should have the NEWER name you listed in your answer. I changed the filter name to the new one you specified and now it works!

However it appears I have to do a lot of work by hand. The reason I'm not using the built-in is because I'm using Infragistics jQuery igUpload control described here http://www.igniteui.com/file-upload/overview and here http://help.infragistics.com/Doc/jQuery/2014.2/CLR4.0?page=igUpload_igUpload.html . When using this control it sends the POST msg as I described in my question. I guess its made to work with ASP.NET MVC Helper and I don't even know what that is. Infragistics IgniteUI never mentions struts in their documentation. Given that I'm using igUpload, can you make any other suggestions for making this easier?

Community
  • 1
  • 1
user3708842
  • 543
  • 8
  • 23
  • You have nothing to do if your request is already handled. You can also change the default implementation. – Roman C Feb 03 '15 at 19:01
  • Thx for the reply, but why is it already handled? Who is handling it? And how can I override the default implementation? In some server code (servlet or whatever) I need to move that temp file to another location, and update a database. – user3708842 Feb 03 '15 at 21:41
  • It's handled by Struts2 filter. You should not use the same thing twice. Either you use Struts2 file upload or without Struts2. – Roman C Feb 04 '15 at 09:01

1 Answers1

1

If you are using Struts2, you will probably have this setting in web.xml:

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Since the url-pattern is /*, it means that every request is intercepted by the StrutsPrepareAndExecuteFilter (usually called Filter Dispatcher, that was also the old filter name, in Struts versions before 2.1.8).

If you call an action, this is perfect. If you call a servlet, a web-service, or something else, this is problematic, because Struts Filter should run for actions only.

To exclude a particular url (or a particular pattern) from being intercepted by the filter, you need to use the constant struts.action.excludePattern in struts.xml, as described in

Then put in struts.xml

<constant name="struts.action.excludePattern" value="/YourServlet"/>

And it should work.


At this point, I'm curious to know why are you using Struts2 without exploiting the great inbuilt File Upload functionality, that is explained in

and that can work with other upload-oriented frameworks with minor adjustments, like demonstrated in:

I don't know the plugin you're using, but in your case the first (and probably only) problem I see is the name of the parameter sent out:

uploadDoc_0__frm_if

should be unnumbered, and it'll still need a mixed CamelCase / Snake_case approach in your variable name.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thanks a lot for the detailed info. I updated my question with results from trying your suggestions. Please read that and any other comments you have would be appreciated. – user3708842 Feb 05 '15 at 13:50
  • You're welcome. I'm glad this helped you to solve your problem(s) (the deprecated Filter and the exclusion pattern for non-action URLs). Now the project is ready to work with external URLs like Servlets, no matter the Upload widget you will use. That said, I've looked now at that plugin, and that is for .NET, not for Java. It is not only not-Struts2 ready, it is not Java ready, so drop it ASAP and choose another amongst the many good UI plugins out there, or use the inbuilt File Upload Interceptor. Please remember to upvote and accept the answer, thank you – Andrea Ligios Feb 05 '15 at 14:07
  • Hhhmmm... thanks for the opinion, but its not what I wanted to hear. I am a Java/DB2 programmer and inherited this web app that used Infragistics and struts2/Java, which apparently don't go together. We have/purchased a license for Infragistics, so I can't drop that. But it sounds like we should/have to move away from of Java to ASP.NET. And that sounds costly in time, money, and learning curve. For now I think I just have to make this work somehow as is... – user3708842 Feb 05 '15 at 15:01
  • 1) Moving from Java to .NET because someone has (mistakenly) bought a license of a product ".NET oriented" is like saying you need to sell your Gasoline Car and buy a Diesel Car because someone has bought 100 liters of Diesel Fuel instead of Gasoline. 2) If you inherited a web-application *that* problematic, you should stop and compare the pros and cons of speding money to adapt and continue the business (of that application) as usual, and pros and cons of spending money to refactor it. It *could* be lower than the money lost in the license. 3) a jQuery plugin should be server-side agnostic -> – Andrea Ligios Feb 05 '15 at 15:49
  • -> , but this seems to be .NET oriented. I'm pretty sure that with *enough* work and *enough* money it can be refactored (if the license allows to alter its source code) to work well with Java and with Struts2. It's always a matter of where to spend time, effort and money with the highest possible ROI. BTW, good luck with that. P.S: If I would you, I'd blame the guy who bought the license, along with the previous team that have chosen a .NET oriented plugin for a Java product. – Andrea Ligios Feb 05 '15 at 15:52