java 请求超时处理_java请求超时处理

package com.it;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

public class HttpConnProcessThread implements Runnable {

public boolean isStop = false;

public boolean readOK = false;

private static HttpURLConnection reqConnection = null;

public Thread readingThread;

private int readLen;

private String msg = null;

private String reqMethod;

private byte[] data;

/**

* ReadThread constructor comment.

*/

public HttpConnProcessThread(HttpURLConnection reqConnection, String msg, String reqMethod ) {

super();

this.reqConnection = reqConnection;

this.msg = msg;

this.reqMethod = reqMethod;

}

public void run() {

InputStream input = null;

OutputStream output = null;

try{

//reqConnection.connect();

output = reqConnection.getOutputStream();

if ("post".equalsIgnoreCase(reqMethod) && msg != null && msg.length() >0)

{

output.write(msg.getBytes());

output.close();

output = null;

}

// 处理HTTP响应的返回状态信息

int responseCode = reqConnection.getResponseCode();// 响应的代码if( responseCode != 200 )

System.out.println("connect failed! responseCode = " + responseCode + " msg=" + reqConnection.getResponseMessage());

input = reqConnection.getInputStream();

int len;

byte[] buf = new byte[2048];

readLen = 0;

// 读取inputStream

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

while (!isStop)

{

len = input.read(buf);

if (len <= 0)

{

this.readOK = true;

input.close();

data=outStream.toByteArray();

break;

}

outStream.write(buf, 0, len);

readLen += len;

}

}

catch( IOException ie)

{}

catch(Exception e)

{}

finally

{

try{

reqConnection.disconnect();

if( input != null )

input.close();

if( output != null )

output.close();

//唤醒线程,跳出等待

wakeUp();

}catch(Exception e)

{

}

}

}

public String getMessage(){

if (!readOK) //超时

{

return "";

}

if (readLen <= 0) {

return "";

}

return new String(data, 0, readLen);

}

public void startUp() {

this.readingThread = new Thread(this);

readingThread.start();

}

//唤醒线程,不再等待

private synchronized void wakeUp() {

notifyAll();

}

public synchronized void waitForData(int timeout)

{

try {

//指定超时时间,等待connection响应

wait(timeout);

}

catch (Exception e)

{

}

if (!readOK)

{

isStop = true;

try{

//中断线程

if( readingThread.isAlive() )

readingThread.interrupt();

}catch(Exception e)

{

}

}

}

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

String msg="";

URL reqUrl = new URL("http://127.0.0.1:8080/");

// 建立URLConnection连接

reqConnection = (HttpURLConnection) reqUrl.openConnection();

HttpConnProcessThread rec = new HttpConnProcessThread(reqConnection, msg, "post" );

// 如果顺利连接到并读完数据,则跳出等待,否则等待超时

rec.startUp();

rec.waitForData(2000);

String retMessage = rec.getMessage();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于上传文件时出现网关超时的问题,有几种可能的解决方法: 1. 增加上传文件的超时时间:在您的Java代码中,可以尝试增加上传文件的超时时间,以便给上传操作更多的时间来完成。您可以使用`setConnectTimeout`和`setReadTimeout`方法来设置超时时间。例如: ```java URLConnection connection = url.openConnection(); connection.setConnectTimeout(5000); // 设置连接超时时间为5秒 connection.setReadTimeout(5000); // 设置读取超时时间为5秒 ``` 2. 增加网关的超时时间:如果您是通过网关进行文件上传,可以检查并增加网关的超时时间。具体的方法会根据您使用的网关软件或服务提供商而有所不同。 3. 检查网络连接:确保您的网络连接稳定,没有丢包或延迟过高的情况。您可以尝试使用其他网络环境或设备进行上传,以确定是否是网络问题导致的超时。 4. 检查文件大小和上传速度:如果您尝试上传的文件过大,可能会导致上传时间过长,进而触发超时。在这种情况下,您可以考虑压缩文件或分块上传来提高上传速度。 5. 使用断点续传机制:如果您的上传操作需要较长时间来完成,可以考虑使用断点续传机制。即将大文件分成多个小块进行上传,每次上传一个小块,上传失败后可以从上次中断的位置继续上传。 这些是常见的解决方法,希望能对您有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值