HttpURLConnection下载文件

public class HttpDownloadUtility
{

private static final Logger logger = LoggerFactory.getLogger(HttpDownloadUtility.class);

private static final int BUFFER_SIZE = 4096;
private static final int END_OF_FILE = -1;

public static boolean downloadFile(String fileURL, File saveDir)
{
return downloadFile(fileURL, saveDir, null);
}

public static boolean downloadFile(String fileURL, File saveDir, String fileName)
{
HttpURLConnection httpConn = null;
try
{
URL url = new URL(fileURL);
httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();

// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK)
{

String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();

if (StringUtils.isEmpty(fileName))
fileName = parseFileName(fileURL, disposition);

logger.info("Content-Type = " + contentType);
logger.info("Content-Disposition = " + disposition);
logger.info("Content-Length = " + contentLength);
logger.info("fileName = " + fileName);

if (!saveDir.exists())
{
logger.info("Creating File Destination Folder " + saveDir.getAbsolutePath());
saveDir.mkdirs();
}

File savedFile = saveFile(saveDir, httpConn, fileName);
logger.info("File downloaded to " + savedFile.getAbsolutePath());
return true;
}
else
{
logger.warn("No file to download. Server replied HTTP code: " + responseCode);
return false;
}
}
catch (Exception e)
{
logger.error("downloading failed from URL " + fileURL, e);
return false;
}
finally
{
if (null != httpConn)
httpConn.disconnect();
}
}

private static String parseFileName(String fileURL, String disposition)
{
String fileName = "";
if (disposition != null)
{
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0)
{
fileName = disposition.substring(index + 10, disposition.length() - 1);
}
}
else
{
// extracts file name from URL
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length());
}
return fileName;
}

private static File saveFile(File saveDir, HttpURLConnection httpConn, String fileName)
throws IOException, FileNotFoundException
{
// opens input stream from the HTTP connection
InputStream inputStream = httpConn.getInputStream();

File saveFile = new File(saveDir, fileName);

// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFile);

int bytesRead = END_OF_FILE;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != END_OF_FILE)
{
outputStream.write(buffer, 0, bytesRead);
}

outputStream.close();
inputStream.close();
return saveFile;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值