• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

为何用getbytes获开名字后再用socket传输会大幅度降低速度?

问题描述:这是一个socket传输文件的程序,在编写的时候出现了出现了个奇怪的问题,就是当(星号)代码存在时,传输11M的txt文件(其他类型文件未测试)时,速度异常慢。但是先在测试类把文件名getbytes以后,再在本类用bufArray[0]=6这种方法一个一个把文件名的bytes存进去以后,删除本类中getbytes方法再运行不会降低速度。而且测试类的getbytes方法运行时速度也很快。但是为何在本类用getbytes方法就会大幅度降低速度?(测试时速度降低存在于客户端文件input期间,无法理解为何会产生影响,虽然getbytes方法在本类中是累赘,但是这是随便写的时候出现的问题,想请教一下)
//代码如下,星号为关键代码
   //客户端代码
      
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 {   
              // 关闭所有连接   
              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);  
    }  
}  
服务器端代码
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;   
        // 缓冲区   
        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("文件的长度为:" + len + "    KB");   
        System.out.println("开始接收文件!");   
        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);   
      }

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!