Java 教程

Java 控制语句

面向对象编程

Java 内置类

Java 文件处理

Java 错误和异常

Java 多线程

Java 同步

Java 网络

Java 集合

Java 接口

Java 数据结构

Java 集合算法

高级 Java

Java 杂项

Java API 和框架

Java 类参考

Java 有用资源

Java - HttpURLConnection setFixedLengthStreamingMode() 方法



Java HttpURLConnection setFixedLengthStreamingMode() 方法允许在预先知道内容长度的情况下,无内部缓冲地流式传输 HTTP 请求正文。

如果应用程序尝试写入的数据量超过指示的内容长度,或者在写入指示的数量之前关闭 OutputStream,则会抛出异常。

声明

以下是 java.net.HttpURLConnection.setFixedLengthStreamingMode() 方法的声明

public void setFixedLengthStreamingMode(long contentLength)

参数

contentLength - 将写入 OutputStream 的字节数。

返回值

异常

IllegalStateException - 如果 URLConnection 已经连接或已经启用了其他流式传输模式。

IllegalArgumentException - 如果指定的内容长度小于零。

示例 1

以下示例演示了如何对具有 https 协议的有效 url 使用 Java HttpURLConnection setFixedLengthStreamingMode() 方法。在此示例中,我们创建了一个 URL 类的实例。使用 url.openConnection() 方法,我们获得了 HttpURLConnection 实例。使用 getInputStream(),我们获取网站主页的内容并打印出来 -

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.com/index1.htm?language=en#j2se");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.setFixedLengthStreamingMode(1000L);  
		 urlConnection.setDoOutput(true);
         urlConnection.connect();  
         System.out.println("Connected.");  
         if(urlConnection.getInputStream() != null) {
            BufferedReader in = new BufferedReader(
               new InputStreamReader(urlConnection.getInputStream()));
            String content = "";
            String current;
            while((current = in.readLine()) != null) {
               content += current;
            }
            System.out.println(content);
         }else {
            System.out.println("Input Stream is null");
         }
         urlConnection.disconnect();  
         System.out.println("Disconnected.");  
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

让我们编译并运行以上程序,这将产生以下结果 -

输出

Connected.
java.io.IOException: insufficient data written
	at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
	at com.tutorialspoint.HttpUrlConnectionDemo.main(HttpUrlConnectionDemo.java:18)

示例 2

以下示例演示了如何对具有 http 协议的有效 url 使用 Java HttpURLConnection setFixedLengthStreamingMode() 方法。在此示例中,我们创建了一个 URL 类的实例。使用 url.openConnection() 方法,我们获得了 HttpURLConnection 实例。使用 getInputStream(),我们获取网站主页的内容并打印出来 -

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.com/index1.htm?language=en#j2se");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.setFixedLengthStreamingMode(1000L);  
		 urlConnection.setDoOutput(true);
         urlConnection.connect();  
         System.out.println("Connected.");  
         if(urlConnection.getInputStream() != null) {
            BufferedReader in = new BufferedReader(
               new InputStreamReader(urlConnection.getInputStream()));
            String content = "";
            String current;
            while((current = in.readLine()) != null) {
               content += current;
            }
            System.out.println(content);
         }else {
            System.out.println("Input Stream is null");
         }
         urlConnection.disconnect();  
         System.out.println("Disconnected.");  
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

让我们编译并运行以上程序,这将产生以下结果 -

输出

Connected.
java.io.IOException: insufficient data written
	at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at com.tutorialspoint.HttpUrlConnectionDemo.main(HttpUrlConnectionDemo.java:18)

示例 3

以下示例演示了如何对具有 http 协议的有效 url 使用 Java HttpURLConnection setFixedLengthStreamingMode() 方法。在此示例中,我们创建了一个 URL 类的实例。使用 url.openConnection() 方法,我们获得了 HttpURLConnection 实例。使用 getInputStream(),我们获取网站主页的内容并打印出来 -

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.setFixedLengthStreamingMode(1000L);  
		 urlConnection.setDoOutput(true);
         urlConnection.connect();  
         System.out.println("Connected.");  
         if(urlConnection.getInputStream() != null) {
            BufferedReader in = new BufferedReader(
               new InputStreamReader(urlConnection.getInputStream()));
            String content = "";
            String current;
            while((current = in.readLine()) != null) {
               content += current;
            }
            System.out.println(content);
         }else {
            System.out.println("Input Stream is null");
         }
         urlConnection.disconnect();  
         System.out.println("Disconnected.");  
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

让我们编译并运行以上程序,这将产生以下结果 -

输出

Connected.
java.io.IOException: insufficient data written
	at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at com.tutorialspoint.HttpUrlConnectionDemo.main(HttpUrlConnectionDemo.java:18)
java_httpurlconnection.htm
广告