The Artima Developer Community
Sponsored Link

Java Answers Forum
IO streams....pl. help me....going to meet the deadline...

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
sakthivel

Posts: 7
Nickname: sakthivel
Registered: Oct, 2002

IO streams....pl. help me....going to meet the deadline... Posted: Nov 10, 2002 3:30 AM
Reply to this message Reply
Advertisement
Dear All,
I have a problem over here, in the transfer of file from my client machine to the server machine....I am zipping the file in the client machine and sending to the server machine....In the server machine I am trying to unzip it and write it over there....

For contacting my server, initially i am getting the authentication.....it is all correct....no problem with the authentication work...the problem over here is.............i am having 3 files to be transfered to my server machine...the first file is getting transfered....after that the command prompt is just standing idle....it is not even coming to the prompt state...it is not even thowing any errors in the server console or in the client machine....


Below is my complete code....

Client program: (simple java program)
****************************
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.io.*;
import java.text.*;
import java.util.*;
import java.net.*;
import java.sql.*;

class testprogram
{

HttpURLConnection urlconnection = null;
public static void main(String args[])
{
String file1="F:/Domino/Data/Applications/Groupware/HP/input/Invcor02/param/new1.txt";
String file2="F:/Domino/Data/Applications/Groupware/HP/input/Invcor02/param/new2.txt";
String file3="F:/Domino/Data/Applications/Groupware/HP/input/Invcor02/param/new3.txt";

String name1="new1.txt";
String name2 ="new2.txt";
String name3="new3.txt";
testprogram tt=new testprogram();
try
{
tt.makeConnection();
tt.zipper(file1,name1);
tt.zipper(fil e2,name2);
tt.zipper(file3,name3);
tt.disconnectConnection();
}
catch(Exception e){}
}

public void zipper(String file,String name) throws Exception
{
try
{
ZipOutputStream zipoutputstream = new ZipOutputStream(urlconnection.getOutputStream());
ZipEntry zipentry = new ZipEntry(name);
zipentry.setMethod(ZipEntry.DEFLATED);
zipoutputstream.putNextE ntry(zipentry);
byte bytearray[] = new byte[1024];
File filedir = new File(file);
FileInputStream fileinputstream = new FileInputStream(filedir);
BufferedInputStream bufferedinputstream = new BufferedInputStream(fileinputstream);
int length = 0;
while((length=bufferedinputstream.read(bytearray)) != -1)
{
zipoutputstream.write(bytearray,0,length);
}

fileinputstream.close();
bu fferedinputstream.close();
zipoutputstream.flush();
zipoutputstream.finish();
zi poutputstream.closeEntry();
zipoutputstream.close();
System.out.println("I am here...");

InputStream in = urlconnection.getInputStream();
while (in.read()!=-1);
}
catch(Exception exp)
{System.out.println("I am from client: " + exp);}
}

/* to make the URL connection with the server - begin */
public void makeConnection()
{
/* Authentication work is done over here - begin */
String setcookie="";
String cookie="";
try
{
URL theurl = new URL("http://192.168.10.55:8001/names.nsf?login&username=sakthivel&passw ord=12345");
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection hurl = (HttpURLConnection)theurl.openConnection();
hurl.connect();
setcookie = hurl.getHeaderField ("set-cookie");
int index=setcookie.indexOf(";");
cookie=(setcookie.substring(0,index)).trim();
hur l.disconnect();
hurl=null;
theurl=null;
}
catch(Exception exp){exp.printStackTrace();}
/* Authentication work is done over here - end */

/* to make the URL connection with the server - begin */
try
{
URL serverURL = new URL("http://192.168.10.55:8001/servlet/FileUploadServlet");
urlconnection= (HttpURLConnection)serverURL.openConnection();
urlconnection.setDoOutput(true);
urlconnection.setRequestMethod("POST");
urlconnection.setRequestProperty("Cooki e",cookie);
}
catch(Exception exp)
{System.out.println("Client machine: Error in Making Connection" + exp );}
/* to make the URL connection with the server - end */
}

/* to disconnect the URL connection with the server - begin */
public void disconnectConnection()
{
try
{
System.runFinalization();
urlconnection.disconne ct();
urlconnection.getInputStream().close();
}
catch(Exception exp)
{System.out.println("Client machine: Error gets fired while disconnecting the connection" + exp);}
}
}
/* to disconnect the URL connection with the server - end */



My Server side Program(servlet program)
*******************************
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;


public class FileUploadServlet extends HttpServlet
{

public void init() throws ServletException
{}

public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse) throws ServletException, IOException
{
try{UnzipAndWriteToDir(httpservletrequest, httpservletresponse);}
catch(Exception exp){System.out.println("I am from server............" + exp);}
}

public void UnzipAndWriteToDir(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse) throws Exception
{

javax.servlet.ServletInputStream servletinputstream = httpservletrequest.getInputStream();

ZipInputStream zipinputstream = new ZipInputStream(servletinputstream);
ZipEntry zipentry = null;
String directory="c:\\test";
try
{
File file = new File(directory);
if(!file.exists())
file.mkdirs();
}
catch(Exception exp)
{System.out.println("I am from Server: " + exp);}

try
{
zipentry = zipinputstream.getNextEntry();
if(zipentry != null)
{
int slash = zipentry.getName().lastIndexOf(File.separator) + 1;
String filename = zipentry.getName().substring(slash);
File file1 = new File(directory + File.separator + filename);
BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file1));
byte abyte0[] = new byte[1024];
for(int index = 0; (index = zipinputstream.read(abyte0)) > 0;)
bufferedoutputstream.write(abyte0, 0, index);

bufferedoutputstream.flush();
bufferedoutputstream.close();
zipinputst ream.closeEntry();
zipinputstream.close();
}
}
catch(IOException ioexception)
{System.out.println("IOException occured in the Server: " + ioexception);}
} // end of the method...

} // end of the class

P.S: Here, I am using the Domino Server, which is same as the Web Logic Server......Pl. post a reply....hanging over here for the past one week...any suggestion is highly appreciated....

Topic: Help Finding Slideshow Applet Previous Topic   Next Topic Topic: Message Error: unreported exception java.rmi.RemoteException; must be caugh

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use