Home arrow static arrow Java Programming [Archive] - 2-way file upload/download b/w client/server
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - 2-way file upload/download b/w client/server
This topic has 19 replies on 2 pages.    « Previous | 1 | 2 |

Posts:155
Registered: 6/9/03
Re: 2-way file upload/download b/w client/server  
Jul 21, 2004 3:55 PM (reply 15 of 19)



 
OK probably i repeated stuff!! i was so irritated :)

anyways Guys, thanks for ur patience. I was't expecting anybody to reply. I really appreciate ur help :)

OK now i got the key to the problem. But still a small problem, which is how should i realize the eof for each file that's being sent by the servelet. The servlet sends the files as streaming bytes as below:
my apologies for repeating ;)
servlet's side code snippet:method#1
Iterator filesToSendIterator = send2Files.iterator();            File tempSendFile = null;            while(filesToSendIterator.hasNext()){                tempSendFile = (File)filesToSendIterator.next();                sendFile(tempSendFile, outputToApplet);            }//other logic comes here////public void sendFile(File file) throws IOException{		byte[] buff = new byte[128];		int cnt = 0;				FileInputStream fromFile = new FileInputStream(file);		try{		while( (cnt = fromFile.read( buff )) > -1 ) {				outputToServlet.write( buff, 0, cnt );			}		}finally{			fromFile.close();					}	}//sendFile


matching applet code is:method#1
ObjectInputStream dump = new ObjectInputStream(connect.getInputStream());         //code to read the output files from the servlet         byte[] buff = new byte[BUF_SIZE];        int cnt = 0;        //getFileList contains the set of files that are to be sent to applet         ArrayList getFileList = (ArrayList)dump.readObject();        Iterator getFileListIterator = getFileList.iterator();         while(getFileListIterator.hasNext()){            File getFile = (File)getFileListIterator.next();            File makeFile = new File(getFile.getName());            FileOutputStream makeFileOS = new FileOutputStream(makeFile);           while( (cnt = dump.read( buff )) > -1 ){                  //to demarcate between the end of files (i.e) since i read chucks of 128 bytes. This code assumes that when the file comes to an end of file the size would be less than 128 and that's how it knows the EOF of the current file has been reached.as somebody pointed out earlier, this trick doesn't work always. [b]now i am looking for more efficient way for finding the EOF[/b]                   if(cnt<BUF_SIZE){                    makeFileOS.write(buff,0,cnt);                    makeFileOS.close();                    break;                }                         makeFileOS.write( buff, 0, cnt );                                    }            makeFileOS.close();}//while(it.next) this writes all the files sent by the servlet to client machine but becoz of improper EOF trick the contents of one file and other are merged. However if i remove the entire code with if condition. all the files are written into the first file and remaining files have 0 size. Therefore i am trying to send "EOF"+filename+"EOF" sequence to mark as the EOF delimite by slightly modifying the logic above as below servlet's side code:[b]method#2[/b][code]Iterator filesToSendIterator = send2Files.iterator();            File tempSendFile = null;            while(filesToSendIterator.hasNext()){                tempSendFile = (File)filesToSendIterator.next();                sendFile(tempSendFile, outputToApplet);                String eof="EOF"+tempSendFile.getName()+"EOF";                outputToApplet.writeUTF(eof);}// some other logic comes in between////public void sendFile(File file, ObjectOutputStream outputToApplet) throws IOException{      	byte[] buff = new byte[BUF_SIZE];	int cnt = 0;        String readline = null;FileInputStream fromFile = new FileInputStream(file);      	BufferedReader frombrd=new BufferedReader(new InputStreamReader(fromFile));		try{  		                while((readline=frombrd.readLine())!=null){                outputToApplet.writeUTF(readline);                outputToApplet.writeUTF("\n");            }              }finally{			fromFile.close();  } }


matching code at the applet's side:method#2
ObjectInputStream dump = new ObjectInputStream(connect.getInputStream());
byte[] buff = new byte[BUF_SIZE];
int cnt = 0;
String readline=null;
String eof=null;
ArrayList getFileList = (ArrayList)dump.readObject();
Iterator getFileListIterator = getFileList.iterator();
while(getFileListIterator.hasNext()){
File getFile = (File)getFileListIterator.next();
File makeFile = new File(getFile.getName());
FileOutputStream makeFileOS = new FileOutputStream(makeFile);
BufferedWriter bufferedFile = new BufferedWriter(new OutputStreamWriter(makeFileOS));
eof = "EOF"getFile.getName()"EOF";
while((readline = dump.readUTF())!= null){
if(readline.equals(eof)){
System.out.println(readline);
break;
}
bufferedFile.write(readline);
}
makeFileOS.close();
}//while(it.next)

Now this recognizes the EOf marker but still doesn't work quite as expected. for some reason the whole of first file is has dissappeared and second file only 3/4 is received at the client's end!!

could u please suggest any improvisations on this code

Thanks
sri
 

Posts:1,111
Registered: 1/13/04
Re: 2-way file upload/download b/w client/server  
Jul 22, 2004 5:19 AM (reply 16 of 19)



 
OK.

What I would like to do is take this discussion up a level, because I think your problem is probably a design problem, not a coding problem. I will tell you in advance that I did not read your code very closely, so if I go astray here, well, complain.

It feels like your design is a little bit mixed up in that the "control" design may depend on mechanisms that can't work.

1. You want to use Object streams. That is, you open stream communications between client and server, and expect that the server will write objects, and the client will read objects. (I use "client" and "server" here loosely - simply to mean "receiver" and "sender".)

There is an implication here: that the client always will know which kind of object is coming next from the server.

There is another implication here: that at the lowest level of input - the bytes coming off the wire - that your program KNOWS when to keep looping and getting more data. (In other words, getting back a -1 does NOT necessarily mean that your receiver has gotten all the data.)

2. Above the level of objects, the data you are transferring is really "files". You handle this - I guess - by making the first object transferred be an array of file info. (I saw that names are transferred; maybe there is more.)

My impression is that you are counting on this file info to control the transfer of the data for each file; that is, you iterate through the header array, trying to read data blocks for each file.

3. But now you have a problem: You don't know a) How many data blocks to read for each file, b) How many data blocks are in a transmission, and c) how to differentiate temporary lack of input data from end of transmission.

////

One way you might be able to get real control is to put the file byte size into your file array. If that data is part of the "header", you can pass it to your file reader, which can figure out when it has read the last data object - 128-byte array - for a given file. And when the last data object has been read, then you are done reading.
 

Posts:155
Registered: 6/9/03
Re: 2-way file upload/download b/w client/server  
Jul 22, 2004 6:41 AM (reply 17 of 19)



 
One way you might be able to get real control is to put the file byte size into your file array.

could you please post the piece of code about how to acheive the above---showing the loop at the client side that keeps track of this filesize!!
 

Posts:1,111
Registered: 1/13/04
Re: 2-way file upload/download b/w client/server  
Jul 22, 2004 7:17 AM (reply 18 of 19)



 
The pseudo code algorithm is

for (each file) {
readFileData
}

readFileData() {
while (remainingDataBlocks) {
readDataBlock
}
}

remainingDataBlocks {
return round up (remainingDataBytes / data block size) > 0;
}

remainingDataBytes () {
return file bytes expected - file bytes received
}

I'll let you work out the method parameters/returns.

 

Posts:155
Registered: 6/9/03
Re: 2-way file upload/download b/w client/server  
Jul 22, 2004 7:40 AM (reply 19 of 19)



 
It works fine!! now
i am demarcating the eof of the files by sending a flag "EOF<filename>EOF" after sending the file. the code i posted earlier works fine!! i forgot to put a BufferedWriter.close() method. that's why it was behaving wierd.
Thanks for all the help :)
 
This topic has 19 replies on 2 pages.    « Previous | 1 | 2 |