为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

FileUtils文件上传

2018-04-15 24页 doc 55KB 17阅读

用户头像

is_215732

暂无简介

举报
FileUtils文件上传FileUtils文件上传 一、文件上:单单单单单单 1:struts.xml文件配置如下: view plaincopy to clipboardprint? 01. 02. 05. 06. 07. 08. 10. 11. 12. 13. 14. 16. 17. 18. image/bmp,image/png,image/gif,image/jpeg,image/jpg 19. ,image/x-png, image/pjpeg 20. 21. 1048576 22. 23. 24...
FileUtils文件上传
FileUtils文件上传 一、文件上:单单单单单单 1:struts.xml文件配置如下: view plaincopy to clipboardprint? 01. 02. 05. 06. 07. 08. 10. 11. 12. 13. 14. 16. 17. 18. image/bmp,image/png,image/gif,image/jpeg,image/jpg 19. ,image/x-png, image/pjpeg 20. 21. 1048576 22. 23. 24. /upload 25. /resultUpload.jsp 26. 27. 28. 29. 30. 31. image/bmp,image/png,image/gif,image/jpeg,image/jpg ,image/x-png, image/pjpeg 1048576 /upload /resultUpload.jsp 单注1:在action中添加/upload主要用来定保存单单单单上文件的路径,相路径和路径都可单单单单单单单单单单单单单单单单单单 单注2:上面那个截器可要可不要,因限制上文件大小单单单单单单单单单单单单单单单单单单单 和型在截器里配置不能起作用,要用常量配置如下面的限制文件大小的单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单 上面的常量覆盖了default.properties里的置单单 2:FileUploadAction代如下:单单单单 view plaincopy to clipboardprint? 01.import java.io.BufferedInputStream; 02.import java.io.BufferedOutputStream; 03.import java.io.File; 04.import java.io.FileInputStream; 05.import java.io.FileNotFoundException; 06.import java.io.FileOutputStream; 07.import java.io.IOException; 08.import java.io.InputStream; 09.import java.io.OutputStream; 10. 11.import org.apache.commons.io.FileUtils; 12.import org.apache.struts2.ServletActionContext; 13. 14. 15.public class FileUploadAction { 16. 17. private final static int BUFFER_SIZE = 16 * 1024; 18. private File upload; 19. private String uploadFileName; 20. private String uploadContentType; 21. private String title; 22. private String SavePath; 23. private String ss; 24. 25. 26. public String getSavePath() { 27. return SavePath; 28. } 29. 30. public void setSavePath(String savePath) { 31. SavePath = savePath; 32. } 33. 34. public String getTitle() { 35. return title; 36. } 37. 38. public void setTitle(String title) { 39. this.title = title; 40. } 41. 42. public File getUpload() { 43. return upload; 44. } 45. 46. public void setUpload(File upload) { 47. this.upload = upload; 48. } 49. 50. public String getUploadContentType() { 51. return uploadContentType; 52. } 53. 54. public void setUploadContentType(String uploadContentType) { 55. this.uploadContentType = uploadContentType; 56. } 57. 58. public String getUploadFileName() { 59. return uploadFileName; 60. } 61. 62. public void setUploadFileName(String uploadFileName) { 63. this.uploadFileName = uploadFileName; 64. } 65. 66. 67. 68. /* 69. * copy单单单单单单单单单单单单单单单单单单个方法主要是用来文件上的,的原理就是分 打入出流,行文件拷的单单单单单单单单单单单单单单 70. */ 71. private void copy(File src, File dist) { 72. InputStream in = null; 73. BufferedOutputStream out = null; 74. 75. try { 76. in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE); 77. out = new BufferedOutputStream(new FileOutputStream(dist),BUFFER_SIZE); 78. byte[] buffer = new byte[BUFFER_SIZE]; 79. int len = 0; 80. try { 81. while ((len=in.read(buffer)) > 0) { 82. out.write(buffer, 0, len); 83. } 84. } catch (IOException e) { 85. e.printStackTrace(); 86. } 87. } catch (FileNotFoundException e) { 88. e.printStackTrace(); 89. }finally{ 90. if(null!=in){ 91. try { 92. in.close(); 93. } catch (IOException e) { 94. e.printStackTrace(); 95. } 96. } 97. if(null!=out){ 98. try { 99. out.close(); 100. } catch (IOException e) { 101. e.printStackTrace(); 102. } 103. } 104. } 105. } 106. 107. 108. public String upload() { 109. String distPath = ServletActionContext.getServletContext().getRealPath(getSavePath()) +"\\"+getUploadFileName(); 110. System.out.println(distPath); 111. File disFile = new File(distPath); 112. System.out.println("文件型单单: "+getUploadContentType()); 113.// try { 114.// FileUtils.copyFile(upload, disFile); 115.// } catch (IOException e) { 116.// e.printStackTrace(); 117.// } 118. copy(upload,disFile); 119. return "success"; 120. } 121. 122.} import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;public class FileUploadAction { private final static int BUFFER_SIZE = 16 * 1024; private File upload; private String uploadFileName; private String uploadContentType; private String title; private String SavePath; private String ss; public String getSavePath() { return SavePath; } public void setSavePath(String savePath) { SavePath = savePath; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType;} public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName;} /* * copy单单单单单单单单单单单单单单单单单单个方法主要是用来文件上的,的原理就是分 打入出流,行文件拷的单单单单单单单单单单单单单单 */ private void copy(File src, File dist) { InputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE); out = new BufferedOutputStream(new FileOutputStream(dist),BUFFER_SIZE); byte[] buffer = new byte[BUFFER_SIZE]; int len = 0; try { while ((len=in.read(buffer)) > 0) { out.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ if(null!=in){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if(null!=out){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } public String upload() { String distPath = ServletActionContext.getServletContext().getRealPath(getSavePath()) +"\\"+getUploadFileName(); System.out.println(distPath); File disFile = new File(distPath); System.out.println("文件型单单: "+getUploadContentType());//try { //FileUtils.copyFile(upload, disFile);//} catch (IOException e) { //e.printStackTrace(); //} copy(upload,disFile); return "success"; } } 单注: 1:ServletActionContext context = ServletActionContext.getServletContext(); 2: ServletActionContext.getServletContext().getRealPath(getSavePath()) 得到上文件单单单目的文件的路径单单单单单单 3: ServletActionContext.getServletContext().getRealPath(getSavePath()) +"\\"+getUploadFileName() 一个文件的路径,个主要是用来建立出流单单单单单单单单单单单单单单单单单 如File disFile = new File(distPath) OutputStream out = new BufferedOutputStream(new FileOutputStream(dist),BUFFER_SIZE); 4: 上文件可以自己用单单单单单单单单io流拷文件,可以用单单单单单单单单FileUtils.copyFile(file1,file2); 5:File的名字要和上面单单单File域name属性的一致如:单单单单单upload 单FileUploadAction中的 File的名字也单单upload (private File upload),除此之外要添加以下两个属性:单单单单单单单单单单单 private String uploadFileName 单单单单单单单单于上文件的名字 private String uploadContentType 单单单单单单单单于上文件的型 以上两个属性的名字的命名必需以File的属性名,然后再加上单单单单单单单单FileName或 ContentType如 uploadFileName 和 uploadContentType 3:jsp单面如下: view plaincopy to clipboardprint? 01.<%@ page language="java" contentType="text/html; charset=utf-8" 02. pageEncoding="utf-8"%> 03. 04. 05. 06. 07.struts2单单单单文件上面 08. 09. 10.
11.单单:,
12.单单单单:
13. 14.
15. 16. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> struts2单单单单文件上面
单单:,
单单单单:
二、多文件上单 1:多文件上的单单struts.xml配置文件和文件上配置文件一,里就不再介了。单单单单单单单单单单单单单单单单单单单单单 2:MultUploadAction如下: view plaincopy to clipboardprint? 01.import java.io.File; 02.import java.io.IOException; 03. 04.import org.apache.commons.io.FileUtils; 05.import org.apache.struts2.ServletActionContext; 06. 07.import com.opensymphony.xwork2.ActionSupport; 08. 09.public class MultUploadAction extends ActionSupport { 10. 11. private File[] upload; 12. private String[] uploadFileName; 13. private String[] uploadContentType; 14. private String savePath; 15. 16. 17. public String getSavePath() { 18. return savePath; 19. } 20. 21. public void setSavePath(String savePath) { 22. this.savePath = savePath; 23. } 24. 25. public File[] getUpload() { 26. return upload; 27. } 28. 29. public void setUpload(File[] upload) { 30. this.upload = upload; 31. } 32. 33. public String[] getUploadContentType() { 34. return uploadContentType; 35. } 36. 37. public void setUploadContentType(String[] uploadContentType) { 38. this.uploadContentType = uploadContentType; 39. } 40. 41. public String[] getUploadFileName() { 42. return uploadFileName; 43. } 44. 45. public void setUploadFileName(String[] uploadFileName) { 46. this.uploadFileName = uploadFileName; 47. } 48. 49. public String execute(){ 50. String realPath = ServletActionContext.getServletContext().getRealPath(savePath); 51. for(int i=0;i 03. 04. 05. 06. 07.Insert title here 08. 09. 10.
11.文件1
12.文件2
13.文件3
14.文件4
15. 16.
17. 18. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> Insert title here
文件1
文件2
文件3
文件4
单单: 1.文件上的原理:单单单单单 单单单单元素的enctype属性指定的是表数据的方式,属性有单单单单单单单单单单单单单3个:单单 1) application/x-www-form-urlencoded:是默方式,它只理表域里的单单单单单单单单单单单单单单单单单单value属性,采用方式的表会将表域的理成单单单单单单单单单单单单单单单单单单单单单单单URL单单方式。 2) multipart/form-data:方式的表会以二制流的方式来单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单单理表数据,方式会把文件域指定文件的内容也封装到求参数里。 3) text/plain:方式主要适用于直接通表送件的方式。单单单单单单单单单单单单单单单单单单单单单单单 单单文件上是web单单单单单单单单单单单单单单单单单单单单单单单单用常用到的一个知。原理是,通表元素置 enctype="multipart/form-data"属性,表提交的数据以二制的方式提交。单单单单单单单单单单单单单单单单单单单单 单单单在接收此求的Servlet中用二制流来取内容,就可以取得上单单单单单单单单单单单单单单单单单单单单单单单单文件的内容,从而文件的上。 在Java单单单单单单单单域中,有两个常用的文件上目:一个是Apache单单Jakarta的Common-FileUpload单件(); 另一个是Oreilly单单的COS框架()。利用两个框架单单单单单都能很方便的文件的上。单单单单单单单单 2. Struts2的文件上:单单 Struts2并未提供自己的求解析器,也就是就单单单单单单单单单单Struts2不会自己去理单单multipart/form-data 的求,它需要用其他求解析器,将单单单单单单单单单单单单单单单单单HTTP单单单单单单单单求中的表域解析出来。 但Struts2在原有的上解析器基上做了一单单单单单单单单单单单单单单单单单单单单单单单封装,更一化了文件上。步步 Struts2默使用的是单单单单单Jakarta的Common-FileUpload框架来上文件,因此,要在单单单单单单单单单web单用中增加两个Jar文件:commons-fileupload-1.2.jar 和commons-io- 1.3.1.jar。它在原上框架上做了一单单单单单单单单单单单单封装,化了文件步 上的代,取消了不同上框架上的程差异。单单单单单单单单单单单单单单单单单单单单单单单 单单单单单单单单如果要改成其它的文件上框架,可以修改struts.multipart.parser常量的单单cos/pell,默单单是jakata。并在classpath中增加相上件的。单单单单单单单单单 3. 如果上文件失,系返回到单单单单单单单单单单单input单单单单单单单的面,要在input单单单单单单单单单的面出文件单单单单单单单单失信息,可以在input单单单单单单单 的面中增加来示信息。单单单单单单单 单单单单单单单单单单单单单单单单单单单单单单单单然,的提示不太友好,用使用国化信息。在国 化源文件中添加如下三句:单单单单单单单单单单单单 #更改上文件型不允的提示信息单单单单单单单单单单单单单 struts.messages.error.content.type.not.allowed=文件上失:你要上的文件型不允单单单单单单单单单单单单单单单单 #更改上文件太大的提示信息单单单单单单单单单单 struts.messages.error.file.too.large=文件上失:你要上的文件太大单单单单单单单单单单单单单 #文件上其它信息单单单单单单单 struts.messages.error.uploading=文件上失:生内部单单单单单单单单单单 4.但是有一点要注意Struts2默文件上最大单单单单单单单单2M即便你置了单单单5242880 单单单单单单当上的文件大于2M单单单单单单单单单单单单单单候也会出的要置另外一个常量 单单单要他的value单 单单单单单单单单单置的比你限定上最大要大一点。 5.有一点要注意单单单单struts2提供了一个 ServletActionContext.getServletContext().getResourceAsStream(filePath);方法根据路径返回单入流;但是 单个方法和servletContext有,当你把下的文件放在目的根单单单单单单单单单单单单单单单单 目中才可以否会出的,最保的法就是根据文件单单单单单单单单单单单单单单单单单单单单单单单单 路径找到个文件然后在化成入流返回。单单单单单单单单单单单单单单单单 本文来自CSDN博客,明出:单单单单单单单单
/
本文档为【FileUtils文件上传】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索