aspose file tools*
The moose likes I/O and Streams and the fly likes How to Change the File Name for Each Uploaded Files to the Socket Server? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to Change the File Name for Each Uploaded Files to the Socket Server?" Watch "How to Change the File Name for Each Uploaded Files to the Socket Server?" New topic
Author

How to Change the File Name for Each Uploaded Files to the Socket Server?

agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Hi, I found this code on the internet and I understand most of the code.
With this code every time I upload a file foe instance "daihatsu_ffmpeg_4.mp4" , the file output name is always name "receivedData".
Could any one help me please, how to create the output name on the server same as the upload file name. Or automatically save the output name same as for every uploaded file name.


Server Socket


James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Interesting. You say "I understand most of the code" yet you want somebody to modify in an almost trivial manner some poor quality and flawed code that you found on the Internet.

This sounds very very like a request to do your homework.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
James Sabre wrote:Interesting. You say "I understand most of the code" yet you want somebody to modify in an almost trivial manner some poor quality and flawed code that you found on the Internet.

This sounds very very like a request to do your homework.



Im sorry, I think you misunderstand.
This is not my homework. This is part of my Thesis.
My thesis is about Distributed Video encoding. I have done the RPC, file download, multi threading video encoding. The last part is uploading the video back to the server.
Im seeking for a help.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

agustinus lumbantoruan wrote:

Im sorry, I think you misunderstand.

I understand perfectly.

This is not my homework. This is part of my Thesis.

Aha; it is all clear now; it is not homework! Am I missing something? What is the essential difference between 'homework' and 'thesis' when it comes to who is supposed to do the work?

My thesis is about Distributed Video encoding. I have done the RPC, file download, multi threading video encoding. The last part is uploading the video back to the server.
Im seeking for a help.


So have you followed the same approach for the rest of your thesis? Did you take a copy of someone else's work and get a forum to massage that so that it met your brief? It would seem to me that anyone who can do significant work on "Distributed Video encoding" should find the modification required to the upload code to be trivial. You are not so much "seeking for a help" as requesting someone to do the work for you.

Am I being unjust?
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
James Sabre wrote:
agustinus lumbantoruan wrote:

Im sorry, I think you misunderstand.

I understand perfectly.

This is not my homework. This is part of my Thesis.

Aha; it is all clear now; it is not homework! Am I missing something? What is the essential difference between 'homework' and 'thesis' when it comes to who is supposed to do the work?

My thesis is about Distributed Video encoding. I have done the RPC, file download, multi threading video encoding. The last part is uploading the video back to the server.
Im seeking for a help.


So have you followed the same approach for the rest of your thesis? Did you take a copy of someone else's work and get a forum to massage that so that it met your brief? It would seem to me that anyone who can do significant work on "Distributed Video encoding" should find the modification required to the upload code to be trivial. You are not so much "seeking for a help" as requesting someone to do the work for you.

Am I being unjust?


OMG man. I am implementing that thesis title. I learned, took bit and pieces of other people's code, combine it, modified it, use it and apply it to make my thesis works. Oh James, you see, there is no original thesis. Everybody borrows on every body idea and works. It's sharing, being open for progress. FYI, I put reference on my thesis book, where i got the piece of code and modified it. If you are sincerely would like to give me help, please do. I do not have time to argue with this and i do not want to argue to this, copying stuff arguement. People invent things by learning/copying what others are doing. Take a look around.

Have a nice day man. I gotta go back to work solving on my program
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

If you're able to write a multithreaded video encoding then you should be able to read documentation and use google.
I would advise you to read this and then you'll be able to comprehend why the
code you found is bad.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Wouter Oet wrote:If you're able to write a multithreaded video encoding then you should be able to read documentation and use google.
I would advise you to read this and then you'll be able to comprehend why the
code you found is bad.


Thanks Wouter for the reference. I will read the tutorial and try to come up with a solution. I'll come back soon.
I appreciate it man.
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Ok so this is the latest update code. I decided to create a server that keeps on listening.
The server uses the socket input stream to get the uploaded data from the client.
At the end, there is a loop which reads buffereinputstream.

Unforunately, each file uploaded from the client is always missing about 8 bytes. Or in other word, the file becomes corrupted (missing bytes).
Interestingly the server side always gave me this message at the end. Correct me if im wrong that this because the loop hsa reached -1?
java.net.SocketException: Connection reset


Server


James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

I haven't gone through the code in detail but these is at least one problem that could cause you to loose bytes. You get the file name from the input by wrapping the input in a BufferedReader and reading a line. You then use this name to create the file and then read the file content from a a BufferedInputStream which wraps the the original InputStream. This means that anything buffered in the BufferedReader will be lost forever.

Consider the use of DataInputStream and DataOutputStream. You can then use the readUTF() and writeUTF() methods to pass the file name and use the data streams directly to write and read the file content.

Edit: This code
is very suspect. You seem to be trying to read a buffer full of data and then write "something" but you sure as heck are not writing out the data you have written. All you need is something along the lines of


Note : there are very few legitimate uses for available() and in conjunction with reading from a socket like you are doing is definitely not one of them.
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
James Sabre wrote:I haven't gone through the code in detail but these is at least one problem that could cause you to loose bytes. You get the file name from the input by wrapping the input in a BufferedReader and reading a line. You then use this name to create the file and then read the file content from a a BufferedInputStream which wraps the the original InputStream. This means that anything buffered in the BufferedReader will be lost forever.

Consider the use of DataInputStream and DataOutputStream. You can then use the readUTF() and writeUTF() methods to pass the file name and use the data streams directly to write and read the file content.

Edit: This code
is very suspect. You seem to be trying to read a buffer full of data and then write "something" but you sure as heck are not writing out the data you have written. All you need is something along the lines of


Note : there are very few legitimate uses for available() and in conjunction with reading from a socket like you are doing is definitely not one of them.


Thanks man for the help! I appreciate it. I decide it to re-program it from the beginning. I took your advice about the loop and modify it a little bit. The good news is, it's working perfectly as expected. The output file at the server side is saved same as the original file name and no more byte loss

So here is the code



James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Looks like I wasted my time; you are still using a buffered reader in the server!
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
James Sabre wrote:Looks like I wasted my time; you are still using a buffered reader in the server!


I use buffered reader to get the file name.
From the java docs
public String readLine() throws IOException
Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached


If i do not use "BufferedReader" how can i get the input file name? I want the transfered file to the server has the same name as the inputted name from the client.
The bufferedreader reads the client's PrintWriter. In the client side, I wrote this in line 25-27
If i do not use the BufferedReader, i have to write the file name manually and that is not what I want. Imagine if there are >100 videos uploaded from each encoder server to the streaming server
Do you know any other alternative?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

As I said earlier! Use a DataOutputStream and method writeUTF() to write the file name and then DataInputStream and method readUTF() to read it.
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
James Sabre wrote:As I said earlier! Use a DataOutputStream and method writeUTF() to write the file name and then DataInputStream and method readUTF() to read it.


Could you elabore DataOutputStream please? is it like this?
Ok, about writeUTF(String str). How can i get the filename?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

In your client


In your server


Note - once you have wrapped a stream use only the wrapper or you could end up loosing data buffered in the wrapper.
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
James Sabre wrote:In your client


In your server


Note - once you have wrapped a stream use only the wrapper or you could end up loosing data buffered in the wrapper.


I will give you an update change about the code tomorrow. It's late night here.
Thanks James for your help. I appreciate it man.
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Hi everyone, so this is somewhat the final code might look like. I hope it's useful for every one.
Thanks for the support guys!



Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Couple of remarks:

Method names should start with a lowercase letter.
Sometimes you're logging with java.util.Logger (which isn't really a good solution, check out SLF4J) and other times you're using System.out.
Your constructors invoke overridable methods.
Your constructors execute functionality. I think it's better to use constructors only for initialization.
You could close resources sooner.
In Client on line 37 you probably want t.start() instead of t.run().
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Wouter Oet wrote:Couple of remarks:

Method names should start with a lowercase letter.
Sometimes you're logging with java.util.Logger (which isn't really a good solution, check out SLF4J) and other times you're using System.out.
Your constructors invoke overridable methods.
Your constructors execute functionality. I think it's better to use constructors only for initialization.
You could close resources sooner.
In Client on line 37 you probably want t.start() instead of t.run().


Hi thanks for the remarks. I have do your remarks except for this part which
I do not understand "You could close resources sooner."

Could you elaborate that please.

Thanks Oet
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Well scrap that. I'm not sure any more. But you do try to close dis twice. On line 93 and 98.
agustinus lumbantoruan
Ranch Hand

Joined: Jul 17, 2010
Posts: 56
Wouter Oet wrote:Well scrap that. I'm not sure any more. But you do try to close dis twice. On line 93 and 98.


hahahaha i didn't notice that. Thanks man, i appreciate it

So here is the little update of the server and client code. The update was just to make it look cleaner

Server.java


Client.java
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The methods which close the resources are not correct. For instance if out.close(); (on line 84) throws an exception then dis and sock won't be closed.
You'll need a (couple) of try-finally blocks. Also check if the resource isn't null since that can be the case if the resources couldn't be opened.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Change the File Name for Each Uploaded Files to the Socket Server?