Package com.tz.util.screen;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import javax.management.RuntimeErrorException;
/**
* Image upload
* Customer Service side
* 1. Service Endpoint
* 2. Read the image data from the customer service side
* 3. Send data to server via socket output stream
* 4. Read the service side feedback information
* 5. Close
* @author Administrator
*
*/
public class Tcppic {
public static void Main (string[] args) throws Exception, Exception {
if (args.length!=1) {
SYSTEM.OUT.PRINTLN ("Please select a jpg format picture");
Return
}
File File=new file (Args[0]);
if (!file.exists () &&file.isfile ()) {
System.out.println ("Problem with this document");
}
if (!file.getname (). EndsWith (". jpg")) {
System.out.println ("Malformed");
}
if (File.length () >1024*1024*5) {
System.out.println ("File too large");
Return
}
Socket s=new socket ("192.168.1.254", 10007);
FileInputStream fis=new FileInputStream ("1.bmp");
OutputStream Out=s.getoutputstream ();
Byte[] Buf=new byte[1024];
int len=0;
while ((Len=fis.read (BUF))!=-1) {
Out.write (Buf,0,len);
}
Tell the server to finish writing the data
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 ();
}
}
/**
* server side
* This service side of the limitations, when a customer service side connection, by the server to obtain, the service side to carry out the specific process, then the B client connection, only wait, because the server has not finished processing a customer service side of the request, There is no loop back to execute the next accept method, so temporarily get not to the B object
* in order to allow multiple customer service side concurrently access the server side, then the service side is best to say each customer service side packaged into a separate thread, so that can process multiple customer service requests simultaneously
* Defining Threads:
* Clear the code that each customer service will execute on the server, save the code in the Run method
*/
class Picthread implements Runnable {
private Socket s;
int count=1;
picthread (Socket s) {
this.s=s;
}
@Override
public void Run () {
string ip=s.getinetaddress (). gethostaddress ();
try {
system.out.println (IP);
inputstream In=s.getinputstream ();
//is named with IP to avoid overwriting
file file=new File (ip+ "(" + (count) + ")" + ". jpg");
while (File.exists ()) {
file=new file (ip+ "(" + ( count++) + "+" + ". jpg");
}
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 ("Upload succeeded". GetBytes ());
Fos.close ();
S.close ();
} catch (Exception e) {
throw new RuntimeException (ip+ "upload failed");
}
}
}
Class picserver{
public static void Main (string[] args) throws Exception {
ServerSocket ss=new ServerSocket (10007);
while (true) {
Socket s=ss.accept ();
New Thread (new Picthread (s)). Start ();
}
Ss.close ();
}
}
Tcppic multiple uploads