Java image upload Implementation code _java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.