[mw_shl_code=java、真] <body><%-- ORMフォームはバイナリ形式でクエリされ、enctype="multipart/form-data" を設定する必要があります。 --%><form action="${pageContext.request.contextPath}/de/ServletActionupload" enctype="multipart/form-data" method="post"/> file<input type="file" name="image"/> <input type="submit" value="upload"/> </form> </body>
<action name="ServletAction*" class="action.ServletAction" method="{1}"> <結果名="メッセージ">/index.jsp</result> <結果名="uploadmsg">/uploadmsg.jsp</result> </action>
... プライベートファイルイメージ; private 文字列 imageFileName; フィールド名の後にファイル名が続きます
public String getImageFileName() { imageFileNameを返します; }
public void setImageFileName(String imageFileName) { this.imageFileName = imageFileName; }
public File getImage() { 画像を返す; }
public void setImage(File image) { this.image = image; }
/** * ファイルをアップロード * @return * @throws IOException */ public String upload(){
試してみて { String realpath = ServletActionContext.getServletContext().getRealPath("/images"); System.out.println(realpath); もし(image!=null){ ファイル savefile = new File(new File(realpath),imageFileName); if(!savefile.getParentFile().exists()) { savefile.getParentFile().mkdir(); } FileUtils.copyFile(image,savefile); ActionContext.getContext().put("msg", "アップロード成功!");
}else { ActionContext.getContext().put("msg", "ファイルを選択してください");
} } catch (IOException e) { TODO自動生成キャッチブロック e.printStackTrace(); ActionContext.getContext().put("msg", "アップロード失敗!"); } 「UploadMSG」を返す; }
[/mw_shl_code] |