1:Action类
protected File fromFile;
protected File toFile;
protected String savePath;
protected String presentPath;
protected String basePath;
protected String doctContentType;
protected String docFileName;
protected String oldPhotoUrl;
生成get,set方法
public String execute(){
//添加药品供应信息
docFileName=new Date().getTime()+"_"+fromFile.getName();
toFile=new File(savePath);
if(! (toFile.exists()) && ! (toFile.isDirectory())){
toFile.mkdirs();
}
fromFile.renameTo(new File(savePath+docFileName));
presentPath=basePath.replace("wmampr","wmamprUpload")+presentPath+docFileName;}
如果修改参照下面:
public String updatenewsinf()
{
toFile=new File(savePath);
if(! (toFile.exists()) && ! (toFile.isDirectory())){
toFile.mkdirs();
}
if(null==oldPhotoUrl || "".equals(oldPhotoUrl)){
if(null==fromFile || "".equals(fromFile)){
newinfo.setHitpicture(null);
}else{
docFileName=new Date().getTime()+"_"+fromFile.getName();
fromFile.renameTo(new File(savePath+docFileName));
presentPath=basePath.replace("wmampr","wmamprUpload")+presentPath+docFileName;
newinfo.setHitpicture(presentPath);
}
}else{
if(null==fromFile || "".equals(fromFile)){
System.out.println(oldPhotoUrl);
newinfo.setHitpicture(oldPhotoUrl);
}else{
docFileName=new Date().getTime()+"_"+fromFile.getName();
String imgName=oldPhotoUrl.substring(oldPhotoUrl.lastIndexOf("/")+1);
new File(savePath+imgName).delete();
fromFile.renameTo(new File(savePath+docFileName));
presentPath=basePath.replace("wmampr","wmamprUpload")+presentPath+docFileName;
newinfo.setHitpicture(presentPath);
}
}
String result=newsinfoservice.updateNewsInf(newinfo);
if("success".equals(result)){
return SUCCESS;
}else{
request.setAttribute("errorMessage","修改失败");
return ERROR;
}
}
2:webwork.proterties
webwork.multipart.parser=pell(或jakarta)
如果用pell要导入pell-multipart.jar包
3:拦截器
<action name="insertMedicineSupply" class="com.wmampr.md.action.MedicineAction" method="insertMedicineSupply">
<result name="success">/jsps/COMMON/COMMON20000-29990/common20000.jsp</result>
<result name="error">/jsps/COMMON/COMMON20000-29990/common20000.jsp</result>
<interceptor-ref name="fileUploadStack"/>
</action>
如果有乱码在<action></action>里加
<param name="contentType">
application/octet-stream;charset=UTF-8
</param>
4:页面
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<style type="text/css">
.newPreview
{
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);
}
</style>
<script type="text/javascript">
function preview5(){
var x = document.getElementById("fromFile");
if(!x || !x.value) return;
var patn = //.jpg$|/.jpeg$|/.gif$/i;
if(patn.test(x.value)){
// var y = document.getElementById("img5");
var presentImg=document.getElementById("presentImg");
presentImg.innerHTML="";
presentImg.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src=x.value;
presentImg.style.width = "100px";
presentImg.style.height = "100px";
//y.src = 'file://localhost/' + x.value;
}else{
alert("您选择的似乎不是图像文件。");
x.outerHTML="<input type='file' name='fromFile' id='fromFile' οnchange='preview5()'/>"
}
}
</script>
<form action="" method="post" name="form1" enctype="multipart/form-data">
<ww:hidden name="basePath" value="<%=basePath%>"></ww:hidden>
<input type="hidden" name="savePath" value="D:/WmamprUploadFiles/MD/image/">
<ww:hidden name="presentPath" value="MD/image/"></ww:hidden>
<input type="file" name="fromFile" id="fromFile" onchange="preview5()"/>
<div id="presentImg" align="center" class="newPreview">暂无图片</div>
5:注意
1:enctype="multipart/form-data"---是必须要的
2:定义style是为了在ie6或者ie7都能呈现图片