gpt4 book ai didi

Java - 通过套接字发送文件 - 文件未完全接收

转载 作者:太空宇宙 更新时间:2023-11-04 06:34:49 25 4
gpt4 key购买 nike

我真的被我正在做的一个小项目困住了。我正在尝试通过套接字从服务器向客户端发送音乐文件(仅 .wav)。一切都工作得很好(我认为......),除了客户端收到的文件不完整。我无法播放该文件,而且我可以看到它比服务器上的文件小一点。我哪里做错了?

这是服务器代码:

    private Socket client;
private String filename;
private TBMCAudioServer ac;

private FileInputStream fis;
private BufferedOutputStream out;

int bufferSize = 0;

FileSender(Socket client, String filename, TBMCAudioServer ac){

this.client = client;
this.filename = filename;
this.ac = ac;
}

@Override
public void run(){

ac.ex.sendMessage(client, "[#preload#]" + filename);

File dir = new File(ac.getDataFolder() + File.separator + "music");
if(!dir.exists()){
dir.mkdir();
}

File file = new File(dir, filename + ".wav");

long length = file.length();
if(length > Integer.MAX_VALUE){
logger.info("File is too large.");
}
byte[] bytes = new byte[(int) length];

try{
fis = new FileInputStream(file);
out = new BufferedOutputStream(client.getOutputStream());
} catch (IOException e){
logger.info(e.getMessage());
}

int count;

try {
while((count = fis.read(bytes,0,bytes.length)) != -1){
out.write(bytes, 0, count);
}

out.flush();
out.close();
fis.close();
} catch (IOException e) {
logger.info(e.getMessage());
}

}

在这里您可以看到我的客户端代码:

    private Socket server;
private String filename;
private AudioClient ac;

InputStream is = null;
FileOutputStream fos = null;
int bufferSize = 0;

FileReceiver(Socket server, String filename, AudioClient ac){
this.server = server;
this.filename = filename;
this.ac = ac;
}

@Override
public void run() {

try{
is = server.getInputStream();

bufferSize = server.getReceiveBufferSize();
ac.logConsole("Buffer size: " + bufferSize);
} catch (IOException ex){
ac.logConsole(ex.getMessage());
}

try{
fos = new FileOutputStream(AudioClient.util.getLineValue(3) + filename + ".wav");
} catch (FileNotFoundException e){
ac.logConsole(e.getMessage());
}

byte[] bytes = new byte[bufferSize];

int count;

try {
while((count = is.read(bytes, 0, bytes.length)) != -1){
fos.write(bytes, 0, count);
}

ac.logConsole("yay");
is.close();
fos.flush();
fos.close();
} catch (IOException e) {
ac.logConsole(e.getMessage());
}
}

最佳答案

好吧,我成功发送了完整的文件,这样我就可以看到它的大小与我的新代码的大小相同。唯一的问题是我正在发送音乐文件,但无法播放发送的文件。也许有人知道问题是什么?

服务器代码:

package me.Ciaran.simplefileserver;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleFileServer {

public final static int SOCKET_PORT = 13267; // you may change this
public final static String FILE_TO_SEND = "D:/server/plugins/TBMCAudioServer/music/DLTALL.wav"; // you may change this

public static void main (String [] args ) throws IOException {
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
ServerSocket servsock = null;
Socket sock = null;
try {
servsock = new ServerSocket(SOCKET_PORT);
while (true) {
System.out.println("Waiting...");
try {
sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
// send file

final File myFile= new File(FILE_TO_SEND); //sdcard/DCIM.JPG
byte[] mybytearray = new byte[8192];
fis = new FileInputStream(myFile);
bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
try {
os = sock.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(myFile.getName());
dos.writeLong(mybytearray.length);
int read;
while((read = dis.read(mybytearray)) != -1){
dos.write(mybytearray, 0, read);
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("Done.");
}
finally {
if (bis != null) bis.close();
if (os != null) os.close();
if (sock!=null) sock.close();
}
}
}
finally {
if (servsock != null) servsock.close();
}
}
}

这是客户端代码:

package me.Ciaran.simplefileclient;

import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class SimpleFileClient {

public final static int SOCKET_PORT = 13267; // you may change this
public final static String SERVER = "127.0.0.1"; // localhost
public final static String
FILE_TO_RECEIVED = "C:/Users/Ciaran/Documents/TESTEN/music/DLTALL.wav"; // you may change this, I give a
// different name because i don't want to
// overwrite the one used by server...

public final static int FILE_SIZE = 6022386; // file size temporary hard coded
// should bigger than the file to be downloaded

public static void main (String [] args ) throws IOException {
int bytesRead;
int current = 0;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
Socket sock = null;
try {
sock = new Socket(SERVER, SOCKET_PORT);
System.out.println("Connecting...");

// receive file

InputStream in;
int bufferSize=0;

try {
bufferSize=sock.getReceiveBufferSize();
in=sock.getInputStream();
DataInputStream clientData = new DataInputStream(in);
String fileName = clientData.readUTF();
System.out.println(fileName);
OutputStream output = new FileOutputStream(FILE_TO_RECEIVED);
byte[] buffer = new byte[bufferSize];
int read;
while((read = clientData.read(buffer)) != -1){
output.write(buffer, 0, read);
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("File " + FILE_TO_RECEIVED
+ " downloaded (" + current + " bytes read)");
}
finally {
if (fos != null) fos.close();
if (bos != null) bos.close();
if (sock != null) sock.close();
}
}

}

关于Java - 通过套接字发送文件 - 文件未完全接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25565095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com