- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class UploadImage {
- public static void main(String[] args) {
- }
- }
- class ImageClient{
- public static void main(String[] args) throws Exception{
- Socket imageClientSocket = new Socket("192.168.0.107",10010);
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\1.jpg"));
- BufferedOutputStream bos = new BufferedOutputStream(imageClientSocket.getOutputStream());
- byte[] buf = new byte[1024];
- int len = 0 ;
- while((len=bis.read(buf))!=-1){
- bos.write(buf,0,len);
- }
- imageClientSocket.shutdownOutput();
- BufferedInputStream bis2 = new BufferedInputStream(imageClientSocket.getInputStream());
- byte[] buf2 = new byte[1024];
- int len2 = 0 ;
- while((len2=bis2.read(buf2))!=-1){
- System.out.println(new String(buf2,0,len2));
- }
- bis.close();
- imageClientSocket.close();
- }
- }
- class ImageServer{
- public static void main(String[] args) throws Exception{
- ServerSocket ss = new ServerSocket(10010);
- Socket s = ss.accept();
- BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e:\\01.jpg"));
- byte[] buf = new byte[1024];
- int len = 0 ;
- while((len=bis.read(buf))!=-1){
- bos.write(buf, 0, len);
- }
- BufferedOutputStream bos2 = new BufferedOutputStream(s.getOutputStream());
- bos2.write("upload successful".getBytes());
- bos2.close();
- s.close();
- ss.close();
- }
- }
复制代码 连接服务端时,文件没拷贝成功,只建立个空的图片 端口也没被占用
报出的异常是 Exception in thread "main" java.io.FileNotFoundException: E:\1.jpg (系统找不到指
定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at ImageClient.main(UploadImage.java:23)
,找不到文件,可是文件就存在E盘下,
我用的是深度xp纯净版的系统,是不是访问权限设置的问题 |
|