<pre name="code" class="html"><span style="font-size:14px;"><form name="webform" method="post" action="personBaseInfo_upload_employee_newEmploy.action" enctype="multipart/form-data" >
<table id="sreachArea" width="100%" border="0" cellspacing="1" cellpadding="0" class="module_table_bg">
<tr>
<td class="content_title_right" rowspan="6" width="10%">照片:</td>
<td class="content_content" width="40%" rowspan="6" align="center" >
<input name="pic" type="file" style="display:none" >
<input type="hidden" id ="SGuid" value="${personPhotoInfo.SGuid }" name="personPhotoInfo.SGuid"/>
<input type="hidden" id ="SPersonGuid" value="${personPhotoInfo.SPersonGuid }" name="personPhotoInfo.SPersonGuid"/>
<a href="javascript:void(sDialog());" >
c:if test="${personPhotoInfo.SFilePath != null}">
<img src="${appPath}/uploadImage//${personPhotoInfo.SFileIname}" width="105px;" height="130px;"></img>
</c:if>
<c:if test="${personPhotoInfo.SFilePath == null}">
<img src="${appPath}/common/images/login/UserImg.jpg" width="105px;" height="130px;"></img>
</c:if>
</a>
<input type="submit" value="上传"/>
</td>
</tr>
</table>
</form></span>
JS函数:<span style="font-size:14px;">//上传照片响应
function sDialog() {
var dataForm = document.forms['webform'];
dataForm.pic.click();
}</span> 后台图片上传代码:<span style="font-size:14px;">/**
* 功能:文件上传
* @author gaoying
* @return 跳转页面的字符串
* @throws IOException
* @throws ParseException
*/
public String upload(){
personPhotoInfo = new PersonPhotoInfo();
try {
//服务器路径
String serverPath = ServletActionContext.getServletContext().getRealPath("/");
System.out.println("服务器路径为:" + serverPath);
System.out.println("打印一下:" + serverPath + "uploadImage");
//判断服务器上是否有uploadImage这个文件夹,如果没有则创建
if(!(new File(serverPath + "uploadImage").isDirectory())){
//mkdir创建一层目录;mkdirs为创建多层目录
new File(serverPath+"uploadImage").mkdir();
}
//服务器的路径
String realpath = ServletActionContext.getServletContext().getRealPath("/uploadImage");
System.out.println("上传到服务器的地址realpath: "+realpath);
//给文件重新命名
File exFileName = new File(picFileName); //picFileName为原文件名
//System.out.println("重命名后的文件名称:" + renameFile(exFileName));
newFileName = renameFile(exFileName); //给上传文件重命名
//具体地址+文件名
fileSavePath = realpath+"\\"+renameFile(newFileName); //文件保存的路径名+文件名
System.out.println("文件保存的路径名+文件名:" + fileSavePath);
//列如------ E:\workspace\tomcat\webapps\sems\\005.jpg
if (pic != null) {
File savefile = new File(new File(realpath), newFileName.toString());
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(pic, savefile);
ActionContext.getContext().put("message", "文件上传成功");
}
//将照片信息保存到数据库
personPhotoInfo.setSFileIname(newFileName.toString());//文件存储名称
personPhotoInfo.setSFilePath(fileSavePath); //文件存储路径
personPhotoInfo.setSGuid(UUIDHexGenerator.getUUID());
personPhotoInfo.setSFileInitialName(picFileName); //上传文件原名
personPhotoInfo.setSFileExtension(picFileName.substring(picFileName.lastIndexOf('.')));
personPhotoInfo.setSPersonGuid(personBaseInfo.getSGuid()); //人员id
personPhotoInfo.setSUploadDate(new Date());
personPhotoInfoManager.savePersonPhotoInformation(personPhotoInfo);
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
} </span> 文件重命名代码:<span style="font-size:14px;">/**
* <p>Description: 上传文件重命名</p>
* @param file 文件名
* @return 文件
* @author : gaoying
* @update :
* @date : 2015-7-26
*/
public File renameFile(File file){
String body = "";
String ext = "";
Date date = new Date();
int pot = file.getName().lastIndexOf(".");
if(pot != -1){
body = date.getTime() +"";
ext = file.getName().substring(pot);
}else{
body = (new Date()).getTime()+"";
ext = "";
}
String newName = body + ext;
file = new File(file.getParent(), newName);
return file;
}</span> 版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/gaoying_blogs/article/details/47123679