java通过struts实现web中的文件上传

更新时间:2019-01-11 17:26:18 点击次数:1348次

单文件上传

fileupload.jsp


	
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'fileupload.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <!-- enctype 默认是 application/x-www-form-urlencoded -->
  22. <form action="FileUpload2" enctype="multipart/form-data" method="post" >
  23. 用户名:<input type="text" name="usename"> <br/>
  24. 上传文件:<input type="file" name="file1"><br/>
  25. <input type="submit" value="提交"/>
  26. </form>
  27. </body>
  28. </html>

具体处理上传的 FileUpload.java


	
  1. package com.struts2.fileupload;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import org.apache.struts2.ServletActionContext;
  8. import com.opensymphony.xwork2.ActionSupport;
  9. /**
  10. * 单个文件上传
  11. * @author Administrator
  12. * 上传文件其实是上传了两份,
  13. *
  14. * 首先将上传的文件保存到 default.properties 文件中 struts.multipart.saveDir键指定的目录中
  15. * 默认是空的
  16. * 保存在 Tomcat 6.0\work\Catalina\localhost\struts2目录下以.tmp后缀名的文件
  17. *
  18. * 如果要在 struts.multipart.saveDir 指定目录, 则可以在 src文件夹下 建一个 struts.properties,
  19. * 覆盖 default.properties 的某些键值
  20. *
  21. * 还有一份是 存放在自己设定的目录下
  22. */
  23. public class FileUpload extends ActionSupport {
  24. private String usename ;
  25. private File file1 ; //具体上传文件的 引用 , 指向临时目录中的临时文件
  26. private String file1FileName ; // 上传文件的名字 ,FileName 固定的写法
  27. private String file1ContentType ; //上传文件的类型, ContentType 固定的写法
  28. public String getUsename() {
  29. return usename;
  30. }
  31. public void setUsename(String usename) {
  32. this.usename = usename;
  33. }
  34. public File getFile1() {
  35. return file1;
  36. }
  37. public void setFile1(File file1) {
  38. this.file1 = file1;
  39. }
  40. public String getFile1FileName() {
  41. return file1FileName;
  42. }
  43. public void setFile1FileName(String file1FileName) {
  44. this.file1FileName = file1FileName;
  45. }
  46. public String getFile1ContentType() {
  47. return file1ContentType;
  48. }
  49. public void setFile1ContentType(String file1ContentType) {
  50. this.file1ContentType = file1ContentType;
  51. }
  52. @Override
  53. public String execute() throws Exception {
  54. //获取文件存储路径
  55. String path = ServletActionContext.getRequest().getRealPath("/upload");
  56. //输出流
  57. OutputStream os = new FileOutputStream(new File(path,file1FileName));
  58. //输入流
  59. InputStream is = new FileInputStream(file1);
  60. byte[] buf = new byte[1024];
  61. int length = 0 ;
  62. while(-1 != (length = is.read(buf) ) )
  63. {
  64. os.write(buf, 0, length) ;
  65. }
  66. is.close();
  67. os.close();
  68. return SUCCESS;
  69. }
  70. }

最终显示结果的页面,filedemo.jsp


	
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="s" uri="/struts-tags" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>My JSP 'filedemo.jsp' starting page</title>
  12. <meta http-equiv="pragma" content="no-cache">
  13. <meta http-equiv="cache-control" content="no-cache">
  14. <meta http-equiv="expires" content="0">
  15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <meta http-equiv="description" content="This is my page">
  17. <!--
  18. <link rel="stylesheet" type="text/css" href="styles.css">
  19. -->
  20. </head>
  21. <body>
  22. 上传成功: <br/>
  23. usename: <s:property value="usename" /><br/>
  24. file: <s:property value="file1FileName"/><br/>
  25. contentType: <s:property value="file1ContentType"/>
  26. </body>
  27. </html>

ps:还需定义spring bean  FileUpload2 指向 FileUpload.java   返回 success后 sturts中 定义 跳转到 filedemo.jsp



多文件上传

fileupload.jsp

这里我们以上传两个为例,如果要上传N个,相应增加
<input type="file" name="file1">即可
两个上传文件的name属性值要是一样的,后台方便处理


	
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'fileupload.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <!-- enctype 默认是 application/x-www-form-urlencoded -->
  22. <form action="FileUpload2" enctype="multipart/form-data" method="post" >
  23. 用户名:<input type="text" name="usename"> <br/>
  24. 上传文件:<input type="file" name="file1"><br/>
  25. 上传文件: <input type="file" name="file1"><br/> <!-- 两个名字相同 都是file1 -->
  26. <input type="submit" value="提交"/>
  27. </form>
  28. </body>
  29. </html>


具体处理上传文件的FileUpload2.java

多文件上传用集合的方式


	
  1. package com.struts2.fileupload;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.util.List;
  8. import org.apache.struts2.ServletActionContext;
  9. import com.opensymphony.xwork2.ActionSupport;
  10. /**
  11. * 多文件上传,用集合的方式
  12. * @author Administrator
  13. *
  14. */
  15. public class FileUpload2 extends ActionSupport {
  16. private String usename ;
  17. private List<File> file1 ;
  18. private List<String> file1FileName ;
  19. private List<String> file1ContentType ;
  20. public String getUsename() {
  21. return usename;
  22. }
  23. public void setUsename(String usename) {
  24. this.usename = usename;
  25. }
  26. public List<File> getFile1() {
  27. return file1;
  28. }
  29. public void setFile1(List<File> file1) {
  30. this.file1 = file1;
  31. }
  32. public List<String> getFile1FileName() {
  33. return file1FileName;
  34. }
  35. public void setFile1FileName(List<String> file1FileName) {
  36. this.file1FileName = file1FileName;
  37. }
  38. public List<String> getFile1ContentType() {
  39. return file1ContentType;
  40. }
  41. public void setFile1ContentType(List<String> file1ContentType) {
  42. this.file1ContentType = file1ContentType;
  43. }
  44. @Override
  45. public String execute() throws Exception {
  46. //获取文件存储路径
  47. String path = ServletActionContext.getRequest().getRealPath("/upload");
  48. for(int i = 0 ; i < file1.size() ; i++ )
  49. {
  50. OutputStream os = new FileOutputStream(new File(path,file1FileName.get(i)));
  51. InputStream is = new FileInputStream(file1.get(i));
  52. byte[] buf = new byte[1024];
  53. int length = 0 ;
  54. while(-1 != (length = is.read(buf) ) )
  55. {
  56. os.write(buf, 0, length) ;
  57. }
  58. is.close();
  59. os.close();
  60. }
  61. return SUCCESS;
  62. }
  63. }



用于显示的界面filedemo.jsp


	
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="s" uri="/struts-tags" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. <base href="<%=basePath%>">
  11. <title>My JSP 'filedemo2.jsp' starting page</title>
  12. <meta http-equiv="pragma" content="no-cache">
  13. <meta http-equiv="cache-control" content="no-cache">
  14. <meta http-equiv="expires" content="0">
  15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <meta http-equiv="description" content="This is my page">
  17. <!--
  18. <link rel="stylesheet" type="text/css" href="styles.css">
  19. -->
  20. </head>
  21. <body>
  22. 上传成功:<br/>
  23. usename:<s:property value="usename"/><br/>
  24. <!-- 遍历值 -->
  25. <s:iterator value="file1FileName" id="f"> <!-- id是一个对象,目前是一个字符串集合 可任意命名 -->
  26. 文件:<s:property value="#f"/> <br/>
  27. <!-- 这里也可以调用方法 <s:property value="#f.toUpperCase()"/> -->
  28. </s:iterator>
  29. </body>
  30. </html>

遍历集合的方式,用struts2提供的标签 iterator 可以实现

              <s:iterator value="file1FileName" id="f"> <!-- id是一个对象,目前是一个字符串集合  可任意命名-->
                                                 文件:<s:property value="#f"/> <br/>  
               <!-- 这里也可以调用方法  <s:property value="#f.toUpperCase()"/> toUpperCase()字符串的方法是把字母转为大写 -->

              
              </s:iterator>


本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

回到顶部
嘿,我来帮您!