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("Malfor medURLExce ption " + e );
}
connectionForUpload = null;
try
{
connectionForUpload = (HttpsURLConnection) productURLCSV.openConnecti on();
}
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.setHos tnameVerif ier( DO_NOT_VERIFY1 );
connectionForUpload.setDoI nput(true) ;
connectionForUpload.setDoO utput(true );
connectionForUpload.setAll owUserInte raction(fa lse);
try
{
connectionForUpload.setReq uestMethod ("POST");
}
catch( ProtocolException e )
{
System.out.println(" ProtocolException ");
}
connectionForUpload.setUse Caches(fal se);
connectionForUpload.setReq uestProper ty("conten t-type", "application/zip");
//connectionForUpload.setR equestProp erty("cont ent-dispos ition", "form-data;name=theFile;fi lename=upl oad.zip");
PrintWriter sender = null;
OutputStream ops = null;
System.out.println( connectionForUpload );
try
{
ops = connectionForUpload.getOut putStream( );
}
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.getInp utStream() ;
}
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.getPropertySy stemParame terValue(p ropertyId, "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("Uplo adAction:p rocessUplo adFile - Missing CSV upload location in DB for Property Id "+propertyId);
File uploadedFile = new File(fileUploadDir+File.se parator+up loadFileNa me);
if(uploadedFile.exists() && (!toBeOverwritten))
throw new DataInvalidException("Uplo adAction:p rocessUplo adFile - Specified file already exists");
try
{
if(size > 0)
{
byte memoryfile[] = theFile.getFileData();
FileOutputStream diskFileStream = new FileOutputStream(uploadedF ile);
diskFileStream.write(memor yfile);
diskFileStream.close();
if(size != uploadedFile.length())
throw new DataCorruptException("Uplo adAction:p rocessUplo adFile - Error writing file "+uploadFileName);
}
else
throw new DataCorruptException("Uplo adAction:p rocessUplo adFile - Specified file "+uploadFileName+" not found");
}catch(FileNotFoundExcepti on fnfe){
throw new DataCorruptException("Uplo adAction:p rocessUplo adFile - Specified file "+uploadFileName+" not found");
}catch(IOException ioexception){
throw new DataCorruptException("Uplo adAction:p rocessUplo adFile - Error reading file "+uploadFileName);
}
}
/************* Server end ***************/
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("Malfor
}
connectionForUpload = null;
try
{
connectionForUpload = (HttpsURLConnection) productURLCSV.openConnecti
}
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.setHos
connectionForUpload.setDoI
connectionForUpload.setDoO
connectionForUpload.setAll
try
{
connectionForUpload.setReq
}
catch( ProtocolException e )
{
System.out.println(" ProtocolException ");
}
connectionForUpload.setUse
connectionForUpload.setReq
//connectionForUpload.setR
PrintWriter sender = null;
OutputStream ops = null;
System.out.println( connectionForUpload );
try
{
ops = connectionForUpload.getOut
}
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
//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=
//sender.close();
try
{
ops.flush();
ops.close();
}
catch( IOException e )
{
System.out.println(" IOException " + e );
}
BufferedReader reader;
InputStream ips = null;
try
{
ips = connectionForUpload.getInp
}
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
throws DataCorruptException, DataMissingException, DataInvalidException
{
String uploadFileName = theFile.getFileName();
int size = theFile.getFileSize();
String fileUploadDir
= StaticLookup.getPropertySy
if(fileUploadDir.indexOf("
fileUploadDir.replace('\\'
if(fileUploadDir.indexOf("
fileUploadDir.replace('/',
// throw DataMissingException if the upload directory for CSVs for given property is not available
if(fileUploadDir.equals(""
throw new DataMissingException("Uplo
File uploadedFile = new File(fileUploadDir+File.se
if(uploadedFile.exists() && (!toBeOverwritten))
throw new DataInvalidException("Uplo
try
{
if(size > 0)
{
byte memoryfile[] = theFile.getFileData();
FileOutputStream diskFileStream = new FileOutputStream(uploadedF
diskFileStream.write(memor
diskFileStream.close();
if(size != uploadedFile.length())
throw new DataCorruptException("Uplo
}
else
throw new DataCorruptException("Uplo
}catch(FileNotFoundExcepti
throw new DataCorruptException("Uplo
}catch(IOException ioexception){
throw new DataCorruptException("Uplo
}
}
/************* Server end ***************/
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)
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)
> 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.
> 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)
this won't work with FormFile at all
> the zip file in a FormFile (a struts abstraction)
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, 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.getOutputSt ream() and print the data at the client
"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.getOutputSt
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.
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
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.
You need to rewrite your client to use "multipart/form-data" encoding.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
much simpler solution will be to use "normal" POST (application/x-www-form-ur
"name=aa.txt&data=" + URLEncoder.encode(fileByte