Server side of Unity3d and Java server delivery files

Source: Internet
Author: User
Tags readfile

  • Just to be useful in the work, to record that the Java server is using the jfinal framework.
    • The Unity upload file only transmits the byte stream, and the server then writes the byte stream to the file
    •  Public voidUploadFile ()throwsIOException {rendernull (); //================== start processing files ===================//the file name of the temporary file in the content of the upload fileSystem.out.println (Getrequest (). Getcontentlength ()); if(Getrequest (). Getcontentlength () >0) {String tempfilename=NewString ("TempFileName.txt"); //Tempfile object pointing to a temporary fileFile tempfile =NewFile (Pathkit.getwebrootpath () + file.separator+tempfilename); System.out.println ("FilePath:" +pathkit.getwebrootpath () + file.separator+tempfilename); //outputfile file output stream points to this temporary fileFileOutputStream OutputStream =NewFileOutputStream (tempfile); //get all the data submitted by the clientInputStream Filesourcel =getrequest (). getInputStream (); //writes the resulting client data to a temporary file        byteB[] =New byte[1000]; intN;  while((N=filesourcel.read (b))!=-1) {System.out.println ("B:" +b); System.out.println ("N:" +N); Outputstream.write (b,0, N); }        //turn off the output stream and input streamOutputstream.close ();        Filesourcel.close (); //Randomfile object pointing to a temporary fileRandomaccessfile Randomfile =NewRandomaccessfile (Tempfile, "R"); //reads the first three rows of data from a temporary fileRandomfile.readline ();        Randomfile.readline ();        Randomfile.readline (); //reads the fourth row of data from the temporary file, which contains the path and file name of the filesString FilePath =Randomfile.readline (); //Get file nameSystem.out.println (FilePath); intPosition = Filepath.lastindexof ("filename"); String filename=tool.codestring (Filepath.substring (Position+10,filepath.length ()-1)); System.out.println ("FileName" +filename); //Reposition Read file pointer to file headerRandomfile.seek (0); //get the position of the fifth line carriage return, which is where the data of the uploaded file begins        Longfifthenterposition = 0; intFifth = 1;  while((N=randomfile.readbyte ())!=-1&& fifth<=5) {System.out.println ("N:" +N); System.out.println (Fifth:"fifth);if(n== ' \ n ') {fifthenterposition=Randomfile.getfilepointer (); Fifth++; } System.out.println (fifthenterposition:"Fifthenterposition);        }        //generate directory for uploaded filesFile fileupLoad =NewFile (Pathkit.getwebrootpath () + File.separator, "UpLoad");        Fileupload.mkdir (); //the SaveFile object points to the file to be savedFile SaveFile =NewFile (Pathkit.getwebrootpath () + "\\upLoad", filename); Randomaccessfile Randomaccessfile=NewRandomaccessfile (SaveFile, "RW"); //Find the end location of the uploaded file data, that is, the third line to the bottomRandomfile.seek (Randomfile.length ()); LongEndposition =Randomfile.getfilepointer (); System.out.println ("Endposition:" +endposition); intj = 1;  while((endposition>=0) && (j<=3) {System.out.println ("EndPosition1:" +endposition); System.out.println ("J:" +j); Endposition--;            Randomfile.seek (endposition); if(Randomfile.readbyte () = = ' \ n ') {J++; }        }        //writes data to the file to be saved from the beginning of the uploaded file data to the end locationSystem.out.println (fifthenterposition: "Fifthenterposition");Randomfile.seek fifthenterposition); LongStartPoint =Randomfile.getfilepointer (); System.out.println ("StartPoint:" +startPoint); //Endposition=randomfile.getfilepointer ();System.out.println ("Endposition:" +endposition);  while(startpoint<endposition) {System.out.println ("StartPoint1:" +startPoint); //System.out.println ("Randomfile.readbyte ():" +randomfile.readbyte ());Randomaccessfile.write (Randomfile.readbyte ()); StartPoint=Randomfile.getfilepointer (); }        //close file input, outputRandomaccessfile.close ();        Randomfile.close ();        Tempfile.delete (); //================== Processing file End ===================//upload success to console output fileSystem.out.println ("File upload success!");        Checkcsv (); }Else{RenderText ("False"); }            } 
    •  Public classTool {/**file byte stream*/     Public Static byte[] getBytes (String filePath) {byte[] buffer =NULL; Try{File File=NewFile (FilePath); FileInputStream FIS=Newfileinputstream (file); Bytearrayoutputstream Bos=NewBytearrayoutputstream (1000); byte[] B =New byte[1000]; intN;  while((n = fis.read (b))! =-1) {Bos.write (b,0, N);            } fis.close ();            Bos.close (); Buffer=Bos.tobytearray (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }        returnbuffer; }    /**functions for handling Chinese strings*/     Public Staticstring codestring (String str) {string S=str; Try {            byte[] temp = s.getbytes ("UTF-8"); S=NewString (temp); returns; } Catch(unsupportedencodingexception e) {e.printstacktrace (); returns; }    }
      }

    • Using Web uploads is completely different from byte stream transmission.
    •  Public voidUploadfileweb ()throwsIOException {uploadfile uploadfile= This. GetFile (); String FileName=Uploadfile.getoriginalfilename (); File File=Uploadfile.getfile (); Fileservice FS=NewFileservice (); File T=NewFile ("d:\\file\\" +fileName); Try{t.createnewfile (); } Catch(IOException e) {e.printstacktrace ();        } fs.filechannelcopy (file, T);    File.delete (); }
    • Download the file with Jfinal's RenderFile
    • //Download File     Public voidDownfile ()throwsFileNotFoundException, IOException {readfiles ("D:\\file\\"); String name= Getpara ("filename");        SYSTEM.OUT.PRINTLN (name); intindex =-1;  for(inti = 0; I < filearraylist.size (); i++) {            if(Filearraylist.get (i). INDEXOF (name)! =-1) {Index=i;        }} System.out.println (index); if(Index >-1) {RenderFile (NewFile (Filearraylist.get (index)));        SYSTEM.OUT.PRINTLN (Filearraylist.get (index)); }    }    //get all the files in the directory recursively     Public Static BooleanReadfiles (String filepath)throwsFileNotFoundException, IOException {Try{File File=NewFile (filepath); if(!file.isdirectory ()) {System.out.println (File); System.out.println ("Path=" +File.getpath ()); System.out.println ("Absolutepath=" +File.getabsolutepath ()); System.out.println ("Name=" +file.getname ());                Filearraylist.add (File.getpath ()); //filearraylist.add ("<a href= '/file/downfile ' > Download </a><br>");}Else if(File.isdirectory ()) {System.out.println (Folder); String[] FileList=file.list ();  for(inti = 0; i < filelist.length; i++) {File ReadFile=NewFile (filepath + "\ \" +Filelist[i]); if(!readfile.isdirectory ()) {System.out.println ("Path=" +Readfile.getpath ()); System.out.println ("Absolutepath=" +Readfile.getabsolutepath ()); System.out.println ("Name=" +readfile.getname ());                    Filearraylist.add (Readfile.getpath ()); } Else if(Readfile.isdirectory ()) {Readfiles (filepath+ "\\" +Filelist[i]); }                }            }        } Catch(FileNotFoundException e) {System.out.println ("ReadFile () Exception:" +e.getmessage ()); }        return true; }

Server side of Unity3d and Java server delivery files

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.