不是很完美Struts2文件上传带进度条



Struts2文件上传带进度条,虽然不是很完美先说一下大概是这样实现的,在我们平时的上传表单里面,除了文件上传之外,也许还有其他的信息需要填写的,这样问题就来了:点击上传按钮之后,这个表单都封装并提交上去了,在上传完成后整个页面就跳转了。而且也不利于我们验证用户输入。很多人这样做的,把这2个操作分开,当然这样也行。。。

我们这样做:一个普通页面(可以用于填写所有信息的),一个文件上传页面,一个上传成功页面(这个不怎么重要)

然后关键的就是:把文件上传的页面嵌入到普通页面里面去,相当于说文件上传实际是由上传页面独立完成,信息填写页面也独立成功信息填写,这里我们用iframe。

不多说,上代码:

1、处理上传的Action

Java代码
1.package org.yzsoft.upload.action;
2.
3.import java.io.*;
4.import org.apache.struts2.ServletActionContext;
5.import com.opensymphony.xwork2.ActionSupport;
6.
7.public class UploadAction extends ActionSupport {
8. private static final int BUFFER_SIZE = 100;// 上传的字符流大小(单位:字节)
9. // 用File来封装上传文件域对象
10. private File file;
11. // 保存文件的目录路径(通过自动注入)
12. private static String ext; //文件后缀
13. private static String fileFileName;
14. private static float percent = 0;//百分比
15.
16.
17. // 自己封装的一个把源文件对象复制成目标文件对象
18.
19. private static void copy(File src, File dst) {
20. InputStream in = null;
21. OutputStream out = null;
22. float completedSize = 0;//已经上传的大小
23. float fileSize = 0;//文件总大小
24. try {
25. in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
26. out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
27. fileSize = in.available();
28. byte[] buffer = new byte[BUFFER_SIZE];
29. int len = 0;
30. while ((len = in.read(buffer)) > 0) {
31. out.write(buffer, 0, len);
32. completedSize += (long) len;
33. percent = completedSize / fileSize * 100;//百分比计算
34. }
35. } catch (Exception e) {
36. System.out.println(e);
37. } finally {
38. if (null != in) {
39. try {
40. in.close();
41. } catch (IOException e) {
42. System.out.println(e);
43. }
44. }
45. if (null != out) {
46. try {
47. out.close();
48. } catch (IOException e) {
49. System.out.println(e);
50. }
51. }
52. }
53. }
54.
55. public String sumPre() { //计算后百分比输回页面
56. try {
57. PrintWriter out = ServletActionContext.getResponse().getWriter();
58. System.out.println(getFileFileName() + ” filename”);
59. out.print(percent);
60. } catch (IOException e) {
61. System.out.println(“异常:” + e);
62. }
63. return null;
64. }
65. //上传的方法
66. public String upload() {
67. try {
68. if (percent >= 99.9) {//这里保险起见我们设百分比》99.9就清0,避免进度条到了100%就停在那里不动的尴尬
69. percent = 0;
70. }
71. File srcfile = this.getFile();// 自动注入的方法取得文件域的对象
72. // 根据服务器的文件保存地址和原文件名创建目录文件全路径
73. String uploadPath = ServletActionContext.getServletContext().getRealPath(“upload”);// 上传路径
74. ext = fileFileName.substring(fileFileName.lastIndexOf(“.”)).toLowerCase();// 取得后缀
75. System.out.println(“取得后缀 ” + ext);
76. File dstFile = new File(uploadPath, fileFileName);
77. copy(srcfile, dstFile);
78. } catch (Exception e) {
79. System.out.println(e);
80. }
81. return “success”;
82.
83. }
84. /**************/
85.
86. public File getFile() {
87. return file;
88. }
89.
90. public void setFile(File file) {
91. this.file = file;
92. }
93.
94.
95. public String getFileFileName() {
96. return fileFileName;
97. }
98.
99. public void setFileFileName(String fileFileName) {
100. this.fileFileName = fileFileName;
101. }
102.}
package org.yzsoft.upload.action;

import java.io.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
private static final int BUFFER_SIZE = 100;// 上传的字符流大小(单位:字节)
// 用File来封装上传文件域对象
private File file;
// 保存文件的目录路径(通过自动注入)
private static String ext; //文件后缀
private static String fileFileName;
private static float percent = 0;//百分比

// 自己封装的一个把源文件对象复制成目标文件对象

private static void copy(File src, File dst) {
InputStream in = null;
OutputStream out = null;
float completedSize = 0;//已经上传的大小
float fileSize = 0;//文件总大小
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
fileSize = in.available();
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
completedSize += (long) len;
percent = completedSize / fileSize * 100;//百分比计算
}
} catch (Exception e) {
System.out.println(e);
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
System.out.println(e);
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
}

public String sumPre() { //计算后百分比输回页面
try {
PrintWriter out = ServletActionContext.getResponse().getWriter();
System.out.println(getFileFileName() + ” filename”);
out.print(percent);
} catch (IOException e) {
System.out.println(“异常:” + e);
}
return null;
}
//上传的方法
public String upload() {
try {
if (percent >= 99.9) {//这里保险起见我们设百分比》99.9就清0,避免进度条到了100%就停在那里不动的尴尬
percent = 0;
}
File srcfile = this.getFile();// 自动注入的方法取得文件域的对象
// 根据服务器的文件保存地址和原文件名创建目录文件全路径
String uploadPath = ServletActionContext.getServletContext().getRealPath(“upload”);// 上传路径
ext = fileFileName.substring(fileFileName.lastIndexOf(“.”)).toLowerCase();// 取得后缀
System.out.println(“取得后缀 ” + ext);
File dstFile = new File(uploadPath, fileFileName);
copy(srcfile, dstFile);
} catch (Exception e) {
System.out.println(e);
}
return “success”;

}
/**************/

public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}

public String getFileFileName() {
return fileFileName;
}

public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
}

2、上传表单页面(就是我们平时普通的表单页面,样式有点乱。不理它了。嘻嘻~~~)

Java代码
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.
7.
8.
9.
10. 16.

17.
18.

19.

20.

21.

41.

42.

43.


46.

47.

文件上传
22.
23.

24.

25.

26.

39.

40.

 
27.
28.
29.

30.

31.

32.

33.

34.

35.

36.

文件名:
文件路径:

37.

38.

44.
45.

48.

49.
50.
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>




文件上传
 
文件名:
文件路径:



3、上传进度条页面(注意,这个是用一个iframe的源文件链接实现的)

Java代码
1.<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2.<%
3. String path = request.getContextPath();
4. String basePath = request.getScheme() + "://"
5. + request.getServerName() + ":" + request.getServerPort()
6. + path + "/";
7.%>
8.
9.
10.
11.
12. 14.
15.

26.
37.
38.
39.
40.

41.
42. enctype="multipart/form-data">
43.
44.
45.

46.

47.

53.
54.
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>





enctype="multipart/form-data">



4、上传成功后的页面(就是一个简单的页面)

Java代码
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.
7.
8.
9.
10. 12. 21.

22.
23.
24. 上传成功
25.
26.
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>






上传成功

5、Struts.xml 配置文件

Java代码
1. 2."-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
3."http://struts.apache.org/dtds/struts-2.0.dtd">
4.
5.
6.
7. 8.
9.
10. /filejd.jsp
11.

12.
13.

14.
15.

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">



/filejd.jsp


接着:

我们在本机调试上传的时候,网页可能会有所卡顿,这个没关系,我们绑到服务器上就正常了,主要是浏览器处理文件的时候硬件性能问题。

总的来说,这个上传还是比较简单的(至少我这样认为~_~)

最后附上源码:欢迎大家多多交流,小弟这里先谢过啦~~~嘻嘻~~

SSH2Upload.rar (5.1 MB)
下载次数: 162
http://yzxqml.iteye.com/blog/1689590