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