Java讀取網(wǎng)上靜態(tài)文件資源
來源:易賢網(wǎng) 閱讀:1662 次 日期:2015-04-16 11:33:40
溫馨提示:易賢網(wǎng)小編為您整理了“Java讀取網(wǎng)上靜態(tài)文件資源”,方便廣大網(wǎng)友查閱!

Java讀取網(wǎng)上靜態(tài)文件資源,具體代碼如下:

public class ResourceLoader {

private static final int BUFFER_SIZE = 1024;

public void loadResourceIntoFile(String urlStr, String file)

throws IOException {

URL url = null;

URLConnection con = null;

InputStream in = null;

FileOutputStream out = null;

try {

url = new URL(urlStr);

con = url.openConnection();

File targetFile = new File(file);

long curLength = targetFile.length();

if (curLength > 0)

con.setRequestProperty("RANGE", "bytes=" + curLength);

con.connect();

long totalLength = con.getHeaderFieldLong("Content-Length", 0);

System.out.println("total length:" + totalLength);

in = con.getInputStream();

out = new FileOutputStream(targetFile, true);

if (totalLength > 0) {

int len;

byte[] buffer = new byte[BUFFER_SIZE];

while ((len = in.read(buffer)) != -1) {

out.write(buffer);

}

}

out.flush();

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

out.close();

in.close();

}

}

}

更多信息請查看IT技術(shù)專欄

更多信息請查看技術(shù)文章
易賢網(wǎng)手機網(wǎng)站地址:Java讀取網(wǎng)上靜態(tài)文件資源
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權(quán)所有:易賢網(wǎng)