質問編集履歴

2

closeしてはいけないとのご指摘に対して追記致しました。

2019/05/11 16:01

投稿

y.endou
y.endou

スコア11

test CHANGED
File without changes
test CHANGED
@@ -189,3 +189,73 @@
189
189
  JDK 8u131
190
190
 
191
191
  Apache Tomcat Version 8.5.23
192
+
193
+
194
+
195
+ ### 追記
196
+
197
+
198
+
199
+ OutputStreamを閉じてしまっているのが原因ではないかとご指摘いただいたので、送信部分下記のように変更して試してみました。
200
+
201
+ ``` Java
202
+
203
+ try (InputStream is = Files.newInputStream(uploadFile);
204
+
205
+ OutputStream os = connection.getOutputStream();) {
206
+
207
+ int len;
208
+
209
+ byte[] buf = new byte[8192];
210
+
211
+ try {
212
+
213
+ while ((len = is.read(buf, 0, buf.length)) > 0) {
214
+
215
+ os.write(buf, 0, len);
216
+
217
+ }
218
+
219
+ } catch (Exception e) {
220
+
221
+ e.printStackTrace();
222
+
223
+ }
224
+
225
+ System.out.println(connection.getResponseCode());
226
+
227
+ } catch (Exception e) {
228
+
229
+ e.printStackTrace();
230
+
231
+ }
232
+
233
+ ```
234
+
235
+ 結果
236
+
237
+ ```
238
+
239
+ java.io.IOException: Error writing request body to server
240
+
241
+ at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.checkError(HttpURLConnection.java:3536)
242
+
243
+ at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(HttpURLConnection.java:3519)
244
+
245
+ at UploadTest.main(UploadTest.java:27)
246
+
247
+ java.io.IOException: insufficient data written
248
+
249
+ at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(HttpURLConnection.java:3558)
250
+
251
+ at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1521)
252
+
253
+ at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
254
+
255
+ at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
256
+
257
+ at UploadTest.main(UploadTest.java:32)
258
+
259
+ ```
260
+
261
+ レスポンスを取得しようとした時に、内部でcloseが呼ばれているようでした。

1

ソースコードの改行が消えてしまって、エラー内容とズレてしまっていました。

2019/05/11 16:01

投稿

y.endou
y.endou

スコア11

test CHANGED
File without changes
test CHANGED
@@ -90,7 +90,9 @@
90
90
 
91
91
  connection.setFixedLengthStreamingMode(Files.size(uploadFile));
92
92
 
93
- try (InputStream is = Files.newInputStream(uploadFile); OutputStream os = connection.getOutputStream();) {
93
+ try (InputStream is = Files.newInputStream(uploadFile);
94
+
95
+ OutputStream os = connection.getOutputStream();) {
94
96
 
95
97
  int len;
96
98