A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 凝聚 中级黑马   /  2013-12-10 17:47  /  1268 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

客户端:
package twenty_four;
import java.io.*;
import java.net.*;
public class PicClient2 {
        public static void main(String[] args)throws Exception {
if(args.length!=1)
{
System.out.println("请选择一个JPG格式端图片");
return;
}
File file=new File(args[0]);
if(!(file.exists()&&file.isFile()))
{
        System.out.println("文件有问题");
}
if(!file.getName().endsWith(".jpg"))
{
        System.out.println("图片格式错误");
        return;
}
if(file.length()>1024*1024*5)
{
                System.out.println("文件过大");
                return;
}
Socket s=new Socket("192.168.106.88",40007);
FileInputStream fis=new FileInputStream(file);
OutputStream out =s.getOutputStream();
byte[]buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1)
{
out.write(buf,0,len);       
}
s.shutdownOutput();//告诉服务端数据已写完。
InputStream in=s.getInputStream();
byte[]bufin=new byte[1024];
int num=in.read(bufin);
System.out.println(new String(bufin,0,num));
fis.close();
s.close();
        }

}
服务端:
package twenty_four;
import java.io.*;
import java.net.*;
class PicThread implements Runnable
{
private Socket s;
PicThread( Socket s)
{
        this.s=s;
}
public void run()
{ int count=1;
        String ip=s.getInetAddress().getHostAddress();
        try{
               
                System.out.println(ip+"...........connection");
        InputStream in=s.getInputStream();
        File file=new File(ip+"("+(count)+")"+".jpg");
        while(file.exists())
                file=new File(ip+"("+(count)+")"+".jsp");
        FileOutputStream fos=new FileOutputStream(file);
        byte[]buf=new byte[1024];
        int len=0;
        while((len=in.read(buf))!=-1)
        {
        fos.write(buf,0,len);
        }
        OutputStream out =s.getOutputStream();
        out.write("上传成功".getBytes());
        fos.close();
        s.close();
        }
        catch(Exception e)
        {
                throw new RuntimeException(ip+"上传失败");
        }

}
}
public class PicServer2 {
        public static void main(String[] args) throws Exception{
ServerSocket ss=new ServerSocket(40007);
while(true)
{
Socket s=ss.accept();
new Thread(new PicThread(s)).start();
}
        }

}

这两段代码运行后,客户端无法输入路径,而且还没输入路径呢就出现   "请选择一个JPG格式端图片"这个警告,这是咋么回事?

评分

参与人数 1技术分 +1 收起 理由
贺奕凯 + 1

查看全部评分

4 个回复

倒序浏览
是你输入方式的问题。
因为你使用了args来判断文件路径的输入,所以应该在windows控制台下编译的时候输入如下代码才能正常运行客户端:
java PicServer2 路径名
而不是编译过后再输入路径!

用这种方式输入路径名并不合理,建议使用Scanner输入流输入路径。

评分

参与人数 1技术分 +1 收起 理由
贺奕凯 + 1

查看全部评分

回复 使用道具 举报
Kyle 发表于 2013-12-10 19:33
是你输入方式的问题。
因为你使用了args来判断文件路径的输入,所以应该在windows控制台下编译的时候输入如 ...

我用的是 myeclipse-10软件编写的代码
请问具体怎么改代码,
回复 使用道具 举报
凝聚 发表于 2013-12-10 20:15
我用的是 myeclipse-10软件编写的代码
请问具体怎么改代码,

选择Run->Run Configurations->选择Arguments选项卡,将需要上传的图片及路径放到Program aguments中,运行就OK了
回复 使用道具 举报
你的客户端代码和服务器端代码都存在一些小问题。
客户端上述我已经说了,服务端是class名字重名了,更改一下就好。
下面贴上代码。
客户端:
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.Scanner;

  4. public class PicClient2 {
  5.         public static void main(String[] args) throws Exception {
  6.                 Scanner sc = new Scanner(System.in);
  7.                 System.out.println("请选择一个JPG格式端图片:");
  8.                 String str = sc.nextLine();
  9.                 sc.close();
  10.                 File file = new File(str);
  11.                 if (!(file.exists() && file.isFile())) {
  12.                         System.out.println("文件有问题");
  13.                         return;
  14.                 }
  15.                 if (!file.getName().endsWith(".jpg")) {
  16.                         System.out.println("图片格式错误");
  17.                         return;
  18.                 }
  19.                 if (file.length() > 1024 * 1024 * 5) {
  20.                         System.out.println("文件过大");
  21.                         return;
  22.                 }
  23.                 Socket s = new Socket("192.168.1.103", 40007);
  24.                 FileInputStream fis = new FileInputStream(file);
  25.                 OutputStream out = s.getOutputStream();
  26.                 byte[] buf = new byte[1024];
  27.                 int len = 0;
  28.                 while ((len = fis.read(buf)) != -1) {
  29.                         out.write(buf, 0, len);
  30.                 }
  31.                 s.shutdownOutput();// 告诉服务端数据已写完。
  32.                 InputStream in = s.getInputStream();
  33.                 byte[] bufin = new byte[1024];
  34.                 int num = in.read(bufin);
  35.                 System.out.println(new String(bufin, 0, num));
  36.                 fis.close();
  37.                 s.close();
  38.         }

  39. }
复制代码


服务端:
  1. import java.io.*;
  2. import java.net.*;

  3. class PicServer1 implements Runnable {
  4.         private Socket s;

  5.         PicServer1(Socket s) {
  6.                 this.s = s;
  7.         }

  8.         public void run() {
  9.                 int count = 1;
  10.                 String ip = s.getInetAddress().getHostAddress();
  11.                 try {

  12.                         System.out.println(ip + "...........connection");
  13.                         InputStream in = s.getInputStream();
  14.                         File file = new File(ip + "(" + (count) + ")" + ".jpg");
  15.                         while (file.exists())
  16.                                 file = new File(ip + "(" + (count) + ")" + ".jsp");
  17.                         FileOutputStream fos = new FileOutputStream(file);
  18.                         byte[] buf = new byte[1024];
  19.                         int len = 0;
  20.                         while ((len = in.read(buf)) != -1) {
  21.                                 fos.write(buf, 0, len);
  22.                         }
  23.                         OutputStream out = s.getOutputStream();
  24.                         out.write("上传成功".getBytes());
  25.                         fos.close();
  26.                         s.close();
  27.                 } catch (Exception e) {
  28.                         throw new RuntimeException(ip + "上传失败");
  29.                 }

  30.         }
  31. }

  32. public class PicServer2 {
  33.         public static void main(String[] args) throws Exception {
  34.                 ServerSocket ss = new ServerSocket(40007);
  35.                 while (true) {
  36.                         Socket s = ss.accept();
  37.                         new Thread(new PicServer1(s)).start();
  38.                 }
  39.                
  40.         }

  41. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马