我会通过套接字发送"文件名"和"文件",我能够轻松发送文件,但是当我尝试发送字符串例如PrintWriter pw.println()或DataOutputStream或" out.writeUTF ( )"文件发送损坏时,我在StackOverflow上阅读了很多问题但没有找到答案,我正在寻找一些发送字符串和文件的例子, 你能帮忙吗?
服务器
package serverprova;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class ServerProva {
final static int porta=8888;//porta server dove si collegano i client
public static void main(String[] args) throws IOException {
// TODO code application logic here
ServerSocket serverSocket=null;
boolean ascoltando=true;
serverSocket = new ServerSocket(porta);//avvia il server con il numero di porta
Socket s;
BufferedReader br1=null;
// ****** interfaccia f=new interfaccia);
boolean r=true;
BufferedInputStream bis=null;
Scanner sc;
FileOutputStream fout;
while(ascoltando)
{
s=serverSocket.accept();// this socket
String filename="";
String nome_cartella="";
InputStream in = null;
OutputStream out = null;
DataInputStream inString;
inString = new DataInputStream(new BufferedInputStream(s.getInputStream()));
// filename = inString.readUTF();
// nome_cartella = inString.readUTF();
in = s.getInputStream();
//out = new FileOutputStream(nome_cartella+"/"+filename);
out = new FileOutputStream("ciao"+"/"+"asd.jpg");
byte[] b = new byte[20*1024];
int i ;
while((i = in.read(b)) >0){
out.write(b, 0, i);
}
out.close();
in.close();
//inString.close();
s.close();
}
}
}
客户
package clientprova;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class ClientProva {
final static int porta=8888;
public static void main(String[] args) throws FileNotFoundException, IOException {
File file;
file = new File("kirlian12.jpg");
InetAddress host = InetAddress.getLocalHost();
Socket sock = new Socket(host.getHostName(), 8888);
DataOutputStream outString = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
// outString.writeUTF(file.getName());
// outString.writeUTF("rivelatore2");
// outString.flush();
byte[] b = new byte[20*1024];
InputStream in = new FileInputStream(file);
OutputStream out = sock.getOutputStream();
int i ;
while ((i = in.read(b)) >0 ) {
out.write( b , 0 , i);
}
out.close();
in.close();
sock.close();
}
}
当我尝试取消注释注释的行时,我的文件已损坏
当你用 BufferedInputStream 包装 InputStream 时,后者"拥有"InputStream。这意味着当您调用 readUtf()
时,BufferedInputStream 从 InputStream 读取的字节数可能会超过读取 UTF 字符串所需的字节数。因此,当您下次直接访问 InputStream 时,传输文件的某些部分丢失了(因为它以前被读入 BufferedInputStream 的缓冲区并且当前驻留在那里)。
inString = new DataInputStream(new BufferedInputStream(s.getInputStream()));
filename = inString.readUTF();
nome_cartella = inString.readUTF();
...
in = s.getInputStream();
while((i = in.read(b)) >0){
您必须从两个备选方案中进行选择:要么始终使用原始输入流,要么始终使用 BufferedInputStream。同样的推理也适用于输出流(但您设法通过调用 flush()
来避免问题)。