Some problems encountered when uploading files over TCP

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.