黑马程序员-Java-网络编程-day24

论坛 期权论坛     
选择匿名的用户   2021-5-22 14:50   54   0
<p>---------------------- <a href="http://edu.csdn.net" rel="noopener noreferrer" target="_blank"> ASP.Net&#43;Android&#43;IOS开发</a>、<a href="http://edu.csdn.net" rel="noopener noreferrer" target="_blank">.Net培训</a>、期待与您交流! ----------------------</p>
<p></p>
<p><strong>1、网络编程(TCP-上传图片)</strong></p>
<p>客户端</p>
<p>1.服务端。</p>
<p>2.读取客户端已有的图片数据</p>
<p>3.通过socket输出流将数据发给服务端。</p>
<p>4.读取服务端反馈信息。</p>
<p>5.关闭资源</p>
<p></p>
<pre class="blockcode"><code class="language-java">import java.io.*;
import java.net.*;
class PicClient
{
       public static void main(String[] args) throws Exception
       {
              Socket s &#61; new Socket(&#34;127.0.0.1&#34;,10006);

              FileInputStream fis &#61; new FileInputStream(&#34;1.png&#34;);

              OutputStream out &#61; s.getOutputStream();

              byte[] buf &#61; new byte[1024];

              int len &#61;0;

              while((len&#61;fis.read(buf))!&#61;-1)
              {
                     out.write(buf,0,len);
              }
              //告诉服务端数据已写完
              s.shutdownOutput();

              InputStream in &#61; s.getInputStream();

              byte[] bufIn &#61; new byte[1024];

              int num &#61; in.read(bufIn);

              System.out.println(new String(bufIn,0,num));

              fis.close();
              s.close();


       }
}</code></pre>
<p></p>
<p></p>
<pre class="blockcode"><code class="language-java">//服务端
class PicServer
{
       public static void main(String[] args)throws Exception
       {
              ServerSocket ss &#61; new ServerSocket(10006);

              Socket s &#61; ss.accept();

              InputStream in &#61; s.getInputStream();

              FileOutputStream fos &#61; new FileOutputStream(&#34;server.png&#34;);

              byte[] buf &#61; new byte[1024];

              int len &#61;0;
              while((len&#61;in.read(buf))!&#61;-1)
              {
                     fos.write(buf,0,len);
              }

              OutputStream out &#61; s.getOutputStream();

              out.write(&#34;上传成功&#34;.getBytes());

              fos.close();

              s.close();

              ss.close();
       }
}</code></pre>
<p><br> </p>
<p><strong>2、网络编程(TCP-客户端并发上传图片)</strong></p>
<p></p>
<pre class="blockcode"><code class="language-java">import java.io.*;
import java.net.*;
class PicClient
{
       public static void main(String[] args) throws Exception
       {
              if(args.length!&#61;1)
              {
                     System.out.println(&#34;请选择一个png格式的图片文件&#34;);
                     return;
              }
              File file &#61; new File(args[0]);
              if(!(file.exists() &amp;&amp;file.isFile()))
              {
                     System.out.println(&#34;该文件有问题,要么不存在,要么不是文件&#34;);
                     return;
              }
              if(!file.getName().endsWith(&#34;.png&#34;))
              {
                     System.out.println(&#34;图片格式错误,请重新选择&#34;);
                     return;
              }

              if(file.length()&gt;1024*1024*5)
              {
                     System.out.println(&#34;文件过大,请重新选择&#34;);
                     return;
              }
              Socket s &#61; new Socket(&#34;127.0.0.1&#34;,10007);

              FileInputStream fis &#61; new FileInputStream(file);

              OutputStream out &#61; s.getOutputStream();

              byte[] buf &#61; new byte[1024];

              int len &#61;0;

              while((len&#61;fis.read(buf))!&#61;-1)
              {
                     out.write(buf,0,len);
              }
              //告诉服务端数据已写完
              s.shutdownOutput();

              InputStream in &#61; s.getInputStream();

              byte[] bufIn &#61; new byte[1024];

              int num &#61; in.read(bufIn);

              System.out.println(newString(bufIn,0,num));

              fis.close();
              s.close();


       }
}</code></pre>
<p> </p>
<p>/*</p>
<p>服务端</p>
<p>这个服务端有个局限性,当A客户端连接上以后,被服务端获取到,服务端执行具体流程。</p>
<p>这时B客户端连接,只有等待;因为服务端还没有处理完A客户端的请求,还要</p>
<p>循环回来执行下次accept方法,所以暂时获取不到B客户端对象。</p>
<p>那么为了可以让多个客户端同时冰法访问服务端,那么服务端最好就是讲每个客户端封装到一个单独的线程中,这样就可以同时处理多个客户端请求。</p>
<p>*/</p>
<p></p>
<pre class="blockcode"><code class="language-java">class PicThread implements Runnable
{
       private Socket s;
       PicThread(Socket s)
       {
              this.s &#61; s;
       }
       public void run()
       {
              String ip &#61; s.getInetAddress().getHostAddress();
              try
              {
                     intcount &#61;1;
                     System.out.println(ip&#43;&#34;....connected&#34;);

                     InputStream in &#61; s.g
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP