本帖最后由 薆情媬証書 于 2013-10-23 09:16 编辑
亲,帮忙看看,这段代码运行也没问题,可是执行后 ,却没有创建新文件,不知是哪里出了问题??- <p> import java.net.*;
- import java.io.*;</p><p>class PicClient
- {
- public static void main(String[] args)throws Exception
- {
- Socket s = new Socket(InetAddress.getByName("192.168.1.100"),20000);
-
- FileInputStream fis = new FileInputStream("d:\\1.jpg");</p><p> OutputStream os= s.getOutputStream();</p><p> byte[] buf = new byte[1024];
- int len =0;
- while ((len =fis.read(buf)) !=-1)
- {
- os.write(buf, 0 ,len);
- }
- s.shutdownOutput();</p><p> InputStream is = s.getInputStream();
- len = is.read(buf);
- System.out.println(new String(buf,0,len));
- }
- }</p><p>class PicServe
- {
- public static void main(String[] args) throws Exception
- {
- ServerSocket ss = new ServerSocket(20000);
- while (true)
- {
- Socket s = ss.accept();</p><p> new Thread(new PicServeThread(s)).start();
- }
-
- }
- }</p><p>
- class PicServeThread implements Runnable
- {
- private Socket s;
- PicServeThread(Socket s)
- {
- this.s=s;
- }</p><p> public void run()
- {
- File file = new File(s.getInetAddress().getHostAddress()+".jpg");
-
- try
- {
- if(!file.exists())
- file.createNewFile();
- InputStream is = s.getInputStream();</p><p> FileOutputStream fis = new FileOutputStream(file);</p><p> byte[] buf = new byte[1024];
- int len = 0;
- while ((len =is.read(buf))!=-1)
- {
- fis.write(buf,0,len);
- }
- System.out.println(s.getInetAddress().getHostAddress()+"上传了"+file.toString());
- OutputStream os = s.getOutputStream();
- os.write("上传成功!".getBytes());
- }
- catch (Exception e)
- {
- throw new RuntimeException("上传失败");
- }
- </p><p> }
- }</p>
复制代码 求指点!!!
|
|