Commit e869f064 authored by gzj34291's avatar gzj34291

文件下载

parent a404cd17
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.hand.hl;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class DoloadFileByUrl {
public DoloadFileByUrl() {
}
public static void main(String[] args) {}
public static Boolean downloadFile(String urlPath, String downloadDir, String fileName) {
File file = null;
BufferedInputStream bin = null;
OutputStream out = null;
Boolean flag = true;
try {
String path = downloadDir + File.separatorChar + fileName;
int len = 0;
byte[] buf = new byte[1024];
int size;
URL url = new URL(urlPath);
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.connect();
int fileLength = httpURLConnection.getContentLength();
URLConnection con = url.openConnection();
bin = new BufferedInputStream(httpURLConnection.getInputStream());
file = new File(path);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
out = new FileOutputStream(file);
while ((size = bin.read(buf)) != -1) {
len += size;
out.write(buf, 0, size);
}
} catch (MalformedURLException var15) {
var15.printStackTrace();
flag = false;
} catch (IOException var16) {
var16.printStackTrace();
flag = false;
} finally {
try {
if (bin != null) {
bin.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
flag = false;
}
}
return flag;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment