2014年5月17日星期六

After transferring files, file corruption problems

I am a java novice, CSDN website is the first time to ask questions, this question may be in the wrong place to ask questions , if that is the case , please a lot of understanding ~ ~
recently doing a small chat room application . TCP protocol , C / S structure, the basic chat function has been completed, but do send the file, there is a strange phenomenon , that is transmitted. , When text document , the document is no lack , but if you send a picture or . doc files, pictures open, suggesting damage ; while the doc file is opened is garbled, before sending the file size is small , the initial process of inference is less certain bytes sent or file corruption.
what I said after the transfer method , I was in the client's wording is:
FileInputStream fis = new FileInputStream (" file address " ) ;/ / first open the file stream
byte [] b = new byte [3];
String rus = "";
for (; ;) {
int i = fis.read (b);
if (i == -1) break;
rus = rus + new String (b, 0, i) ;/ / bytes way after reading out the cast , into a String rus years .
}
fis.close (); / / Off
I got over String contents of the file , but I have to use String to send to the server,
PrintWriter pw = new PrintWriter (new OutputStreamWriter (so.getOutputStream ()));
pw.println (rus);
pw.flush ();
transmission mode is the case. Then talk about the server receives aspects
simply, is this:
BufferedReader br = new BufferedReader (new InputStreamReader (so.getInputStream ()));
br.read () ;/ / to read in an endless loop until you read so far. In this way only read char type, so read things out , I cast a String
This server also received a String type of file data file. Then follow the service receives the same way as in the server sends the String type of file data to the target Chatter , Chatter press after receiving the target , receiving all of the String type of file data , and then cast the byte type , then the following methods production of documents
FileOutputStream fos = new FileOutputStream (" file address + filename" ) ;
fos.write (byte type of file data ) ;
In this way, the file is generated , only . txt file generation is successful, others are damaged.

summarize the deterioration of file data : Client Files - > type of file data into a byte - > forcibly converted to a String data type of file (for sending ) - > read br.read () method Char file generated data ( data going to the server ) after take - > cast to the String type of file data ( for sending ) - > sent to the client , the client generates Char to br.read () method to read file data ( data going to the client ) - > cast byte types of data files , and then generate a file


Well, finally you can ask questions :
1: Is not cast too many times , so that damage to the contents of the documents ?
2: This should not use PrintWriter pw = new PrintWriter (new OutputStreamWriter (so.getOutputStream ()));
and BufferedReader br = new BufferedReader (new InputStreamReader (so.getInputStream ()));
this way to send and receive files ? I used this way , in fact, is to facilitate the transmission and reception of data my chat room . I do not know whether the appropriate transfer files.
3: send the file , is there a better way to find an example ?

novice seeking God guide you big thank friends ~ ~ ~ ~ ~ ~ ~ ~

------ Solution ------------------------------------ --------
recommend the use of a byte stream
------ Solution ------------------------ --------------------
string is encoded format , given a set of byte array , it will at the specified encoding format ( no that is the default system encoding ) encoding ; such a Chinese operating system , may GBK, then reads the byte array , when , according to the encoding format escape into characters , if the byte will be turned into a question mark , or what 's hot garbled . The landlord can do such an experiment , new String (new String ("12 pictures ". GetBytes ("UTF-8"), "GBK"). GetBytes ("GBK"), " UTF-8 "); appear or garbled. This example is meant to be encoded byte array after , if not recognize changes to the original byte will then become a question mark or other sign of irreversible (ISO-8859-1 , except , so tomcat will first look at this code ) . Here the landlord should understand it, the character ( string ) is used to display and recognize that it will be chaos , because it must show! So turn a bit later, byte file and was playing bad .

In addition, the landlord again sincerely to mention a few suggestions
attention yet thread-safe
chats are stored in the database yet ? frequent reader can also optimize it? What is to be cached get hold of Redis
can send this chat face it, or shake the window ?


I 've written so students call " about gun website " thing ~ ~ ~ ~ wish you better sincerely

------ Solution ------------------------------------ --------


FileReader fr = new FileReader("C:\\\error.txt");
BufferedReader br = new BufferedReader(fr);
String str="":
while((readStr = br.readLine())!=null){
str +=readStr ;
}//换一种试试

------ Solution -------------------------- ------------------
not character-oriented programming, byte stream -oriented programming .
text data , there is a problem inside the encoding format .
Also, the picture data coding problems also exist , however, between the two encoding formats are completely different.
You can not deal with the way the text ( text decoding ) to process image information .
So , just do the decoding operation , the binary data sent directly to each other on it.
do not care which in the end is what the data transport layer code, just transfer it.
------ Solution ---------------------------------------- ----
transfer files directly with byte stream , ensure that no problem !

------ For reference only ---------------------------------- -----
I was a beginner , a simple reminder : It is not to set about bi n format, similar to the same transfer files via FTP .
------ For reference only -------------------------------------- -
reply to the second floor :
I'm sorry , I can not understand your words , I'm sorry . But also thank you for reading my words so a lot , thank you!
------ For reference only -------------------------------------- -
just wrong , is on a reply to a floor.
Okay, now to the second floor ,
reply to the second floor :
I now try this , thank you for your reply and concern.
------ For reference only -------------------------------------- -
Reply 3 Floor :
After you do this one , I immediately went to try,
code is as follows :
System.out.println (" Experimental transcoding :". + new String (new String ("12 Pictures " getBytes ("UTF-8"), "GBK" .) getBytes ("GBK"), "UTF-8"));
The results were as follows:
experiment transcoding : 12 Pictures
above is not Tucao ah,
Although there is no distortion, but I can understand that you want to express what you mean , so it does make sense why the . txt no problem , but . doc and . png problems. After
, on the back of your question , I really am a beginner, so now just doing the transmission of documents on the safety of these linear , really have not thought about ==
However, you gave me really great advice on the role , thank you very much !
------ For reference only -------------------------------------- -
actually a user can not restore more than three consecutive , so what can not reply to the third floor , and now make up :
reply 4th Floor :
Now I use this stream to read the file :
DataInputStream dis = new DataInputStream (new BufferedInputStream (new FileInputStream (file)));
feeling can be read successfully . Your Rights as a reference , I will , thank you for your help friends

------ For reference only ---------------------------------- -----
Will use udp or tcp
------ For reference only ------------------------ ---------------
reply to the fifth floor :
byte-oriented , which I now clear.
However send byte , you have to use to send a direct write () and receive byte read ()
But now there is a problem plaguing me , to have the above these methods are now only found object flow
such as : ObjectInputStream ois = new ObjectInputStream (so.getInputStream ());
This stream read () methods to read bytes ;
The other one BufferedReader br = new BufferedReader (new InputStreamReader (so.getInputStream ()));
this stream , the method can not read can not receive bytes , only received characters. On
summary, I send chat messages with BufferedReader with ObjectInputStream transfer files, but now the progress that has been successful , and sometimes fail completely do not understand why.
------ For reference only -------------------------------------- -
Reply 9 Floor :
ah , and now I'm thinking for aspects of bytes sent .
------ For reference only -------------------------------------- -
reply 11th Floor :
used is TCP, transfer files dropout prevention , safe and reliable friends
------ For reference only -------------------- -------------------
concern , and also does not use TCP file transfer .
------ For reference only -------------------------------------- -
Please pass generic type transmission and receiving , you look buffereader, printwrite the API will understand
------ For reference only --------------- ------------------------
reply 16th Floor :
er er, well , are watching .
By the way, everyone , if you do receive files , will open another socket port and thread ?
------ For reference only -------------------------------------- -
amount . Novices are so powerful
------ For reference only --------------------------------- ------
I think we can end this posted . I am here can be considered completed a basic chat program. I finally found , transfer files , or use two ports a little better, so with that port would not be chatting conflict .
In addition,
read the file using the stream is : DataInputStream disfile = new DataInputStream (new FileInputStream (file));
and transfer files using a byte stream of objects : DataOutputStream dos = new DataOutputStream (socket.getOutputStream ());
is also used to receive a stream of objects : DataInputStream dis = new DataInputStream (socket.getInputStream ());
flow is used to write the file : DataOutputStream out = new DataOutputStream (new FileOutputStream (file));
Also, seemingly because TCP is a single transmission, every true information to ensure that only one server , a message is returned. If more than one message with a client's port , be careful not recognize ah. Therefore, transfer files and chat do I separate the two ports .
Finally , thank you for all kinds of help and attention
Results posted slightly !

没有评论:

发表评论