I've been reading a lot in the past 3 days, trying to figure out a way to solve my problem, Is it posible to change upload path with a servlet?, I've tried many many ways, and now I understand a lot more about Java, but I still haven't been able to find a solution to my problem.
The closest to a work around I've found is using a filter and HttpServletRequestWrapper and HttpServletResponseWrapper, but my problem is, like a lot of people has asked in here, is that after getting the response once, it is gone, many suggest using the wrappers, but the wrapper is gone too after 1 use.
My code trying to achieve a very simple working example:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) {
HttpServletRequest httpReq = (HttpServletRequest) request;
HttpServletRequestWrapper reqWrapper = new HttpServletRequestWrapper(httpReq);
List<FileItem> items = upload.parseRequest(reqWrapper); //<--All cool
filterChain.doFilter(reqWrapper, response); //<--reqWrapper still
//has request but when I try to get the file
//through org.apache.commons.fileupload.FileUpload, the file is null
}
Also if I try it like the example explained here: Differences between ServletResponse and HttpServletResponseWrapper?
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) {
HttpServletRequest httpReq = (HttpServletRequest) request;
HttpServletRequestWrapper reqWrapper = new HttpServletRequestWrapper(httpReq);
filterChain.doFilter(reqWrapper, response); //<--All cool
//**It goes and does an action, which gets Form parameters and file from a POST request and saves file to folder within context**
List<FileItem> items = upload.parseRequest(reqWrapper);//<--throws exception:
//org.apache.commons.fileupload.FileUploadException: Stream Closed
}
I have also implemented classes for my wrappers and they work fine, but still, when I try to use the request again in the same instance, its gone. What's the proper approach to do this, or is it impossible to do?
I only dare to ask this risking negative votes because I've spend over 40 hours trying to find a solution to this, but I simply can't figure it out. Thanks for patience and understanding.
EDIT: Forgot to mention that I'm doing this to get the input values and a file from a post request multipart data
EDIT2: Changed comments on code to try to be more clear.
EDIT3: Changed comments again to show stack trace exceptions
What I'm trying to achieve is to use the form POST data twice. This is to replicate the uploaded file without having to modify the original action(servlet). I thought HttpServletRequestWrapper, would wrap my POST Data from my form, so that I could reuse it, but now I understand it doesn't.
Is there anyway to achieve this?