This example for you to share the Java image upload code for your reference, the specific contents are as follows
Import java.io.*;
Import java.net.*;
* * Send End/class Picsend {public static void main (string[] args) throws Exception {if (args.length!=1)
{System.out.println ("Please select a. jpg picture");
Return
' File File = new file (Args[0]); if (!) (
File.exists () && file.isfile ()) {System.out.println ("The picture has a problem (not a file or does not exist)");
Return
} if (!file.getname (). EndsWith (". jpg")) {System.out.println ("picture is not in the wrong format, please select the picture again");
Return
} if (File.length () >1024*1024*10) {System.out.println ("picture too large to upload");
Return Socket s = new socket ("192.168.33.1", 10006);//Create service FileInputStream FIS = new FileInputStream ("d:\\ belle. jpg");
/Read the picture outputstream out = S.getoutputstream ()//Read Write byte [] b = new byte[1024];
int len = 0;
while (len = Fis.read (b))!=-1) {out.write (B,0,len); } s.shutdownoutput ()//mark over InputStream in = S.getinputstream ();//Read clothesThe service end returns the data byte [] bin = new byte[1024];
int num = In.read (BIN);
System.out.println (New String (Bin,0,num));
Fis.close ();
S.close ();
} class Picthread implements Runnable {private Socket s;
Picthread (Socket s) {this.s = s;
public void Run () {int count = 1; String IP = s.getinetaddress (). gethostaddress ();//Get IP try {System.out.println (ip+ ...). Connect "
);
InputStream in = S.getinputstream ()//read to the stream data File = new file (ip+ "(" + (count) +) "+". jpg ");
while (File.exists ())//To determine whether a file is present = new file (ip+ "(" + (count++) + ")" + ". jpg");
FileOutputStream fos = new FileOutputStream (file);//write Byte [] b = new byte[1024];
int len = 0;
while (len = In.read (b))!=-1) {fos.write (B,0,len); OutputStream out = S.getoutputstream ()//write service end of data out.write ("Upload success!")
". GetBytes ());
Fos.close ();
S.close ();
}catch (Exception e) {throw new RuntimeException ("Upload failed"); }}/* Service End/class Picrece {public static void main (string[] args) throws Exception {Se
Rversocket ss = new ServerSocket (10006);
while (true) {Socket s = ss.accept ();//Receive New Thread (new Picthread (s)). Start (); }
}
}
Effect:
The above is the entire content of this article, I hope to learn Java program to help you.