-1

I am working on a migration project from j2ee to Springboot.. I do struggle to upload a file in spring boot. As in the J2EE application "org.apache.tomcat.util.http.fileupload" is used, to upload a file, I thought as spring-boot has embedded tomcat, it is going to work, but unfortunately it doesn't. I have to work with Multipart.. I can't make it false at any time. Same thing I tried with apache common end up with same result. Here is my code .. could you please suggest me how to proceed .. your help would really appreciated..

I have two filters too.. for testing I use postman

   import org.apache.tomcat.util.http.fileupload.FileItemIterator;
    import org.apache.tomcat.util.http.fileupload.FileItemStream;
    import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
        @RestController
        @RequestMapping("/v2")
        public class PDFExtractController {
            private static final long serialVersionUID = 1L;
            private static Logger log = Logger.getLogger(PDFExtractController.class);
        //    @RequestHeader(PESConstants.CLIENT_CONTEXT) String intuit_clientcontext, // deleted from the function parameter
            @RequestMapping(value = "document/{DocType}",method =RequestMethod.POST,consumes = "multipart/form-data")
            public ResponseData fileUpload(@PathVariable("DocType") String docType,  HttpServletRequest request){
        boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
                    String provider = null;
                    String password = null;
                    boolean verbose = false;

                    if (isMultipartContent) {
                        // Grab the content
                        try {
                            ServletFileUpload fileUpload = new ServletFileUpload();
                                FileItemIterator items = fileUpload.getItemIterator(request);

                            while (items.hasNext()) {
                               FileItemStream item = items.next();
                                String fieldname = item.getFieldName();

                                if (PESConstants.PARAM_PROVIDER.equals(fieldname)) {
                                    provider = PdfServletMgr.getTextValue(item);
                                    SplunkMgr.addtoMDC(MDCFieldNames.PROVIDER.getValue(), provider.trim());
                                }
                                else if (PESConstants.PARAM_VERBOSE.equals(fieldname)) {
                                    verbose = "true".equals(PdfServletMgr.getTextValue(item));
                                }
                                if (PESConstants.PARAM_PDF.equals(fieldname) && "form".equalsIgnoreCase(docType)) {
                                    file = PdfServletMgr.getValue(item);
                                }
                                else if (PESConstants.PARAM_PASSWORD.equals(fieldname)) {
                                    password = PdfServletMgr.getTextValue(item);

                                }
                            }
                        }
                    }
              }
        }
Bam
  • 79
  • 1
  • 2
  • 11
  • I'm done with this problem... got the solution as in the answer section.. my suggestion is never use apache common or apache http fileupload rather use springbot Multipartfile if you are using spring-boot – Bam Jul 26 '19 at 10:08

1 Answers1

0

I changed my Mapping as below, and it started working...

@RequestMapping(value = "document/{DocType}",method =RequestMethod.POST)
    public ResponseData fileUpload(@PathVariable("DocType") String docType, @RequestParam("pdf") MultipartFile[] multipartfiles,HttpServletRequest request){
ResponseData responseData =  new ResponseData();
System.out.println("your code in between...");

return responseData;
}
Bam
  • 79
  • 1
  • 2
  • 11
  • Please refactor the code youve given in this whole thread, reading this is very exhausting and thus an answer has to be dug up by the reader itself, this is not how its supposed to work here. – pikkuez May 19 '20 at 07:12