質問編集履歴

3

解決

2018/03/02 07:21

投稿

superZako
superZako

スコア11

test CHANGED
File without changes
test CHANGED
@@ -1,189 +1 @@
1
- ある商品の販売サイトを作るという設定で開発しています。jspから画像データや商品の名前、価格などを取得し、データベースに登録するという作業です。
2
-
3
- jspからjavaで受け取るときにgetParameterで受け取れないらしいので、以下のサイトを参考にそのままコードを入力してみました。javaクラスのほうでitem.write(uploadFile)の実行の時にExceptionエラーで(アクセス権限が拒否されました)と出ます。
4
-
5
- わかる方ご助力お願いします。
6
-
7
-
8
-
9
- 参考リンク
10
-
11
- https://stackoverflow.com/questions/5512442/input-type-text-value-from-jsp-form-enctype-multipart-form-data-returns-null
12
-
13
- ```jsp
14
-
15
- コード
16
-
17
- <form action="AddController" enctype="multipart/form-data" method = "post">
18
-
19
- <table cellspacing="1" cellpadding="8" border="0" bgcolor="#999999">
20
-
21
- <tr>
22
-
23
- <th width="100" bgcolor="#EBEBEB">コード</th>
24
-
25
- <td width="250" bgcolor="#FFFFFF"><input type="text" id="code" name="code" readonly="readonly" value=""> </td>
26
-
27
- </tr>
28
-
29
- <tr>
30
-
31
- <th width="100" bgcolor="#EBEBEB">品名<sup><font color="#FF0000">*</font></sup></th>
32
-
33
- <td width="250" bgcolor="#FFFFFF"><input type="text" id="name" name="name" value=""> </td>
34
-
35
- </tr>
36
-
37
- <tr>
38
-
39
- <th width="100" bgcolor="#EBEBEB">金額<sup><font color="#FF0000">*</font></sup></th>
40
-
41
- <td width="250" bgcolor="#FFFFFF"><input type="text" id="unitPrice" name="unitPrice" value=""> </td>
42
-
43
- </tr>
44
-
45
- <tr>
46
-
47
- <th width="100" bgcolor="#EBEBEB">数量<sup><font color="#FF0000">*</font></sup></th>
48
-
49
- <td width="250" bgcolor="#FFFFFF"><input type="text" id="count" name="count" value=""> </td>
50
-
51
- </tr>
52
-
53
- <tr>
54
-
55
- <th width="100" bgcolor="#EBEBEB">画像</th>
56
-
57
- <td width="250" bgcolor="#FFFFFF"><input type="file" id="image" name="image"> </td>
58
-
59
- </tr>
60
-
61
- <tr>
62
-
63
- <th bgcolor="#EBEBEB">おすすめ</th>
64
-
65
- <td bgcolor="#FFFFFF"><input type="checkbox" id="isPR" name="isPR" value="1" {% if item.isPR %} checked{% endif %}>おすすめ商品棚に並べる</input>
66
-
67
- <input type="hidden" name="isPR" value="0"></td>
68
-
69
- </tr>
70
-
71
- </table>
72
-
73
- ```
74
-
75
-
76
-
77
- ```java
78
-
79
- コード
80
-
81
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
82
-
83
- //文字コードをUTF-8にセット
84
-
85
- request.setCharacterEncoding("UTF-8");
86
-
87
- response.setContentType("text/html:charset=UTF-8");
88
-
89
- response.setCharacterEncoding("UTF-8");
90
-
91
-
92
-
93
- T001ItemDao itemDao = null;
94
-
95
-
96
-
97
- try{
98
-
99
- int result = 0;
100
-
101
- String kensaku;
102
-
103
- //遷移先指定変数
104
-
105
- String nextPage;
106
-
107
- itemDao = new T001ItemDao();
108
-
109
-
110
-
111
- FileItemFactory factory = new DiskFileItemFactory();
112
-
113
- ServletFileUpload upload = new ServletFileUpload(factory);
114
-
115
- ServletContext context = this.getServletContext();
1
+ FileNotExceptionエラー(アクセス権限)は、解決しました。ありがとうございました。
116
-
117
-
118
-
119
- Iterator<FileItem> iterator = upload.parseRequest(request).iterator();
120
-
121
- File uploadFile;
122
-
123
- String dirPath = this.getServletContext().getRealPath("WebContent/image");
124
-
125
-
126
-
127
- while (iterator.hasNext()) {
128
-
129
-
130
-
131
- FileItem item = iterator.next();
132
-
133
- System.out.println("item=="+item);
134
-
135
- if (!item.isFormField()) {
136
-
137
- String fileNameWithExt = item.getName();
138
-
139
- System.out.println(fileNameWithExt);
140
-
141
-
142
-
143
- File filePath = new File(dirPath);
144
-
145
-
146
-
147
- if (!filePath.exists()) {
148
-
149
- filePath.mkdirs();
150
-
151
- }
152
-
153
-
154
-
155
- uploadFile = new File(dirPath);
156
-
157
- System.out.println("path=" + context.getRealPath("/WebContent/image"));
158
-
159
- item.write(uploadFile);
160
-
161
- }
162
-
163
- }
164
-
165
-
166
-
167
- } catch(ClassNotFoundException e){
168
-
169
- e.printStackTrace();
170
-
171
- } catch(SQLException e){
172
-
173
- e.printStackTrace();
174
-
175
- } catch (Exception e) {
176
-
177
- e.printStackTrace();
178
-
179
- } finally{
180
-
181
- //データベース切断
182
-
183
- itemDao.close();
184
-
185
- }
186
-
187
- }
188
-
189
- ```

2

修正

2018/03/02 07:21

投稿

superZako
superZako

スコア11

test CHANGED
File without changes
test CHANGED
@@ -5,6 +5,10 @@
5
5
  わかる方ご助力お願いします。
6
6
 
7
7
 
8
+
9
+ 参考リンク
10
+
11
+ https://stackoverflow.com/questions/5512442/input-type-text-value-from-jsp-form-enctype-multipart-form-data-returns-null
8
12
 
9
13
  ```jsp
10
14
 

1

誤字

2018/02/27 05:43

投稿

superZako
superZako

スコア11

test CHANGED
File without changes
test CHANGED
@@ -100,8 +100,6 @@
100
100
 
101
101
  String nextPage;
102
102
 
103
- //T001ItemDaoクラスのインスタンス生成
104
-
105
103
  itemDao = new T001ItemDao();
106
104
 
107
105