文件上传功能是最基本的所以需要真正的掌握
Java代码
=========文件上传功能================
@Property
private UploadedFile file;
@Inject
private ApplicationGlobals globals;
//获得绝对路径
String path = globalsgetServletContext()getRealPath(/images/person/head);
File copied = new File(path +/+ filegetFileName());
filewrite(copied); //写入项目
===========js=============
function fileBtn(){
var file=documentgetElementById(file)value;
//var houzui=kzName(file);
//获取文件后缀名并转成小写
var ext=filesubstring(filelastIndexOf())toLowerCase();
if( filevalue==){
alert(请选择上传的图片);
return false;
}else{
if(ext!=gif && ext!=jpg && ext!=jpeg && ext!=bmp) {
alert(此图片类型不支持:[ +ext+ ]);
return false;
}
}
return true;
}
//获取后缀名
function kzName(u)
{
var s = /[^]+$/exec(u);
return (s!=null)?s[]:null;
}
=============filetml================
<div id=fileDiv>
<t:form>
图片上传
<input t:type=upload t:id=file name=file size=/>
<input type=submit value=提 交 onclick=return fileBtn();/>
</t:form>
<! 用来提示信息 >
<div><span id=errormsg><t:if t:test=errorCode>${errorMsg}</t:if></span></div>
</div>
===============filejava===相关代码=============
@Property
private UploadedFile file;
@Persist(PersistenceConstantsFLASH)
@Property
private String message;
@Inject
private Messages messages;
@Property
@Persist(value=flash)
private int errorCode;
Object onUploadException(FileUploadException ex)
{
message = Upload exception: + exgetMessage();
return this;
}
//用来在页面做提示信息
public String getErrorMsg(){
switch (errorCode) {
case :
return messagesget(fileNameMsg);
case :
return messagesget(fileSuccess);
default:
break;
}
return ;
}