首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2下传文件及多文件下传

2012-08-26 
struts2上传文件及多文件上传1. struts2中的文件上传?第一步:在WEBINF/lib下加入commons-fileupload-1.2.

struts2上传文件及多文件上传

1. struts2中的文件上传?
第一步:在WEB=INF/lib下加入commons-fileupload-1.2.1.jar , commons-io-1.3.2.jar。?

第二步:把form表单的enctype属性设置为"multipart/form-data",如?
?Java代码?

  1. <form?action="${pageContext.request.contextPath}/control/employee/list_execute.action"?enctype="multipart/form-data"?method="post">??
  2. ????文件:<input?type="file"?name="image">??
  3. ????<input?type="submit"?value="上传"/>??
  4. ??</form>??
  5. ??//${pageContext.request.contextPath}:获取服务器根路径??
????
第三步:在action中添加一下属性,?
Java代码?
  1. public?class?HelloWorldAction?{??
  2. ????private?File?image;???????????//与jsp表单中的名称对应??
  3. ????private?String?imageFileName;?//FileName为固定格式??
  4. ????private?String?imageContentType?;//ContentType为固定格式??
  5. ??????
  6. ????public?String?getImageContentType()?{??
  7. ????????return?imageContentType;??
  8. ????}??
  9. ????public?void?setImageContentType(String?imageContentType)?{??
  10. ????????this.imageContentType?=?imageContentType;??
  11. ????}??
  12. ????public?String?getImageFileName()?{??
  13. ????????return?imageFileName;??
  14. ????}??
  15. ????public?void?setImageFileName(String?imageFileName)?{??
  16. ????????this.imageFileName?=?imageFileName;??
  17. ????}??
  18. ????public?File?getImage()?{??
  19. ????????return?image;??
  20. ????}??
  21. ????public?void?setImage(File?image)?{??
  22. ????????this.image?=?image;??
  23. ????}??
  24. ????public?String?execute()?throws?Exception{??
  25. System.out.println("imageFileName?=?"+imageFileName);?????
  26. System.out.println("imageContentType?=?"+imageContentType);??
  27. ????????//获取服务器的根路径realpath??
  28. ????????String?realpath?=?ServletActionContext.getServletContext().getRealPath("/images");??
  29. System.out.println(realpath);??
  30. ????????if(image!=null){??
  31. ????????????File?savefile?=?new?File(new?File(realpath),?imageFileName);??
  32. ????????????if(!savefile.getParentFile().exists())?savefile.getParentFile().mkdirs();??
  33. ????????????FileUtils.copyFile(image,?savefile);??
  34. ????????????ActionContext.getContext().put("message",?"上传成功");??
  35. ????????}else{??
  36. ????????????ActionContext.getContext().put("message",?"上传失败");??
  37. ????????}??
  38. ????????return?"success";??
  39. ????}??
  40. }??
???
此外,可以在struts.xml中配置上传文件的大小?
??? <constant name="struts.multipart.maxSize" value="10701096"/> //最大上传配置成10M?
??? 默认的上传大小为2M?

思维拓展:如果要上传的文件非常大,如上传的是电影,好几百M ,用web上传一般是不可能难上传成功的,这时候要安装一个插件,类似于应用程序?
????????? socket ,通过网络通讯上传。?

2 . 多文件上传?
??? 在上面的基础上略加改动?
??? 1.jsp表单?
?Java代码?
  1. <form?action="${pageContext.request.contextPath}/control/employee/list_execute.action"?enctype="multipart/form-data"?method="post">??
  2. ????文件1:<input?type="file"?name="image"><br/>??
  3. ????文件2:<input?type="file"?name="image"><br/>??
  4. ????文件3:<input?type="file"?name="image"><br/>??
  5. ????<input?type="submit"?value="上传"/>??
  6. ??</form>??
??? 2. action中用数组接收?
?Java代码?
  1. ??public?class?HelloWorldAction?{??
  2. ????private?File[]?image;??
  3. ????private?String[]?imageFileName;??
  4. ????private?String[]?imageContentType?;??
  5. ????//省略了set和get方法??
  6. ????public?String?execute()?throws?Exception{??
  7. ??????????
  8. ????????String?realpath?=?ServletActionContext.getServletContext().getRealPath("/images");??
  9. ????????System.out.println(realpath);??
  10. ????????if(image!=null){??
  11. ????????????File?savedir?=?new?File(realpath);??
  12. ????????????if(!savedir.exists())??
  13. ???????????????{???
  14. ??????????????????savedir.mkdirs();??
  15. ???????????????}??
  16. ????????System.out.println("image.length?=?"+image.length);??
  17. ????????????for(int?i?=?0?;?i<image.length?;?i++){??
  18. ??????????????????
  19. System.out.println("imageContentType["+i+"]?=?"+imageContentType[i]);??
  20. ????????????????File?savefile?=?new?File(savedir,?imageFileName[i]);??
  21. ????????????????FileUtils.copyFile(image[i],?savefile);??
  22. ????????????}??
  23. ????????????ActionContext.getContext().put("message",?"上传成功");??
  24. ????????}??
  25. ????????return?"success";??
  26. ????}??
  27. } ?

热点排行