ImageVerifierCode 换一换
格式:DOCX , 页数:19 ,大小:19.19KB ,
资源ID:5455628      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5455628.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(struts2 单个文件上传的三种方法以及多文件上传.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

struts2 单个文件上传的三种方法以及多文件上传.docx

1、struts2 单个文件上传的三种方法以及多文件上传1、 填加JAR包:commons-fileupload-1.2.1.jar、connons-io-1.3.2.jar放在WEB-INF/lib下2、在from表单增加enctype属性form action=” method=”enctype=”multipart/form-data”3、Struts.xml配置文件中引入上传文件的配置4、Struts-upload.xml配置文件中填加上传的action处理.并且初始化上传文件路径参数Xml代码 1. 2. 3. /uploads 4. /ac.jsp 5. xml view plainc

2、opyprint?1. 2. 3. /uploads 4. /ac.jsp 5. /uploads /ac.jsp 以上是准备工作 ,做好之后就是上传功能的实现第一种方法:用字节流实现核心代码:Java代码 1. private String savePath; 2. private String title; 3. private File pic;/ 文件名 与视图层的名称一致 4. private String picContentType;/文件名+ContentType 5. private String picFileName;/文件名+FileName 6. 7. 8. publ

3、ic String getSavePath() 9. return ServletActionContext.getServletContext().getRealPath(savePath); 10. 11. public void setSavePath(String savePath) 12. this.savePath = savePath; 13. 14. public String getTitle() 15. return title; 16. 17. public void setTitle(String title) 18. this.title = title; 19. 2

4、0. public File getPic() 21. return pic; 22. 23. public void setPic(File pic) 24. this.pic = pic; 25. 26. public String getPicContentType() 27. return picContentType; 28. 29. public void setPicContentType(String picContentType) 30. this.picContentType = picContentType; 31. 32. public String getPicFil

5、eName() 33. return picFileName; 34. 35. public void setPicFileName(String picFileName) 36. this.picFileName = picFileName; 37. 38. /文件上传功能实现:方法一:字节流 39. public String uploadfile() 40. System.out.println(getSavePath()+getSavePath(); 41. System.out.println(savePath:+savePath); 42. System.out.println(t

6、itle:+title); 43. System.out.println(pic:+pic); 44. System.out.println(picContentType:+picContentType); 45. System.out.println(picFileName:+picFileName); 46. 47. FileInputStream fis=null; 48. FileOutputStream fos=null; 49. /定义保存的路径 50. String savepath=getSavePath(); 51. 52. /根据路径创建文件路径对象 53. File fi

7、le=new File(savepath); 54. if(!file.exists() 55. file.mkdirs(); 56. 57. 58. try 59. /创建输入流 60. fis=new FileInputStream(pic); 61. /创建输出流 62. fos=new FileOutputStream(savepath+/+picFileName); 63. 64. byte buf=new byte1024; 65. int n=0; 66. while(n=fis.read(buf)!=-1) 67. fos.write(buf, 0, n); 68. 69. i

8、f(fis!=null) 70. fis.close(); 71. 72. if(fos!=null) 73. fos.close(); 74. 75. catch (FileNotFoundException e) 76. / TODO Auto-generated catch block 77. e.printStackTrace(); 78. catch (IOException e) 79. / TODO Auto-generated catch block 80. e.printStackTrace(); 81. 82. return SUCCESS; 83. java view p

9、laincopyprint?1. private String savePath; 2. private String title; 3. private File pic;/ 文件名 与视图层的名称一致 4. private String picContentType;/文件名+ContentType 5. private String picFileName;/文件名+FileName 6. 7. 8. public String getSavePath() 9. return ServletActionContext.getServletContext().getRealPath(sav

10、ePath); 10. 11. public void setSavePath(String savePath) 12. this.savePath = savePath; 13. 14. public String getTitle() 15. return title; 16. 17. public void setTitle(String title) 18. this.title = title; 19. 20. public File getPic() 21. return pic; 22. 23. public void setPic(File pic) 24. this.pic

11、= pic; 25. 26. public String getPicContentType() 27. return picContentType; 28. 29. public void setPicContentType(String picContentType) 30. this.picContentType = picContentType; 31. 32. public String getPicFileName() 33. return picFileName; 34. 35. public void setPicFileName(String picFileName) 36.

12、 this.picFileName = picFileName; 37. 38. /文件上传功能实现:方法一:字节流 39. public String uploadfile() 40. System.out.println(getSavePath()+getSavePath(); 41. System.out.println(savePath:+savePath); 42. System.out.println(title:+title); 43. System.out.println(pic:+pic); 44. System.out.println(picContentType:+pic

13、ContentType); 45. System.out.println(picFileName:+picFileName); 46. 47. FileInputStream fis=null; 48. FileOutputStream fos=null; 49. /定义保存的路径 50. String savepath=getSavePath(); 51. 52. /根据路径创建文件路径对象 53. File file=new File(savepath); 54. if(!file.exists() 55. file.mkdirs(); 56. 57. 58. try 59. /创建输入流

14、 60. fis=new FileInputStream(pic); 61. /创建输出流 62. fos=new FileOutputStream(savepath+/+picFileName); 63. 64. byte buf=new byte1024; 65. int n=0; 66. while(n=fis.read(buf)!=-1) 67. fos.write(buf, 0, n); 68. 69. if(fis!=null) 70. fis.close(); 71. 72. if(fos!=null) 73. fos.close(); 74. 75. catch (FileNo

15、tFoundException e) 76. / TODO Auto-generated catch block 77. e.printStackTrace(); 78. catch (IOException e) 79. / TODO Auto-generated catch block 80. e.printStackTrace(); 81. 82. return SUCCESS; 83. private String savePath; private String title; private File pic;/ 文件名 与视图层的名称一致 private String picCon

16、tentType;/文件名+ContentType private String picFileName;/文件名+FileName public String getSavePath() return ServletActionContext.getServletContext().getRealPath(savePath); public void setSavePath(String savePath) this.savePath = savePath; public String getTitle() return title; public void setTitle(String

17、title) this.title = title; public File getPic() return pic; public void setPic(File pic) this.pic = pic; public String getPicContentType() return picContentType; public void setPicContentType(String picContentType) this.picContentType = picContentType; public String getPicFileName() return picFileNa

18、me; public void setPicFileName(String picFileName) this.picFileName = picFileName; /文件上传功能实现:方法一:字节流 public String uploadfile() System.out.println(getSavePath()+getSavePath(); System.out.println(savePath:+savePath); System.out.println(title:+title); System.out.println(pic:+pic); System.out.println(p

19、icContentType:+picContentType); System.out.println(picFileName:+picFileName); FileInputStream fis=null; FileOutputStream fos=null; /定义保存的路径 String savepath=getSavePath(); /根据路径创建文件路径对象 File file=new File(savepath); if(!file.exists() file.mkdirs(); try /创建输入流 fis=new FileInputStream(pic); /创建输出流 fos=

20、new FileOutputStream(savepath+/+picFileName); byte buf=new byte1024; int n=0; while(n=fis.read(buf)!=-1) fos.write(buf, 0, n); if(fis!=null) fis.close(); if(fos!=null) fos.close(); catch (FileNotFoundException e) / TODO Auto-generated catch block e.printStackTrace(); catch (IOException e) / TODO Aut

21、o-generated catch block e.printStackTrace(); return SUCCESS; 第二种方法:用FileUtils核心代码:Java代码1. / 文件上传功能实现:方法二:FileUtils 2. public String uploadfile() 3. / 定义保存的路径 4. String savepath = getSavePath(); 5. 6. / 根据路径创建文件路径对象 7. File file = new File(savepath); 8. if (!file.exists() 9. file.mkdirs(); 10. 11. 1

22、2. try 13. FileUtils.copyFile(pic, new File(file, getPicFileName(); 14. catch (Exception ex) 15. ex.printStackTrace(); 16. 17. return SUCCESS; 18. 19. java view plaincopyprint?1. / 文件上传功能实现:方法二:FileUtils 2. public String uploadfile() 3. / 定义保存的路径 4. String savepath = getSavePath(); 5. 6. / 根据路径创建文件路

23、径对象 7. File file = new File(savepath); 8. if (!file.exists() 9. file.mkdirs(); 10. 11. 12. try 13. FileUtils.copyFile(pic, new File(file, getPicFileName(); 14. catch (Exception ex) 15. ex.printStackTrace(); 16. 17. return SUCCESS; 18. 19. / 文件上传功能实现:方法二:FileUtils public String uploadfile() / 定义保存的路径

24、 String savepath = getSavePath(); / 根据路径创建文件路径对象 File file = new File(savepath); if (!file.exists() file.mkdirs(); try FileUtils.copyFile(pic, new File(file, getPicFileName(); catch (Exception ex) ex.printStackTrace(); return SUCCESS; 第三种方法:三层管道 核心代码:Java代码1. / 文件上传功能实现:方法三:三层管道 2. public String upl

25、oadfile() 3. 4. BufferedReader br = null; 5. BufferedWriter bw = null; 6. 7. / 定义保存的路径 8. String savepath = getSavePath(); 9. 10. / 根据路径创建文件路径对象 11. File file = new File(savepath); 12. if (!file.exists() 13. file.mkdirs(); 14. 15. 16. try 17. / 创建输入流 18. br = new BufferedReader(new InputStreamReader

26、(new FileInputStream( 19. pic); 20. / 创建输出流 21. bw = new BufferedWriter(new OutputStreamWriter( 22. new FileOutputStream(file + / + getPicFileName(); 23. 24. char buf = new char1024; 25. int n = 0; 26. while (n = br.read(buf) != -1) 27. bw.write(buf, 0, n); 28. 29. if (br != null) 30. br.close(); 31. 32. if (bw != null) 33. bw.close(); 34. 35. catch (FileNotFoundException e) 36. / TODO Auto-generated catch block 37. e.printStackTrace(); 38. catch (IOException e) 39. / TOD

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1