JAVA通过URL下载文件(POST,参数)

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;


/**
 * Java原生的API可用于发送HTTP请求,即java.net.URL、java.net.URLConnection,这些API很好用、很常用,
 * 但不够简便;
 * 
 * 1.通过统一资源定位器(java.net.URL)获取连接器(java.net.URLConnection) 
 * 2.设置请求的参数 
 * 3.发送请求
 * 4.以输入流的形式获取返回内容 5.关闭输入流
 * 
 * @author H__D
 *
 */
public class HttpConnectionUtil {




    /**
     * 
     * @param urlPath
     *            下载路径
     * @param downloadDir
     *            下载存放目录
     * @return 返回下载文件
     * @throws Exception 
     */
    public static void downloadFile(String urlPath, String saveDir) throws Exception {
        URL url = new URL(urlPath);
        // 连接类的父类,抽象类
        URLConnection urlConnection = url.openConnection();
        // http的连接类
        HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
        // 设定请求的方法,默认是GET(对于知识库的附件服务器必须是GET,如果是POST会返回405。流程附件迁移功能里面必须是POST,有所区分。)
        httpURLConnection.setRequestMethod("GET");
        // 设置字符编码
        httpURLConnection.setRequestProperty("Charset", "UTF-8");
        // 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
        int code = httpURLConnection.getResponseCode();


        InputStream inputStream = httpURLConnection.getInputStream();
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        byte[] buffer = new byte[1024];
//        int len = 0;
//        byte[] dataBytes = null;
//        while ((len = inputStream.read(buffer)) != -1 ) {  
//            baos.write(buffer, 0, len);  
//        }
//        baos.flush();
//        dataBytes = baos.toByteArray();
//        InputStream returnInputStream = new ByteArrayInputStream(dataBytes);
        
        File file = new File(saveDir);
        OutputStream out = new FileOutputStream(file);
        int size = 0;
        int lent = 0;
        byte[] buf = new byte[1024];
        while ((size = inputStream.read(buf)) != -1) {
            lent += size;
            out.write(buf, 0, size);
        }
        inputStream.close();
        out.close();
    }


    public static void main(String[] args) throws Exception {


        // 下载文件测试
        downloadFile("http://image5.suning.cn/uimg/cms/img/150338612019892140.png", "c:\\logo.jpg");
    }
}
被注释掉的代码,放开会更好。如果你获取的InputStream不是保存到本地,而是直接直接作为输入流上传到其他平台,那么必须把注释的代码放开,这个代码是用来保证一个完整的InputStream。
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以通过以下步骤通过POST请求下载文件: 1. 创建一个URL对象,用于连接下载文件的地址。 ``` URL url = new URL("http://example.com/download"); ``` 2. 创建一个HttpURLConnection对象,用于发送POST请求。 ``` HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); ``` 3. 设置POST请求参数,例如文件名和其他参数。 ``` String fileName = "example.txt"; String postData = "filename=" + fileName; OutputStream outputStream = connection.getOutputStream(); outputStream.write(postData.getBytes()); ``` 4. 发送请求并获取响应流。 ``` InputStream inputStream = connection.getInputStream(); ``` 5. 将响应流写入文件。 ``` FileOutputStream fileOutputStream = new FileOutputStream(fileName); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, len); } fileOutputStream.close(); ``` 完整代码示例: ```java import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class FileDownloader { public static void main(String[] args) throws Exception { URL url = new URL("http://example.com/download"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); String fileName = "example.txt"; String postData = "filename=" + fileName; OutputStream outputStream = connection.getOutputStream(); outputStream.write(postData.getBytes()); InputStream inputStream = connection.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream(fileName); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, len); } fileOutputStream.close(); } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值