问题描述:这是一个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);
}