Struts上传照片到服务器
2016-01-29 16:13
453 查看
关于Struts上传照片的功能,可能很多人都已经接触过了。我记得我之前做过一个上传Jar包的功能,那时候是用SpringMVC做的,先做一个类似于上传的功能,将Jar包添加进行之后解读Jar包,是因为要拿到Jar包里面的类名、属性名和中文注释,但是又没办法手动将所有Jar包拷贝到项目下,所以想着用上传的方式来实现。现在是要做上传照片。将用户的照片保存到服务器的文件夹下,如果该文件夹存在,则直接上传,如果不存在就创建该文件夹然后再上传。
前台代码:
JS函数:
前台代码:
<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按钮给去掉呢,但是又没办法触发事件了。各位要是有好的想法,欢迎在下方留言,我们一起讨论、交流。
相关文章推荐
- Java历史
- Spring bean的生命周期
- Java批量插入、更新数据
- Spring 实现容器销毁该bean的时候获得一次回调
- 未知来源应用禁止/允许开关
- Struts2中Result类型配置详解
- java并发之CAS操作
- 关于Spark 1.5 版本中Spark自己管理内存而不是由Java管理内存的解释
- java -cp 用法介绍
- 淘宝技术发展(Java时代:脱胎换骨)
- Java内存溢出的详细解决方案
- Struts Spring Hibernate三大框架整合
- Spring 依赖注入后行为实现
- 把Eclipse项目迁移到AndroidStudio
- java引用的问题
- 删除.java文件中所有未用到的引用的包的菜单命令或快捷键是什么?
- [转]Java堆和栈的区别 经典总结
- 利用Java反射机制将Bean转成Map
- Java四种线程池的使用
- Java实现把测试结果写入Excel表中