java下载网络文件, ppt,pdf,图片

该博客介绍了如何使用Java编程从网络上下载不同类型的文件,包括PPT、PDF文档以及图片。提供了详细的步骤和可能遇到的问题解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public void goDownLoad() throws IOException, URISyntaxException{ 
          
// String urlPath ="http://b.hiphotos.baidu.com/pic/w%3D230/sign=1bb6c5bd8ad4b31cf03c93b8b7d7276f/21a4462309f79052c97999c00df3d7ca7acbd5ee.jpg";
URL _URL=new URL(urlPath);  
   HttpURLConnection con=(HttpURLConnection) _URL.openConnection();
   InputStream fis= new BufferedInputStream(con.getInputStream());  
   
//获取文件名 
   String trueurl=con.getURL().toString(); 
   String filename=trueurl.substring(trueurl.lastIndexOf('/')+1); 
  
                this.getResponse().reset();   
  
                this.getResponse().addHeader("Content-Disposition",   
                    "attachment;filename="  
                            + new String(filename.getBytes(),   
                                    "UTF-8"));   
            this.getResponse().addHeader("Content-Length", "" + con.getContentLength());   
            OutputStream toClient = new BufferedOutputStream(this.getResponse()   
                    .getOutputStream());   
            this.getResponse().setContentType("application/octet-stream");   
            byte[] buffer = new byte[1024 * 1024 * 8];   
            int i = -1;   
            while ((i = fis.read(buffer)) != -1) {   
                toClient.write(buffer, 0, i);   
            }
fis.close(); 
toClient.flush(); 
toClient.close(); 

}


可下载网络图片,ppd,pdf等网络文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值