Java Write HTTP server download tool _java

Source: Internet
Author: User

This tool is simpler to use with another tool for file transfer, less nonsense, on code

Import Java.net.URL;
Import java.net.URLConnection;
Import Java.io.File;
Import Java.io.InputStream;
Import Java.io.FileOutputStream;
Import java.io.FileNotFoundException;

Import java.io.IOException;

Import Org.apache.commons.io.FileUtils;

  public class httputil{Private String Httppath = "";
  public void Sethttppath (String httppath) {this.httppath = Httppath;
  Public String Gethttppath () {return this.httppath;
  Public Httputil (String httppath) {this.httppath = Httppath;
    Public inputstream getstream (String url) {InputStream instream = null;
      try{url httpurl = new URL (URL);
      URLConnection conn = Httpurl.openconnection ();
    Instream = Conn.getinputstream ();
      }catch (Exception e) {e.printstacktrace ();
    return null;
  return instream; public int downLoad (String url,string localname, int lines) throws FileNotFoundException, ioexception{fileoutput
    Stream fos = null;
 InputStream instream = null;   int ret = 0;
      try{url httpurl = new URL (URL);
      URLConnection conn = Httpurl.openconnection ();
      Instream = Conn.getinputstream ();
      FOS = new FileOutputStream (localname);
      Byte[] B = new byte[102400];
      int j = 0; while (Instream.read (b)!=-1 && lines > 0) {for (int i = j; i < B.length; i++) {if (b[i] = =
            ' \ n ') {Fos.write (b, J, i-j + 1);
            lines--;
            if (lines <= 0) {break;
            } j = i + 1;
          Continue
      }}}catch (Exception e) {e.printstacktrace ();
    ret =-1;
      }finally {fos.close ();
      Instream.close ();
    return ret;
    } public static void Main (string[] args) {String httppath = ' ";
    int lines = 0;
    String localname = "";
      try{Httppath = args[0];
      LocalName = args[1];
    Lines = Integer.parseint (args[2]); }catch (Exception e) {e.printstAcktrace ();
    Return
      try{httputil hu = new Httputil (Httppath);
    Hu.download (Hu.gethttppath (), localname, lines);
    }catch (Exception e) {e.printstacktrace ();

 }
  }
}

This tool implements a file that downloads a specified number of rows from the HTTP server and does not cause the downloaded file contents to be garbled because of the encoding problem
Three tools have been fixed, the next time is to combine the three tools to transfer HTTP, FTP files to HDFs

Hadoop Tools
FTP Tools

The above is the full content of this article, I hope you can enjoy.

Please take a moment to share the article with your friends or leave a comment. We will sincerely thank you for your support!

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.