FileUtils文件上传.docx_第1页
FileUtils文件上传.docx_第2页
FileUtils文件上传.docx_第3页
FileUtils文件上传.docx_第4页
FileUtils文件上传.docx_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一、单文件上传: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/pjpeg1048576/upload/resultUpload.jsp!- - 备注1:在action中添加/upload主要用来设定保存上传文件的路径,相对路径和绝对路径都可 备注2:上面那个拦截器可要可不要,因为限制上传文件大小和类型在拦截器里配置不能起作用,要用常量配置如下面的限制文件大小的 上面的常量覆盖了perties里的设置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 mons.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 byteBUFFER_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 mons.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 byteBUFFER_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();finallyif(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 和 uploadContentType3:jsp页面如下: view plaincopy to clipboardprint?01. 03. 04. 05. 06. 07.struts2单文件上传页面 08. 09. 10. 11.标题: 12.请选选择: 13. 14. 15. 16. struts2单文件上传页面标题:请选选择: 二、多文件上传1:多文件上传的struts.xml配置文件和单文件上传配置文件一样,这里就不再介绍了。2:MultUploadAction如下: view plaincopy to clipboardprint?01.import java.io.File; 02.import java.io.IOException; 03. 04.import mons.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;iupload.length;i+) 52. File distFile = new File(realPath+uploadFileNamei); 53. try 54. FileUtils.copyFile(uploadi,distFile); 55. catch (IOException e) 56. e.printStackTrace(); 57. 58. 59. return SUCCESS; 60. 61. 62. import java.io.File;import java.io.IOException;import mons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class MultUploadAction extends ActionSupport private File upload;private String uploadFileName;private String uploadContentType;private String savePath;public String getSavePath() return savePath;public void setSavePath(String savePath) this.savePath = savePath;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;public String execute()String realPath = ServletActionContext.getServletContext().getRealPath(savePath);for(int i=0;iupload.length;i+)File distFile = new File(realPath+uploadFileNamei);try FileUtils.copyFile(uploadi,distFile); catch (IOException e) e.printStackTrace();return SUCCESS; 备注1: 多文件上传的实质的单文件上传一样,只不过多文件上传时文件,文件名,文件类型会保存到数组里面如: private File upload; private String uploadFileName; private String uploadContentType; 有一点很重要,那就是uploadi 和 uploadFileNamei 和 uploadContentTypei 相对应,理解这一点就好办了,利用循环针到单文件操作了 备注2: 这里用了第二种方法上传文件FileUtils.copyFile(file1,file2),自己写io流copy也可以3:jsp页面如下: view plaincopy to clipboardprint?01. 03. 04. 05. 06. 07.Insert title here 08. 09. 10. 11.文件1 12.文件2 13.文件3 14.文件4 15. 16. 17. 18. 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组件(/fileupload/); 另一个是Oreilly组织的COS框架(/cos/)。利用这两个

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论