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

質問編集履歴

3

解決

2018/03/02 07:21

投稿

superZako
superZako

スコア11

title CHANGED
File without changes
body CHANGED
@@ -1,95 +1,1 @@
1
- ある商品の販売サイトを作るという設定で開発しています。jspから画像データや商品の名前、価格などを取得し、データベースに登録するという作業です。
2
- jspからjavaで受け取るときにgetParameterで受け取れないらしいので、以下のサイトを参考にそのままコードを入力してみました。javaクラスのほうでitem.write(uploadFile)の実行の時にExceptionエラーで(アクセス権限が拒否されました)と出ます。
3
- わかる方ご助力お願いします。
4
-
5
- 参考リンク
6
- https://stackoverflow.com/questions/5512442/input-type-text-value-from-jsp-form-enctype-multipart-form-data-returns-null
7
- ```jsp
8
- コード
9
- <form action="AddController" enctype="multipart/form-data" method = "post">
10
- <table cellspacing="1" cellpadding="8" border="0" bgcolor="#999999">
11
- <tr>
12
- <th width="100" bgcolor="#EBEBEB">コード</th>
13
- <td width="250" bgcolor="#FFFFFF"><input type="text" id="code" name="code" readonly="readonly" value=""> </td>
14
- </tr>
15
- <tr>
16
- <th width="100" bgcolor="#EBEBEB">品名<sup><font color="#FF0000">*</font></sup></th>
17
- <td width="250" bgcolor="#FFFFFF"><input type="text" id="name" name="name" value=""> </td>
18
- </tr>
19
- <tr>
20
- <th width="100" bgcolor="#EBEBEB">金額<sup><font color="#FF0000">*</font></sup></th>
21
- <td width="250" bgcolor="#FFFFFF"><input type="text" id="unitPrice" name="unitPrice" value=""> </td>
22
- </tr>
23
- <tr>
24
- <th width="100" bgcolor="#EBEBEB">数量<sup><font color="#FF0000">*</font></sup></th>
25
- <td width="250" bgcolor="#FFFFFF"><input type="text" id="count" name="count" value=""> </td>
26
- </tr>
27
- <tr>
28
- <th width="100" bgcolor="#EBEBEB">画像</th>
29
- <td width="250" bgcolor="#FFFFFF"><input type="file" id="image" name="image"> </td>
30
- </tr>
31
- <tr>
32
- <th bgcolor="#EBEBEB">おすすめ</th>
33
- <td bgcolor="#FFFFFF"><input type="checkbox" id="isPR" name="isPR" value="1" {% if item.isPR %} checked{% endif %}>おすすめ商品棚に並べる</input>
34
- <input type="hidden" name="isPR" value="0"></td>
35
- </tr>
36
- </table>
37
- ```
38
-
39
- ```java
40
- コード
41
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
42
- //文字コードをUTF-8にセット
43
- request.setCharacterEncoding("UTF-8");
44
- response.setContentType("text/html:charset=UTF-8");
45
- response.setCharacterEncoding("UTF-8");
46
-
47
- T001ItemDao itemDao = null;
48
-
49
- try{
50
- int result = 0;
51
- String kensaku;
52
- //遷移先指定変数
53
- String nextPage;
54
- itemDao = new T001ItemDao();
55
-
56
- FileItemFactory factory = new DiskFileItemFactory();
57
- ServletFileUpload upload = new ServletFileUpload(factory);
58
- ServletContext context = this.getServletContext();
1
+ FileNotExceptionエラー(アクセス権限)は、解決しました。ありがとうございました。
59
-
60
- Iterator<FileItem> iterator = upload.parseRequest(request).iterator();
61
- File uploadFile;
62
- String dirPath = this.getServletContext().getRealPath("WebContent/image");
63
-
64
- while (iterator.hasNext()) {
65
-
66
- FileItem item = iterator.next();
67
- System.out.println("item=="+item);
68
- if (!item.isFormField()) {
69
- String fileNameWithExt = item.getName();
70
- System.out.println(fileNameWithExt);
71
-
72
- File filePath = new File(dirPath);
73
-
74
- if (!filePath.exists()) {
75
- filePath.mkdirs();
76
- }
77
-
78
- uploadFile = new File(dirPath);
79
- System.out.println("path=" + context.getRealPath("/WebContent/image"));
80
- item.write(uploadFile);
81
- }
82
- }
83
-
84
- } catch(ClassNotFoundException e){
85
- e.printStackTrace();
86
- } catch(SQLException e){
87
- e.printStackTrace();
88
- } catch (Exception e) {
89
- e.printStackTrace();
90
- } finally{
91
- //データベース切断
92
- itemDao.close();
93
- }
94
- }
95
- ```

2

修正

2018/03/02 07:21

投稿

superZako
superZako

スコア11

title CHANGED
File without changes
body CHANGED
@@ -2,6 +2,8 @@
2
2
  jspからjavaで受け取るときにgetParameterで受け取れないらしいので、以下のサイトを参考にそのままコードを入力してみました。javaクラスのほうでitem.write(uploadFile)の実行の時にExceptionエラーで(アクセス権限が拒否されました)と出ます。
3
3
  わかる方ご助力お願いします。
4
4
 
5
+ 参考リンク
6
+ https://stackoverflow.com/questions/5512442/input-type-text-value-from-jsp-form-enctype-multipart-form-data-returns-null
5
7
  ```jsp
6
8
  コード
7
9
  <form action="AddController" enctype="multipart/form-data" method = "post">

1

誤字

2018/02/27 05:43

投稿

superZako
superZako

スコア11

title CHANGED
File without changes
body CHANGED
@@ -49,7 +49,6 @@
49
49
  String kensaku;
50
50
  //遷移先指定変数
51
51
  String nextPage;
52
- //T001ItemDaoクラスのインスタンス生成
53
52
  itemDao = new T001ItemDao();
54
53
 
55
54
  FileItemFactory factory = new DiskFileItemFactory();