java通过http方式下载文件

package com.qiyi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.File;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class HttpRequest {
    /**
     * 从网络Url中下载文件
     * @param urlStr
     * @param fileName
     * @param savePath
     * @throws IOException
     */
    public static void  downLoadFromUrl(String urlStr,String fileName,String savePath,String toekn) throws IOException{
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        //设置超时间为3秒
        conn.setConnectTimeout(3*1000);
        //防止屏蔽程序抓取而返回403错误
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        conn.setRequestProperty("lfwywxqyh_token",toekn);

        //得到输入流
        InputStream inputStream = conn.getInputStream();
        //获取自己数组
        byte[] getData = readInputStream(inputStream);

        //文件保存位置
        File saveDir = new File(savePath);
        if(!saveDir.exists()){
            saveDir.mkdir();
        }
        File file = new File(saveDir+File.separator+fileName);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(getData);
        if(fos!=null){
            fos.close();
        }
        if(inputStream!=null){
            inputStream.close();
        }

        System.out.println("info:"+url+" download success");

    }

    /**
     * 从输入流中获取字节数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }

}
package com.qiyi;

import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException {
    // write your code here

    ;
      //String url="http://127.0.0.1:9001/abc/notice/export?startDate=2017-1-27&endDate=2017-12-31";

        String token="v32Eo2Tw+qWI/eiKW3D8ye7l19mf1NngRLushO6CumLMHIO1aryun0/Y3N3YQCv/TqzaO/TFHw4=";
       // String token="SiGBCH6QblUHs7NiouV09rL6uAA3Sv0cGicaSxJiC/78DoWIMzVbW6VCwwkymYsZaxndDkYqkm4=";
        HttpRequest.downLoadFromUrl(url,"abc.xls","D:\\",token);
        System.out.println("下载完成");

    }
}
时间: 2024-08-02 10:33:05

java通过http方式下载文件的相关文章

C# Http方式下载文件到本地

下文代码是从网络(http://www.cnblogs.com/hayden/archive/2012/04/26/2472815.html)得来,亲测好用.我中修改了下格式和注释,版权属于原作者. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Net; namespace ConsoleTest { class

Java Sftp上传下载文件

需要使用jar包  jsch-0.1.50.jar sftp上传下载实现类 package com.bstek.transit.sftp; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import co

Java 上传和下载文件(附加密和解密)

本文首发于:https://antoniopeng.com 使用 Jersey 服务器实现上传,使用 HTTP 请求实现下载 引入依赖 在 pom.xml 中添加 Jersey 相关依赖 <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.18.1</version> </de

java中如何设置下载文件

如果想要设置某一url为下载文件的方法如下 需要设置文件响应类型,使用response.setContentType,比如jpeg格式的图片.如果想要访问该页面时出现下载保存的窗口,使用response.setHeader("Content-Disposition", "attachment;filename="+filename.getName())函数.也可以是pdf文件,或者其他格式的,如果想要查找浏览器可以识别什么格式的文件,需要到服务器的配置文件中找,比如

文件流的方式下载文件

//流方式下载 protected void Button4_Click(object sender, EventArgs e) { string fileName = "aaa.zip";//客户端保存的文件名 string filePath = Server.MapPath("DownLoad/aaa.zip");//路径 //以字符流的形式下载文件 FileStream fs = new FileStream(filePath, FileMode.Open);

asp.net已流的方式下载文件

string filePath = context.Server.MapPath("~/" + uploadFolder+"/"+file_name);//路径 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); if (fileInfo.Exists == true) { //以字符流的形式下载文件 FileStream fs = new FileStream(filePath, File

以流的方式下载文件

封装的axios下载 // 下载数据 export const downloadFile = (id) => { return axios.request({ url: '/file-manage/file/download/' + id, method: 'get', responseType: 'blob' }) } 传入下载的文件名,对应的文件id来下载流格式的文件 // 下载文件 downloadFile (filename, id) { downloadFile(id).then(re

(IO流)java中多种方式读文件,追加文件内容,对文件的各种操作

import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.io.R

重载Python FTP_TLS 实现Implicit FTP Over TLS方式下载文件

对于Python2.7来说,内置的FTP_TLS类并不支持Implicit FTP Over TLS加密方式的FTP Server操作,为支持Implicit FTP Over TLS加密方式,必须重载内置的FTP_TLS类,具体代码如下: import ftplib class FTP_TLS(ftplib.FTP_TLS): def __init__(self, host='', user='', passwd='', acct='', keyfile=None, certfile=None