teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

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

2019/05/11 16:01

投稿

y.endou
y.endou

スコア11

title CHANGED
File without changes
body CHANGED
@@ -93,4 +93,39 @@
93
93
  ### 補足情報(FW/ツールのバージョンなど)
94
94
 
95
95
  JDK 8u131
96
- Apache Tomcat Version 8.5.23
96
+ Apache Tomcat Version 8.5.23
97
+
98
+ ### 追記
99
+
100
+ OutputStreamを閉じてしまっているのが原因ではないかとご指摘いただいたので、送信部分下記のように変更して試してみました。
101
+ ``` Java
102
+ try (InputStream is = Files.newInputStream(uploadFile);
103
+ OutputStream os = connection.getOutputStream();) {
104
+ int len;
105
+ byte[] buf = new byte[8192];
106
+ try {
107
+ while ((len = is.read(buf, 0, buf.length)) > 0) {
108
+ os.write(buf, 0, len);
109
+ }
110
+ } catch (Exception e) {
111
+ e.printStackTrace();
112
+ }
113
+ System.out.println(connection.getResponseCode());
114
+ } catch (Exception e) {
115
+ e.printStackTrace();
116
+ }
117
+ ```
118
+ 結果
119
+ ```
120
+ java.io.IOException: Error writing request body to server
121
+ at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.checkError(HttpURLConnection.java:3536)
122
+ at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(HttpURLConnection.java:3519)
123
+ at UploadTest.main(UploadTest.java:27)
124
+ java.io.IOException: insufficient data written
125
+ at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(HttpURLConnection.java:3558)
126
+ at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1521)
127
+ at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
128
+ at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
129
+ at UploadTest.main(UploadTest.java:32)
130
+ ```
131
+ レスポンスを取得しようとした時に、内部でcloseが呼ばれているようでした。

1

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

2019/05/11 16:01

投稿

y.endou
y.endou

スコア11

title CHANGED
File without changes
body CHANGED
@@ -44,7 +44,8 @@
44
44
  connection.setRequestMethod("POST");
45
45
  connection.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString("admin:adimnnn".getBytes()));
46
46
  connection.setFixedLengthStreamingMode(Files.size(uploadFile));
47
- try (InputStream is = Files.newInputStream(uploadFile); OutputStream os = connection.getOutputStream();) {
47
+ try (InputStream is = Files.newInputStream(uploadFile);
48
+ OutputStream os = connection.getOutputStream();) {
48
49
  int len;
49
50
  byte[] buf = new byte[8192];
50
51
  while ((len = is.read(buf, 0, buf.length)) > 0) {