用post或者get实现文件下载
来源:互联网 发布:mac os host 编辑:程序博客网 时间:2024/05/16 11:07
package test;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.Calendar;public class filedownload1 {/** * @功能 下载临时素材接口 * @param filePath 文件将要保存的目录 * @param method 请求方法,包括POST和GET * @param url 请求的路径 * @return */public static File saveUrlAs(String url,String filePath,String method){ FileOutputStream fileOut = null; HttpURLConnection conn = null; InputStream inputStream = null; File file=null; try{ // 建立链接 URL httpUrl=new URL(url); conn=(HttpURLConnection) httpUrl.openConnection(); //以Post方式提交表单,默认get方式 conn.setRequestMethod(method); conn.setDoInput(true); conn.setDoOutput(true); // post方式不能使用缓存 conn.setUseCaches(false); //连接指定的资源 conn.connect(); //获取网络输入流 inputStream=conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(inputStream); //判断文件的保存路径后面是否以/结尾 if (!filePath.endsWith("/")) { filePath += "/"; } String extName = url.substring(url.lastIndexOf(".")); java.util.GregorianCalendar gcalendar = new java.util.GregorianCalendar();String year = gcalendar.get(Calendar.YEAR)+"";String month = gcalendar.get(Calendar.MONTH)+1 + "";String day = gcalendar.get(Calendar.DAY_OF_MONTH)+"";String newFileName =new java.util.Date().getTime() + "_" + (int)(1000 *Math.random())+ extName; String dirPath = year + File.separator +month + File.separator + day + File.separator;//写入到文件(注意文件保存路径的后面一定要加上文件的名称) //创建不同的文件夹目录 file=new File(filePath +dirPath); //判断文件夹是否存在 if (!file.exists()){//如果文件夹不存在,则创建新的的文件夹 file.mkdirs();} fileOut = new FileOutputStream(filePath +dirPath +newFileName); BufferedOutputStream bos = new BufferedOutputStream(fileOut); byte[] buf = new byte[4096]; int length = bis.read(buf); //保存文件 while(length != -1) { bos.write(buf, 0, length); length = bis.read(buf); } bos.close(); bis.close(); conn.disconnect();} catch (Exception e){ e.printStackTrace(); System.out.println("抛出异常!!");} return file; }public static void main(String[] args) {String fileUrl = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"; //String fileUrl = "file:///D:/test/test1.txt";/* * 报错:java.lang.ClassCastException: sun.net.www.protocol.file.FileURLConnection cannot be cast to java.net.HttpURLConnection */ String filePath = "d:/test1"; File file = saveUrlAs(fileUrl, filePath ,"GET"); /* * 若为POST,则报错:java.io.IOException: Server returned HTTP response code: 405 for URL:https://ss0.bdstatic。。。 */ System.out.println("Run ok!/n<BR>Get URL file " + file); }}
注意本方法有较大约束性:
1.此方法只能用GET,不能用POST;
2.只能用http协议下载其他网站的文件,不能用file协议下载服务器本地文件。
阅读全文
0 0
- 用post或者get实现文件下载
- libcurl post/get上传下载文件 以及断点下载(操作libcurl 实现断点下载(续点续传))
- iOS开发-使用AFN实现GET/POST请求,以及文件下载/文件(图片)上传
- Linux下用c语言实现发送http请求 方式可以Get或者Post
- Linux下用c语言实现发送http请求 方式可以Get或者Post
- aiyao Linux下用c语言实现发送http请求 方式可以Get或者Post
- Android 实现HttpClient发起GET或者POST请求
- HttpURLConnection HttpClient AsyncHttpClient 实现Post或者Get方式登陆
- AFNetworking3.0 GET/POST 以及上传(多)文件,下载文件
- AFNetworking3.0 GET/POST 以及上传(多)文件,下载文件
- Python GET或者POST 请求
- 网络编程之get,post请求,文件上传及下载
- AFNetWorking之GET,POST,上传图片,下载文件进度监测
- 单例安全模式--下载上传文件--get,post请求
- html 用get 或者 post 的方式 调用方法
- CHttpFile实现Get/Post - 下载网页 提交表单
- RxJava+RxAndroid+OKHTTP实现get post 以及下载图片功能
- CHttpFile实现Get/Post
- 3.参数的传递问题
- Python3学习之使用Print输出带颜色字体方法步骤详解
- Java绝对布局的编写-不使用布局管理器
- Sagheer, the Hausmeister CodeForces
- 人工智能行业薪酬曝光,是时候转行了
- 用post或者get实现文件下载
- 三种测试用例方法规则
- 多因子模型之因子(信号)测试平台----alphalens(二)
- (配置Hadoop2.x 环境搭建)完全分布式集群
- 自动化运维管理工具ansible的配置与使用
- 编程题 回文串难题
- Vue.js的组件分发 之 作用域槽
- JSON解析、JAVA常用的工具类、JAVA集合框架、JAVA泛型、枚举
- C++常规指针类(浅复制),智能指针类(计数类),值行类(深复制) 区别