問(wèn)題描述:這是一個(gè)socket傳輸文件的程序,在編寫(xiě)的時(shí)候出現(xiàn)了出現(xiàn)了個(gè)奇怪的問(wèn)題,就是當(dāng)(星號(hào))代碼存在時(shí),傳輸11M的txt文件(其他類(lèi)型文件未測(cè)試)時(shí),速度異常慢。但是先在測(cè)試類(lèi)把文件名getbytes以后,再在本類(lèi)用bufArray[0]=6這種方法一個(gè)一個(gè)把文件名的bytes存進(jìn)去以后,刪除本類(lèi)中g(shù)etbytes方法再運(yùn)行不會(huì)降低速度。而且測(cè)試類(lèi)的getbytes方法運(yùn)行時(shí)速度也很快。但是為何在本類(lèi)用getbytes方法就會(huì)大幅度降低速度?(測(cè)試時(shí)速度降低存在于客戶端文件input期間,無(wú)法理解為何會(huì)產(chǎn)生影響,雖然getbytes方法在本類(lèi)中是累贅,但是這是隨便寫(xiě)的時(shí)候出現(xiàn)的問(wèn)題,想請(qǐng)教一下)//代碼如下,星號(hào)為關(guān)鍵代碼? ?//客戶端代碼? ? ? ?package ioexampleclient;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataInputStream; ?import java.io.DataOutputStream; ?import java.io.File; ?import java.io.FileInputStream; ?import java.io.FileNotFoundException; ?import java.io.IOException; ?import java.net.ServerSocket; ?import java.net.Socket; ???public class Ioexampleclient { ?? ? private ServerSocket ss=null; ?? ? public Ioexampleclient(){ ?? ? ? ? ??? ? } ?? ? public void sendFile(String filePath,int port){ ?? ? ? ? DataOutputStream dos=null; ?? ? ? ? DataInputStream dis=null; ?? ? ? ? ??? ? ? ? Socket socket=null; ?? ? ? ? try { ?? ? ? ? ? ? File file=new File(filePath); ?? ? ? ? ? ? ss=new ServerSocket(port); ?? ? ? ? ? ? socket=ss.accept(); ?? ? ? ? ? ? dos=new DataOutputStream(socket.getOutputStream()); ?? ? ? ? ? ? dis=new DataInputStream(new BufferedInputStream(new FileInputStream(filePath))); ?? ? ? ? ? ??? ? ? ? ? ??? ? ? ? ? ? int buffferSize=1024; ?? ? ? ? ? ? byte[]bufArray=new byte[buffferSize]; ?**** ? ? ? ? ? bufArray=file.getName().getBytes();? ? ? ? ? ? dos.writeUTF(file.getName()); ??? ? ? ? ? ? dos.flush(); ??? ? ? ? ? ? dos.writeLong((long) file.length()); ??? ? ? ? ? ? dos.flush(); ??? ? ? ? ? ? while (true) { ??? ? ? ? ? ? ? ? int read = 0; ??? ? ? ? ? ? ? ? if (dis!= null) { ??? ? ? ? ? ? ? ? ? read = dis.read(bufArray); ??? ? ? ? ? ? ? ? } ????? ? ? ? ? ? ? ? if (read == -1) { ??? ? ? ? ? ? ? ? ? break; ??? ? ? ? ? ? ? ? } ??? ? ? ? ? ? ? ? dos.write(bufArray, 0, read); ??? ? ? ? ? ? ? } ??? ? ? ? ? ? ? dos.flush(); ?? ? ? ? } catch (FileNotFoundException e) { ?? ? ? ? ? ? // TODO Auto-generated catch block ?? ? ? ? ? ? e.printStackTrace(); ?? ? ? ? } catch (IOException e) { ?? ? ? ? ? ? // TODO Auto-generated catch block ?? ? ? ? ? ? e.printStackTrace(); ?? ? ? ? } finally { ??? ? ? ? ? ? ? // 關(guān)閉所有連接 ??? ? ? ? ? ? ? try { ??? ? ? ? ? ? ? ? if (dos != null) ??? ? ? ? ? ? ? ? ? dos.close(); ??? ? ? ? ? ? ? } catch (IOException e) { ??? ? ? ? ? ? ? } ??? ? ? ? ? ? ? try { ??? ? ? ? ? ? ? ? if (dis != null) ??? ? ? ? ? ? ? ? ? dis.close(); ??? ? ? ? ? ? ? } catch (IOException e) { ??? ? ? ? ? ? ? } ??? ? ? ? ? ? ? try { ??? ? ? ? ? ? ? ? if (socket != null) ??? ? ? ? ? ? ? ? ? socket.close(); ??? ? ? ? ? ? ? } catch (IOException e) { ??? ? ? ? ? ? ? } ??? ? ? ? ? ? ? try { ??? ? ? ? ? ? ? ? if (ss != null) ??? ? ? ? ? ? ? ? ? ss.close(); ??? ? ? ? ? ? ? } catch (IOException e) { ??? ? ? ? ? ? ? } ??? ? ? ? ? ? } ??????? ? } ?? ? public static void main(String []args){ ?? ? ? ? new Ioexampleclient().sendFile("d:\\TestSpace\\space01\\3.txt", 8821); ?? ? } ?} ?服務(wù)器端代碼package ioexampleserver;import java.io.BufferedInputStream; ?import java.io.BufferedOutputStream; ?import java.io.DataInputStream; ?import java.io.DataOutputStream; ?import java.io.FileOutputStream; ?import java.io.IOException; ?import java.net.Socket; ?import java.net.UnknownHostException; ???public class Ioexampleserver { ?? ? public Ioexampleserver(){ ?? ? ? ? ??? ? } ?? ? public void receiveFile(String savePath,String ip,int port) throws UnknownHostException, IOException{ ?? ? ? ? Socket socket=null; ?? ? ? ? ?? ? ? ? ? ? socket = new Socket(ip,port); ??? ? ? ? ?? ? ? ? DataInputStream dis=null; ?? ? ? ??? ? ? ? ? ? dis = new DataInputStream(new BufferedInputStream(socket ??? ? ? ? ? ? ? ? .getInputStream())); ??? ? ? ? ??? ? ? ? int bufferSize = 1024; ??? ? ? ? // 緩沖區(qū) ??? ? ? ? byte[] buf = new byte[bufferSize]; ??? ? ? ? int passedlen = 0; ??? ? ? ? long len = 0; ??? ? ? ? // 獲取文件名稱 ??? ? ? ?try{ ?? ? ? ? savePath += dis.readUTF(); ??? ? ? ? /*DataOutputStream fileOut = new DataOutputStream( ??? ? ? ? ? ? new BufferedOutputStream(new BufferedOutputStream( ??? ? ? ? ? ? ? ? new FileOutputStream(savePath)))); ? */? ? ? ? FileOutputStream fos=new FileOutputStream(savePath);? ? ? ? BufferedOutputStream bfo=new BufferedOutputStream(fos);? ? ? ? /*BufferedOutputStream bfo1=new BufferedOutputStream(bfo);*/? ? ? ? DataOutputStream fileOut=new DataOutputStream(bfo);? ? ? ? len = dis.readLong(); ??? ? ? ? System.out.println("文件的長(zhǎng)度為:" + len + " ? ?KB"); ??? ? ? ? System.out.println("開(kāi)始接收文件!"); ??? ? ? ? while (true) { ??? ? ? ? ? ? int read = 0; ??? ? ? ? ? ? if (dis!= null) { ??? ? ? ? ? ? ? read = dis.read(buf); ??? ? ? ? ? ? } ??? ? ? ? ? ? passedlen += read; ??? ? ? ? ? ? if (read == -1) { ??? ? ? ? ? ? ? break; ??? ? ? ? ? ? } ??? ? ? ? ? ? System.out.println("文件接收了" + (passedlen * 100 / len) + "%"); ??? ? ? ? ? ? fileOut.write(buf, 0, read); ??? ? ? ? ? } ??? ? ? ? ? System.out.println("接收完成,文件存為" + savePath); ??? ? ? ? ? fileOut.close(); ??? ? ? ? } catch (Exception e) { ??? ? ? ? ? e.printStackTrace(); ??? ? ? ? ? return; ??? ? ? ? } ??? ? } ?? ? public static void main(String[] args) throws UnknownHostException, IOException { ??? ? ? ? new Ioexampleserver().receiveFile("d:\\TestSpace\\space02\\", "localhost", 8821); ??? ? ? } ? ?
添加回答
舉報(bào)
0/150
提交
取消