Quantcast
Viewing all articles
Browse latest Browse all 21760

Re: binary data in a form field gets changed

I have found a possible workaround. Follow these steps to use a servlet to implement a form-handler in ColdFusion 10, for a form whose method is 'post' and enctype 'multipart /form-data':

 

1) Download the libraries to be used, namely, commons-fileupload-1.3.1.jar and commons-io-2.4.jar;

 

2) Copy these 2 JAR files to your ColdFusion installation's /WEB-INF/lib/ directory;

 

3) In the following code, replace C://temp// with an existing path on your file system, to indicate where form data is to be stored. Save the code as FormHandlerServlet.java. Compile the file, and save FormHandlerServlet.class into the directory /WEB-INF/lib/classes/;

 

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import org.apache.commons.fileupload.*;

import org.apache.commons.fileupload.servlet.*;

import org.apache.commons.fileupload.util.Streams;

 

//@WebServlet(name="FormHandlerServlet", urlPatterns={"/FormHandlerServlet"})

public class FormHandlerServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request,

            HttpServletResponse response) throws ServletException, IOException {      

        response.setContentType("text/plain;charset=UTF-8");

       

        try {

            doUpload(request,response);

        }

        catch(FileUploadException fue) {

            fue.printStackTrace();

        }

    }

    protected void doUpload (HttpServletRequest req, HttpServletResponse resp) throws FileUploadException, IOException {

        FileWriter outFile = new FileWriter("C://temp//formFieldData");

        PrintWriter writer = new PrintWriter(outFile);

 

        // Create a new file upload handler

        ServletFileUpload upload = new ServletFileUpload();

               

            // Parse the request

            FileItemIterator iter = upload.getItemIterator(req);

      

                while (iter.hasNext()) {

                    FileItemStream item = iter.next();

                    InputStream stream = item.openStream();

                               

                    if (item.isFormField()) {

                        /* Case where form field is not 'file' type: write string to output */

                        writer.print(Streams.asString(stream));                        

                        writer.close();

                       

                    } else {

                        /* Code to handle case where file is uploaded */            

                    }

                }

          

    }

}

 

4) Create a back-up of /WEB-INF/web.xml. This will enable you to revert to the original set-up.

 

Open web.xml in a text editor. Add the following between the last </servlet> end-tag and the first <servlet-mapping> start-tag:

 

<servlet>

<display-name>FormHandlerServlet</display-name>

<servlet-name>FormHandlerServlet</servlet-name>

<servlet-class>FormHandlerServlet</servlet-class>

</servlet>

 

<servlet-mapping>

<servlet-name>FormHandlerServlet</servlet-name>

<url-pattern>/FormHandlerServlet</url-pattern>

</servlet-mapping>

 

5) One consequence of these settings is that you should require your clients to post form data to the action-page "/FormHandlerServlet";

 

6) Restart ColdFusion.

 

7) When a client submits binary data as before, it will be saved as the file formFieldData.


Viewing all articles
Browse latest Browse all 21760

Trending Articles