<p>---------------------- <a href="http://edu.csdn.net" rel="noopener noreferrer" target="_blank"> ASP.Net+Android+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 = new Socket("127.0.0.1",10006);
FileInputStream fis = new FileInputStream("1.png");
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();
}
}</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 = new ServerSocket(10006);
Socket s = ss.accept();
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("server.png");
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();
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!=1)
{
System.out.println("请选择一个png格式的图片文件");
return;
}
File file = new File(args[0]);
if(!(file.exists() &&file.isFile()))
{
System.out.println("该文件有问题,要么不存在,要么不是文件");
return;
}
if(!file.getName().endsWith(".png"))
{
System.out.println("图片格式错误,请重新选择");
return;
}
if(file.length()>1024*1024*5)
{
System.out.println("文件过大,请重新选择");
return;
}
Socket s = new Socket("127.0.0.1",10007);
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(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 = s;
}
public void run()
{
String ip = s.getInetAddress().getHostAddress();
try
{
intcount =1;
System.out.println(ip+"....connected");
InputStream in = s.g |
|