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!