struts2.0画像アップロードインスタンスの実装
編集ページに埋め込まれたコード
ここでpicUrlIdは、アップロード後に返されるファイルのパスがどのテキストボックスに記入されているかのフラグであり、同じページでこのアップロード画像のページを複数参照できるようにする
upLoadFile.jsp
アクションのコード
<tr>
<td width="40%" align="right" class="info-left"> :</td>
<td width="60%" class="info-right">
<s:textfield name="icon" cssClass="input-text" onfocus="switchClass(this)" onblur="switchClass(this)"/>
<s:hidden name="sysMenu.icon"/>
</td>
</tr>
<tr>
<td width="40%" align="right" class="info-left"> </td>
<td width="60%" class="info-right">
<div align="left">
<iframe style="top:2px" ID="UploadIcon" src="<%=basePath%>common/upLoadFile.jsp?picUrlId=1" frameborder=0 scrolling=no width="300" height="25"></iframe>
</div>
</td>
</tr>
<tr>
<td width="40%" align="right" class="info-left"> :</td>
<td width="60%" class="info-right">
<s:textfield name="iconOpen" cssClass="input-text" onfocus="switchClass(this)" onblur="switchClass(this)"/>
<s:hidden name="sysMenu.iconOpen"/>
</td>
</tr>
<tr>
<td width="40%" align="right" class="info-left"> </td>
<td width="60%" class="info-right">
<div align="left">
<iframe style="top:2px" ID="UploadIconOpen" src="<%=basePath%>common/upLoadFile.jsp?picUrlId=2" frameborder=0 scrolling=no width="300" height="25"></iframe>
</div>
</td>
</tr>
ここでpicUrlIdは、アップロード後に返されるファイルのパスがどのテキストボックスに記入されているかのフラグであり、同じページでこのアップロード画像のページを複数参照できるようにする
upLoadFile.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" target=_self><!-- IE6 , target=_self -->
<title> </title>
<link rel="StyleSheet" type="text/css" href="common/dtree.css"/>
<script type="text/javascript" src="common/dtree.js"></script>
<script type="text/javascript">
function returnValue(picUrl){
formdiv.style.display="none";
messagediv.style.display="";
var picUrlId = document.getElementById("picUrlId").value;
if (picUrlId==1){
parent.document.sysMenuForm.icon.value=picUrl;
} else if (picUrlId==2){
parent.document.sysMenuForm.iconOpen.value=picUrl;
}
}
function check(){
var strFileName=document.uploadForm.myFile.value;
if (strFileName=="")
{
alert(" ");
document.uploadForm.myFile.focus();
return false;
}
}
</script>
</head>
<body topmargin="0" bottommargin="0" leftmargin="0" onload="this.focus();">
<table width="100%" height="100%" cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top" valign="middle">
<div id="formdiv">
<form action="sysmanage/SysMenuManageAction!upLoadImage.action" method="post" name="uploadForm" onSubmit="check();" enctype="multipart/form-data">
<input name="myFile" type="FILE">
<input type="submit" name="Submit" value=" " >
<input name="picUrlId" type="hidden" id="picUrlId" value="<%=request.getParameter("picUrlId")%>">
</form>
</div>
<div id="messagediv" style="display:none;font-size:9pt;text-align:center;">
!
</div>
</td>
</tr>
</table>
</body>
</html>
<s:if test="#request.imageFilePath!=null" >
<script type="text/javascript">
returnValue('<s:property value="imageFilePath"/>');
</script>
</s:if>
アクションのコード
//
private static final int BUFFER_SIZE = 16 * 1024 ;
private File myFile; //
private String contentType;
private String fileName;
private String imageFilePath; //
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf( "." );
return fileName.substring(pos);
}
public String upLoadImage(){
log.info(">>>>>>>>>>>>>> ........");
HttpServletRequest request = ServletActionContext.getRequest();
String imageFileName = new Date().getTime() + getExtention(fileName);
File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "img" ) + "/" + imageFileName);
imageFilePath = "img" + "/" + imageFileName;
copy(myFile, imageFile);
log.info(">>>>>>>>>> ");
return Constants.Dispatcher.TO_UPLOAD_FILE;
}