Link to home
Start Free TrialLog in
Avatar of shayad
shayad

asked on

Simulating HTTP Post.

Hello,
I am developing a small utility, which will post a zip file to an application running on TOMCAT server.
On the server end, I have a listener which expects the zip file in a FormFile (a struts abstraction). The form field name for the is called "theFile".

On the client/utility end, I open a HttpsURLConnection, write the contents of the zip file into the stream and then close the connection. But, I do not know how to associate the name of the zip file with the contents I upload/write onto the stream. I am not sure, if header settings are correct as well.

I am pasting both the client and server code below. Thanks in advance.

/************** Client end ***************/

public void uploadCSV()
      {

                  try
                  {
                        productURLCSV = new URL("https://192.168.2.14:8443/productx/UploadCSV.do");
                  }
                  catch( MalformedURLException e )
                  {
                        System.out.println("MalformedURLException " + e );
                  }

                  connectionForUpload = null;
                  try
                  {
                        connectionForUpload = (HttpsURLConnection) productURLCSV.openConnection();
                  }
                  catch( IOException e )
                  {
                        System.out.println(" IOException in openConnection" + e );
                  }


                  /*
                        the Default verify method in JSSE seems to be failing. Provide an override, which will
                        always return true for the verify method.
                  */
                  MyHostnameVerifier DO_NOT_VERIFY1 = new MyHostnameVerifier();

                  connectionForUpload.setHostnameVerifier( DO_NOT_VERIFY1 );

                  connectionForUpload.setDoInput(true);

                  connectionForUpload.setDoOutput(true);

                  connectionForUpload.setAllowUserInteraction(false);

                  try
                  {
                        connectionForUpload.setRequestMethod("POST");
                  }
                  catch( ProtocolException e )
                  {
                        System.out.println(" ProtocolException ");
                  }

                  connectionForUpload.setUseCaches(false);

                  connectionForUpload.setRequestProperty("content-type", "application/zip");

                  //connectionForUpload.setRequestProperty("content-disposition", "form-data;name=theFile;filename=upload.zip");

                  PrintWriter sender = null;
                  OutputStream ops = null;

                  System.out.println( connectionForUpload );

                  try
                  {
                        ops = connectionForUpload.getOutputStream();
                  }
                  catch( IOException e )
                  {
                        System.out.println(" IOException getOutputStream " + e );
                  }

                  sender = new PrintWriter( ops );

                  /* open the zip file and write it to the stream */
                  File newFile = new File("upload.zip");
                  FileInputStream fin = null;
                  try
                  {
                        fin = new FileInputStream(newFile);
                  }
                  catch( FileNotFoundException e )
                  {
                  }

                  BufferedInputStream in = new BufferedInputStream(fin);
                  //Create a buffer for reading raw bytes from the file.
                  byte[] buf = new byte[1024];
                  //The len variable will keep track of how much we
                  //are able to actually read each time we try.
                  int len;
                  int totalSize = 0;

                  //Read from the file and write to the gzip archive.
                  try
                  {
                        while ( (len = in.read(buf)) >= 0 )
                        {
                               //diskFileStream.write(buf,0,len);
                               //sender.write(buf,0,len);
                               ops.write(buf,0,len);
                               totalSize += len;
                        }
                  }
                  catch( IOException e )
                  {
                        System.out.println(" IOException " + e );
                  }
                  System.out.println( totalSize + " bytes have been written to the stream " );


                  //sender.println("theFile=upload.zip");
                  //sender.close();

                  try
                  {
                        ops.flush();
                        ops.close();
                  }
                  catch( IOException e )
                  {
                        System.out.println(" IOException " + e );
                  }



                  BufferedReader reader;
                  InputStream ips = null;
                  try
                  {
                        ips = connectionForUpload.getInputStream();
                  }
                  catch( IOException e )
                  {
                        System.out.println(" IOException getInputStream " + e );
                  }

                  reader = new BufferedReader( new InputStreamReader( ips ) );

                  try
                  {
                        String returnMessage = reader.readLine();
                        System.out.println( "ReadLine returned :" + returnMessage );
                  }
                  catch( IOException e )
                  {
                        System.out.println(" IOException readline");
                  }

                  try
                  {
                        reader.close();
                  }
                  catch( IOException e )
                  {
                        System.out.println(" IOException close");
                  }

      }

/************** Client end ***************/

/************* Server end ***************/
private void processUploadFile(FormFile theFile, int propertyId, boolean toBeOverwritten)
            throws DataCorruptException, DataMissingException, DataInvalidException
      {
        String uploadFileName = theFile.getFileName();
            int size = theFile.getFileSize();

            String fileUploadDir
                  = StaticLookup.getPropertySystemParameterValue(propertyId, "PMS_INTG", "csvLocation");
            if(fileUploadDir.indexOf("\\") != -1)
                  fileUploadDir.replace('\\', File.separatorChar);
            if(fileUploadDir.indexOf("/") != -1)
                  fileUploadDir.replace('/', File.separatorChar);

      // throw DataMissingException if the upload directory for CSVs for given property is not available
            if(fileUploadDir.equals(""))
                  throw new DataMissingException("UploadAction:processUploadFile - Missing CSV upload location in DB for Property Id "+propertyId);

            File uploadedFile = new File(fileUploadDir+File.separator+uploadFileName);

            if(uploadedFile.exists() && (!toBeOverwritten))
                  throw new DataInvalidException("UploadAction:processUploadFile - Specified file already exists");

            try
            {
                  if(size > 0)
                  {
                        byte memoryfile[] = theFile.getFileData();
                        FileOutputStream diskFileStream = new FileOutputStream(uploadedFile);
                        diskFileStream.write(memoryfile);
                        diskFileStream.close();
                        if(size != uploadedFile.length())
                              throw new DataCorruptException("UploadAction:processUploadFile - Error writing file "+uploadFileName);
                  }
                  else
                        throw new DataCorruptException("UploadAction:processUploadFile - Specified file "+uploadFileName+" not found");

            }catch(FileNotFoundException fnfe){
                  throw new DataCorruptException("UploadAction:processUploadFile - Specified file "+uploadFileName+" not found");
            }catch(IOException ioexception){
                  throw new DataCorruptException("UploadAction:processUploadFile - Error reading file "+uploadFileName);
            }
      }

/************* Server end ***************/



Avatar of heyhey_
heyhey_

in your case you will need to upload "multipart/form-data" POST request ... which is somewhat complicated.

much simpler solution will be to use "normal" POST (application/x-www-form-urlencoded). all you have to do is to send


"name=aa.txt&data=" + URLEncoder.encode(fileBytes);

for example on HTTPClient does it check

http://www.innovation.ch/java/HTTPClient/FAQ.html

http://www.innovation.ch/java/HTTPClient/api/HTTPClient/Codecs.html#mpFormDataEncode(HTTPClient.NVPair[], HTTPClient.NVPair[], HTTPClient.NVPair[])

(HTTPClient is URLConnection alternative, you can use it directly in your application)
Avatar of Mick Barry
> I do not know how to associate the name of the zip file
> with the contents I upload/write onto the stream.

just pass the name in the url and pass the data as you are already doing.
you could also pass the file details using the stream that you pass the data over, but in the url is probably easiest.
> just pass the name in the url and pass the data as you are already doing.

this won't work with FormFile at all

> the zip file in a FormFile (a struts abstraction)
Avatar of shayad

ASKER

Thanks for all the comments.

I have tried the approach suggested by heyhey, but it does not seem to work at all.

There are some observations though.
1. The response code is 200, which implies a success.

2. There is no data in the inputstream of the connection. I do not know what this means.

3. How can I figure out th response of the Server. My progress is slow because I am unable to figure out what is happening at the server end.

> I have tried the approach suggested by heyhey,


"normal" POST  ?

please post your code

> 2. There is no data in the inputstream of the connection. I do not know what this means.

not normal

> 3. How can I figure out th response of the Server. My progress is slow because I am unable to figure out what is happening at the server end.

you can use System.out.println() to print in server .log files or you can write to ServletRequest.getOutputStream() and print the data at the client

Avatar of shayad

ASKER

Hello again,
Thanks for you comments.
I have used normal POST. I have another observation:

If, I use a conventional browser to connect to my server, then the server prints out various messages in the log files such as "user shayad has been logged into the system".
However, if I use my own HttpClient, then no message is printed in the log files. TOMCAT's log files simply shows a message "session started [Timestamp]" and "session Terminated [TimeStamp]". I guess, these are printed in response to the URL.getConnection() API calls. But, I do not see any aplication specifc messages at all.

I guess, my login is failing to start with. I guess, the server should respond to a login request irrespective of the client it is talking to (the browser or my utility). Is it imperative for the server to return to response code. Thus, an empty return code is a sure shot sign that something is amiss.
But, the response code is 200, which implies a successful connection. I wonder why is the input stream empty.

SOS
Shayad.

PS:
I am working from home and do not have access to my code right away, but nothing has chnaged and all the core stuff is available in the question I posted y'day.
to sumamrize

1. you want to upload file from Java application to servlet / JSP page
2. my suggestion is to encode everything in a name/value pairs and upload it with HTTP POST.
3. you will have to create dedicated scipt that handles that POST (decodes the parameters, write data to file if needed).

try this technique on a simple servlet outside of your main code
Missed the struts part sorry :)

You need to rewrite your client to use "multipart/form-data" encoding.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the points :)
objects@idg.
        I attempted you code, but I am having a problem uploading zip file created with WinZip, it seem one uploaded the resultant files are corrupt. They seem to be off by 2 bytes in size. Any suggestions. I compiled the code with Jdk 1.3.1

Thanks.