Public class tcpserverfs implements runnable {private socket s; Public tcpserverfs (socket s) {super (); this. S = s ;}@ overridepublic void run () {// todo auto-generated method stub // read the file name try {inputstream in = S. getinputstream (); // read the uploaded file name byte [] Buf = new byte [1024]; int Len = in. read (BUF); string filename = new string (BUF, 0, Len); system. out. println ("the uploaded file is:" + filename); file UF = new file ("F:/0123/myserver", F Ilename); // fileoutputstream Fos = new fileoutputstream (UF); when the read stream is associated with a file object, int COUNT = 1 is automatically created when the file does not exist; // determine whether the file already exists while (UF. exists () {system. out. println (UF. exists (); int Index = filename. lastindexof (". "); string name = filename. substring (0, index); string type = filename. substring (index + 1); UF = new file ("f :\\ 0123 \ myserver", name + "(" + (count ++) + "). "+ type);} system. out. println ("file storage name on the server:" + UF. getname ()); Fileoutputstream Fos = new fileoutputstream (UF); // read the file data while (LEN = in. Read (BUF ))! =-1) {FOS. write (BUF, 0, Len);} printstream out = new printstream (S. getoutputstream (), true); out. println ("the Shang file is successfully uploaded"); system. out. println ("file from:" + S. getinetaddress (). gethostname ();} catch (exception e) {e. getmessage () ;}} public static void main (string [] ARGs) throws exception {// todo auto-generated method stubserversocket Ss = new serversocket (10713); While (true) {system. out. println ("server enabled, waiting for connection"); socket S = ss. accept (); New thread (New tcpserverfs (s )). start ();}}}
File. exists () returns false
// Fileoutputstream Fos = new fileoutputstream (UF); The read stream is associated with a file object. When the file does not exist, the file is automatically created. exists () returns true. The size of the first file on the server that cannot be completely uploaded is 0.
This article comes from "either desperate or rolling back !" Blog, please be sure to keep this source http://jiangzuun2014.blog.51cto.com/8732469/1530944