0

I've a requirement in reading two uploaded text/csv files separately using REST ws(Jersey), below a sample snippet that depicts the same.

    @Path("bcmfileinput")
    public class BCMFileInputCheckResource{

        @POST
        @Path("survivour1")
        @Consumes(MediaType.MULTIPART_FORM_DATA)
        @Produces(MediaType.TEXT_PLAIN) 
        public String getSurvivour1File(@FormDataParam(value="eaiTradesFile")InputStream eaiTradesStream,@FormDataParam(value="outsandingFile")InputStream outTradesStream) throws IOException{
            //writing eaiTradesStream to the file to test
            BufferedReader br = new BufferedReader(new InputStreamReader(eaiTradesStream,"UTF-8"));
            PrintWriter f=new PrintWriter(new File("H:/abc.csv"));
            String line="";
            while((line=br.readLine())!=null  ){
                f.write(line);
                f.println();
            }
            f.flush();
            f.close();
    return "H:/abc.csv";
        }

And a sample html page:

<html>
<body>
    <h2>BCM File Upload</h2>

    <form action="http://localhost:9080/swiftmx/public/bcmfileinput/survivour1" method="post" enctype="multipart/form-data">

       <p>
        Select Eai Positions file : <input type="file" name="eaiTradesFile" size="45" />
        <br>
        <br>
        Select Outstanding  file : <input type="file" name="outsandingFile" size="45" />
        </p>

       <input type="submit" value="Upload It" />
    </form>

</body>
</html>

With the above code, when i write any one of stream to the file then both the files data being printed. But, my requirement is need to bind eaiTradesFile to 1st inputstream and outsandingFile file to 2nd inputstream as I've to process them separately for each file.

Been googling failed to find right solution for my requirement. So, any solution is appreciable for the same, Thank all in advance!!!

  • Is it _just_ the contents of both files, or is there extra content along with the file contents? – Paul Samsotha Oct 25 '18 at 06:27
  • @PaulSamsotha it is just content(and need avoid headers also if possible) of the files(csv file) need to process separately as first file content is dealing with one functionality and 2nd file content is dealing with another functionality. –  Oct 25 '18 at 06:44
  • What do you mean _"and need avoid headers also if possible"_? So there are headers in the content? [this](https://stackoverflow.com/a/37537693/2587435) is what a raw multipart request body looks like. Is this what you are seeing? If so, then it sounds like you just need to register the `MultiPartFeature` with your Jersey application. – Paul Samsotha Oct 25 '18 at 06:45
  • Yea it means you are getting the whole multipart body. You need to register the MultiPartFeature. You can add it to the getClasses() or getSingletons(). Either one will work. – Paul Samsotha Oct 25 '18 at 13:37
  • Thanks @PaulSamsotha, I tried registering MultiPartFeature to getClasses() but unfortunately there is no change in the result. –  Oct 25 '18 at 14:14
  • Well then there is something else wrong that you are not showing. Do you have a GitHub repo you can share? – Paul Samsotha Oct 25 '18 at 16:57
  • @PaulSamsotha I'm sorry as i can't share GitHub repo nd while dealing with multiple options of reading data I'm getting exception like, [ERROR ] No message body reader has been found for class org.glassfish.jersey.media.multipart.FormDataBodyPart, ContentType: multipart/form-data;boundary=--------------------------332263186541188574485383. even after registering MultiPartFeature with Applicaton class's getSingletons(). Can you guess where i might mistaken in the code? –  Oct 26 '18 at 09:18
  • There are many things I can think of that would cause this not to work. I don't want to just list everything. Can you provide more information, like 1) The imports of your resource classes. 2) The dependencies for your project. 3) Your application configuration class. 4) If you are using a web.xml, provide that 5) The server you are using. And anything else you can think of that you think might help. – Paul Samsotha Oct 27 '18 at 05:48

0 Answers0