java根据url下载附件源码
程序员文章站
2022-07-16 14:12:32
...
public static void runDownLoad(String title,String fileurl,String filename){
// 构造URL
URL url;
try {
url = new URL(fileurl);
// 打开URL连接
URLConnection con = (URLConnection)url.openConnection();
// 得到URL的输入流
InputStream input = con.getInputStream();
// 设置数据缓冲
byte[] bs = new byte[1024 * 2];
// 读取到的数据长度
int len;
// 输出的文件流保存图片至本地
String path1 = "c:/aa/"+title;
File f = new File(path1);
f.mkdirs();
// String hzm=fileurl.split("\\.")[fileurl.split("\\.").length-1];
OutputStream os = new FileOutputStream(path1+"\\"+filename);
while ((len = input.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
input.close();
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
// 构造URL
URL url;
try {
url = new URL(fileurl);
// 打开URL连接
URLConnection con = (URLConnection)url.openConnection();
// 得到URL的输入流
InputStream input = con.getInputStream();
// 设置数据缓冲
byte[] bs = new byte[1024 * 2];
// 读取到的数据长度
int len;
// 输出的文件流保存图片至本地
String path1 = "c:/aa/"+title;
File f = new File(path1);
f.mkdirs();
// String hzm=fileurl.split("\\.")[fileurl.split("\\.").length-1];
OutputStream os = new FileOutputStream(path1+"\\"+filename);
while ((len = input.read(bs)) != -1) {
os.write(bs, 0, len);
}
os.close();
input.close();
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
下一篇: 像坏人一样勤奋
推荐阅读
-
AngularJS实现与Java Web服务器交互操作示例【附demo源码下载】
-
python根据url地址下载小文件的实例
-
Java开源生鲜电商平台-监控模块的设计与架构(源码可下载)
-
Java开源生鲜电商平台-一次代码重构的实战案例(源码可下载)
-
Java开源生鲜电商平台-定时器,定时任务quartz的设计与架构(源码可下载)
-
Java开源生鲜电商平台-商品价格的设计与架构(源码可下载)
-
Java开源生鲜电商平台-商品的spu和sku数据结构设计与架构(源码可下载)
-
Java开源生鲜电商平台-技术方案与文档下载(源码可下载)
-
Java开源生鲜电商平台-积分,优惠券,会员折扣,签到、预售、拼团、砍价、秒杀及抽奖等促销模块架构设计(源码可下载)
-
Java开源生鲜电商平台-订单抽成模块的设计与架构(源码可下载)