Java 发送大文件(~1GB)失败,SocketException
我正在尝试使用java中的socket编程为p2p文件共享应用程序发送大文件。此代码发送200-300 mb文件时没有任何问题,但对于1 gb左右的大型文件,它给出了错误:-Java 发送大文件(~1GB)失败,SocketException,java,sockets,networking,large-files,file-handling,Java,Sockets,Networking,Large Files,File Handling,我正在尝试使用java中的socket编程为p2p文件共享应用程序发送大文件。此代码发送200-300 mb文件时没有任何问题,但对于1 gb左右的大型文件,它给出了错误:- java.net.SocketException: Software caused connection abort: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105)
at FileSender.main(FileSender.java:28)
我已经按照我得到的许多答案中的建议,将文件分成小块发送。我应该如何发送1 gb文件。我在Windows上编程
这是我的密码:
服务器
public class FileSender {
public static void main(String...s)
{
BufferedOutputStream bos;
String file="D:\\filename.mp4";
try {
ServerSocket sock=new ServerSocket(12345);
while(true)
{
System.out.println("waiting");
Socket soc=sock.accept();
bos= new BufferedOutputStream(soc.getOutputStream());
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int n=-1;
byte[] buffer = new byte[8192];
while((n = bis.read(buffer))>-1)
{
bos.write(buffer,0,n);
System.out.println("bytes="+n);
bos.flush();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public class FileReciev {
public static void main(String...s)
{
try
{
Socket sock=new Socket("127.0.0.1",12345);
File file=new File("D:\\newfilename.mp4");
BufferedInputStream bis=new BufferedInputStream(sock.getInputStream());
FileOutputStream fos = new FileOutputStream(file);
int n;
byte[] buffer = new byte[8192];
System.out.println("Connected");
while ((n = bis.read(buffer)) > -1) {
System.out.println("bytes="+n);
fos.write(buffer, 0, n);
if(n<(8192)){
fos.close();
bis.close();
break;
}
fos.flush();
}
System.out.println("recieved");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
接收器
public class FileSender {
public static void main(String...s)
{
BufferedOutputStream bos;
String file="D:\\filename.mp4";
try {
ServerSocket sock=new ServerSocket(12345);
while(true)
{
System.out.println("waiting");
Socket soc=sock.accept();
bos= new BufferedOutputStream(soc.getOutputStream());
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int n=-1;
byte[] buffer = new byte[8192];
while((n = bis.read(buffer))>-1)
{
bos.write(buffer,0,n);
System.out.println("bytes="+n);
bos.flush();
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public class FileReciev {
public static void main(String...s)
{
try
{
Socket sock=new Socket("127.0.0.1",12345);
File file=new File("D:\\newfilename.mp4");
BufferedInputStream bis=new BufferedInputStream(sock.getInputStream());
FileOutputStream fos = new FileOutputStream(file);
int n;
byte[] buffer = new byte[8192];
System.out.println("Connected");
while ((n = bis.read(buffer)) > -1) {
System.out.println("bytes="+n);
fos.write(buffer, 0, n);
if(n<(8192)){
fos.close();
bis.close();
break;
}
fos.flush();
}
System.out.println("recieved");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
公共类FileReciev{
公共静态无效主(字符串…s)
{
尝试
{
插座插座=新插座(“127.0.0.1”,12345);
File File=新文件(“D:\\newfilename.mp4”);
BufferedInputStream bis=新的BufferedInputStream(sock.getInputStream());
FileOutputStream fos=新的FileOutputStream(文件);
int n;
字节[]缓冲区=新字节[8192];
System.out.println(“已连接”);
而((n=bis.read(buffer))>-1){
System.out.println(“bytes=“+n”);
fos.写入(缓冲区,0,n);
如果(nif(n很可能接收器与服务器不同步,即接收器无法在服务器发送另一个数据包的时间内读取所有数据。您可以实施阻止机制,使服务器在接收器读取所有以前发送的数据之前不会发送新数据。@ExtremeCoders TCP已经完全做到了这一点。不要在内部刷新循环。这样做会失去缓冲输出流的所有好处。那么如何在不知道文件大小的情况下停止连接…?循环已在流结束时终止。这就是您所需要的。只需完全删除此代码块。即使在删除该块文件后,文件也会完全传输,但循环不会结束,其卡住了。它只是在等待另一个数据块读取。接收方在最后打印“已接收”。但它没有打印“已接收”。我没有关闭发送方的流,这就是接收方仍在等待的原因。。。!!