package ok;import java.net.*;
import java.io.*;public class Application1 {
public static void main(String args[]) {
try {
ServerSocket server = new ServerSocket(1991);
Socket soc = server.accept();
BufferedOutputStream output = new BufferedOutputStream(soc.
getOutputStream());
BufferedInputStream input = new BufferedInputStream(new
FileInputStream(new File(
"C:\\Documents and Settings\\Administrator\\My Documents\\1.bmp")));
byte[] zijie=new byte[1024];
while(input.read(zijie)>0)
{
output.write(zijie);
output.flush();
}
input.close();
output.close(); //把这个去掉就没错了
soc.close();
server.close(); } catch (Exception ex) { System.out.println("有错误发生了!" + ex.getMessage());
} }
}
上面是我写的传图片的代码。 在运行的时候会报
Software caused connection abort: socket write error 这个错误?
可是如果把output.close();去掉后就不会有错了 事实上在我写的所有有关socket的代码中 只要把输出流一关 就会出错! 谁告诉我一下为什么? 谢谢回答!
import java.io.*;public class Application1 {
public static void main(String args[]) {
try {
ServerSocket server = new ServerSocket(1991);
Socket soc = server.accept();
BufferedOutputStream output = new BufferedOutputStream(soc.
getOutputStream());
BufferedInputStream input = new BufferedInputStream(new
FileInputStream(new File(
"C:\\Documents and Settings\\Administrator\\My Documents\\1.bmp")));
byte[] zijie=new byte[1024];
while(input.read(zijie)>0)
{
output.write(zijie);
output.flush();
}
input.close();
output.close(); //把这个去掉就没错了
soc.close();
server.close(); } catch (Exception ex) { System.out.println("有错误发生了!" + ex.getMessage());
} }
}
上面是我写的传图片的代码。 在运行的时候会报
Software caused connection abort: socket write error 这个错误?
可是如果把output.close();去掉后就不会有错了 事实上在我写的所有有关socket的代码中 只要把输出流一关 就会出错! 谁告诉我一下为什么? 谢谢回答!
解决方案 »
- 刚接触java,求iso日期转换问题?thanks
- 一个很基础的String数组初始化的问题
- 关于JAVA语音技术的一些问题。。。
- 日期比较问题(急)!
- private有什么奥秘吗?
- 请教动态代理类
- 如何在JPanel上加载图片!
- NetBeans IDE 4.0中文版中Swing组件的text中文在源码中显示为\u53d6\u6d88,如何解决?谢谢。
- 同志们:为什么我的菜单不显示,代码是。。。
- 小白求助:Certificate for <IP> doesn't match any of the subject alternative alternati
- 我想用JSP+ORACLE开发网站,不知用什么开发工具较好,请各位大侠指点,谢谢.
- JDK 6 javac编译的时候出现Note: Recompile with -Xlint:deprecation for details.
然后看一下有没有写的权限
byte[] zijie=new byte[1024];
while(input.read(zijie)>0)
{
output.write(zijie);
output.flush();
}
要注意最后一次读到的有可能是不满的,所以不能把整个zijie写出去,要用
output.write(zijie,0,len);
在循环外定义:
int len=0;
while((len=input.read(zijie))>0){
...
}
也就是读到多少写多少,否则最后一次是会把上一次的残留也写出去的。
另外就是虽然没问题能运行,但还是要把关流的代码写在finally里比较安全
/**
*
*/
package ok;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;/**
* @author arkwrightzhn
*
*/
public class Application2 { /**
* @param args
*/
public static void main(String[] args) {
Socket socket=null;
InputStream is=null;
FileOutputStream fos=null;
try {
socket=new Socket("localhost",1991);
is=socket.getInputStream();
fos=new FileOutputStream("C:\\Documents and Settings\\Administrator\\My Documents\\2.bmp");
byte[] bs=new byte[1024];
int len=0;
//int i=0;
while((len=is.read(bs))>0){
//i++;
//System.out.println(i);
fos.write(bs,0,len);
fos.flush();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(fos!=null) fos.close();
if(is!=null) is.close();
if(socket!=null) socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
import java.net.*;public class Frame1 {
public static void main(String[] args) {
try {
Socket soc = new Socket("127.0.0.1", 1991);
BufferedInputStream input = new BufferedInputStream(soc.
getInputStream());
BufferedOutputStream output = new BufferedOutputStream(new
FileOutputStream(new File("d:\\1.bmp"))); byte[] zijie=new byte[1024];
while(input.available()>0)
{
input.read(zijie);
output.write(zijie);
output.flush();
} System.out.println("传完了");
input.close();
output.close(); if (soc.isClosed()==false)
{
soc.close();
} } catch (Exception ex) {
System.out.println("有错误:" + ex.getMessage());
}
}}