您的位置:首页 > 编程语言 > Java开发

Struts上传照片到服务器

2016-01-29 16:13 453 查看
               关于Struts上传照片的功能,可能很多人都已经接触过了。我记得我之前做过一个上传Jar包的功能,那时候是用SpringMVC做的,先做一个类似于上传的功能,将Jar包添加进行之后解读Jar包,是因为要拿到Jar包里面的类名、属性名和中文注释,但是又没办法手动将所有Jar包拷贝到项目下,所以想着用上传的方式来实现。现在是要做上传照片。将用户的照片保存到服务器的文件夹下,如果该文件夹存在,则直接上传,如果不存在就创建该文件夹然后再上传。

前台代码:
<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>
       这样,整个Struts上传图片到服务器的功能就完成了。其实整个流程重要的代码就那么几句,还有就是上传照片的时候要注意一定要把文件重命名之后再上传。还有,前台是浏览输入上传文件和submit按钮,只不过我前台处理让上传文本框隐藏了,所以点击用户照片的位置就能直接上传。本来想着也想把submit按钮给去掉呢,但是又没办法触发事件了。各位要是有好的想法,欢迎在下方留言,我们一起讨论、交流。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: