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

質問編集履歴

2

編集

2018/03/03 09:32

投稿

eisaku123
eisaku123

スコア77

title CHANGED
File without changes
body CHANGED
@@ -72,7 +72,7 @@
72
72
  response.setContentType("application/octet-stream ;charset=UTF-8");
73
73
  response.setHeader("Content-Disposition" ,"attachment");
74
74
 
75
- OutputStream out = response.getOutputStream();//画像側のストリーム<===getOutputStream()はこのレスポンスに対して既に呼び出されています
75
+ OutputStream out = response.getOutputStream();//画像側のストリーム<===エラー:getOutputStream()はこのレスポンスに対して既に呼び出されています
76
76
 
77
77
         //////テキスト////////start/////////////////////////////////
78
78
  PrintWriter out2=response.getWriter();//テキスト側のストリーム

1

編集

2018/03/03 09:32

投稿

eisaku123
eisaku123

スコア77

title CHANGED
File without changes
body CHANGED
@@ -7,6 +7,7 @@
7
7
 
8
8
 
9
9
  ```java
10
+ //①クライント側 アンドロイドアプリ
10
11
  @Override
11
12
  protected Result doInBackground(Void... voids) {
12
13
 
@@ -55,4 +56,53 @@
55
56
  }
56
57
 
57
58
  }
59
+ ```
60
+
61
+ ```java
62
+ //②サーバー側
63
+ public class Hello extends HttpServlet{
64
+
65
+ @Override
66
+ public void doGet(
67
+ HttpServletRequest request,
68
+ HttpServletResponse response
69
+ ) throws ServletException, IOException {
70
+
71
+ try {
72
+ response.setContentType("application/octet-stream ;charset=UTF-8");
73
+ response.setHeader("Content-Disposition" ,"attachment");
74
+
75
+ OutputStream out = response.getOutputStream();//画像側のストリーム<===getOutputStream()はこのレスポンスに対して既に呼び出されています
76
+
77
+        //////テキスト////////start/////////////////////////////////
78
+ PrintWriter out2=response.getWriter();//テキスト側のストリーム
79
+ out2.println("こんにちは777777");
80
+ //////テキスト////////end/////////////////////////////////
81
+
82
+
83
+ //////画像側////////start/////////////////////////////////
84
+ FileInputStream input =new FileInputStream("/Applications/Eclipse_4.7.2.app/Contents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/book777/WEB-INF/classes/chapter3/1.jpg");
85
+
86
+ int i = input.read();
87
+ while(i !=-1) {
88
+ char c = (char)i;
89
+ out.write(c); //ここでバイナリーデータを流している
90
+ i = input.read();
91
+ }
92
+ input.close();
93
+ out.close();
94
+
95
+ //////画像側////////end/////////////////////////////////
96
+
97
+ }catch(Exception e) {
98
+ System.out.println("意味不明のエラーです");
99
+ System.out.println(e);
100
+ }catch (Throwable e) {
101
+ System.out.println("byte[] data = IOUtils.toByteArray(input);でエラーです");
102
+ }
103
+
104
+ //out.close();
105
+ }
106
+
107
+ }
58
108
  ```