我正在测试代码示例,但它始终在connection.setDoInput(true);
HttpsURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String urlServer = "https://www.myurl.com/upload.php";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead = 0;
int bytesAvailable = 0;
int bufferSize = 0;
byte[] buffer = null;
int maxBufferSize = 1*1024*1024;
try {
FileInputStream fileInputStream = new FileInputStream(new File(params[0]));
URL url = new URL(urlServer);
connection = (HttpsURLConnection) url.openConnection();
connection.setConnectTimeout(1000);
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
connection.setConnectTimeout(1000);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + params[0] + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
错误日志为java.lang.IllegalStateException: Already connected
。
我试过这些,但没有一个工作:
connection.setRequestProperty("Connection", "close");
connection.disconnect();
connection.setConnectTimeout(1000);
编辑:即使我没有致电connection.connect()
,它仍然会给出同样的错误already connected
。
答案 0 :(得分:1)
您必须在将输入流读取到流末尾后关闭输入流。
您应该删除对connect()
的通话。你把它放在错误的地方,但它是自动的,根本不需要被调用。
您还可以删除设置POST的行。这在调用setDoOutput(true)
时是隐含的。
您还可以删除复制循环中的大部分内容。使用固定大小的缓冲区:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
每次读取不要使用新的缓冲区;不要打电话给available()
;不要通过GO;不要收200美元。
答案 1 :(得分:0)
移动
connection.connect();
后
connection.setRequestMethod("POST");
答案 2 :(得分:0)
关于http连接here
的帖子非常好最重要的是,对于POST,您只需要以下内容。
HttpURLConnection connection = (HttpURLConnection) new URL(urlToRead).openConnection();
// setDoOutput(true) implicitly set's the request type to POST
connection.setDoOutput(true);
我不确定您是否需要指定HttpsURLConnection。您可以使用HttpURLConnection连接到Https站点。让java在幕后为你工作。
这是我用于json帖子的POST代码
public static String doPostSync(final String urlToRead, final String content) throws IOException {
final String charset = "UTF-8";
// Create the connection
HttpURLConnection connection = (HttpURLConnection) new URL(urlToRead).openConnection();
// setDoOutput(true) implicitly set's the request type to POST
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-type", "application/json");
// Write to the connection
OutputStream output = connection.getOutputStream();
output.write(content.getBytes(charset));
output.close();
// Check the error stream first, if this is null then there have been no issues with the request
InputStream inputStream = connection.getErrorStream();
if (inputStream == null)
inputStream = connection.getInputStream();
// Read everything from our stream
BufferedReader responseReader = new BufferedReader(new InputStreamReader(inputStream, charset));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = responseReader.readLine()) != null) {
response.append(inputLine);
}
responseReader.close();
return response.toString();
}
答案 3 :(得分:-1)
添加
if (connection != null) connection.disconnect();
前
connection = (HttpsURLConnection) url.openConnection();
查看问题是否已解决。如果是,则表示原始代码中未调用connection.disconnect()
。也许你将connection.disconnect()
放在try
块的末尾,但是在它之前会发生异常,所以它跳转到catch块并且永远不会调用connection.disconnect()
。