struts2.0 实现图片上传实例

嵌入编辑页面的代码

<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>


Action中的代码

//上传图片参数
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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值