http://www.uploadify.com/download/
二、将压缩包解压,复制
uploadify.css
uploadify.swf
swfobjects.js
jquery.uploadify.v2.1.0.min.js
cancel.png
jquery-1.3.2.min.js
default.css到项目中。
三、在项目中添加三个jar包
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
commons-io.jar
四、在需要使用上传插件的页页头部导入
Html代码
1.
2.
3.
4.
5.
6.
7.
8.
五、编写js文件
Js代码
1.$(document).ready(function(){
2. //A文件上传
3. $("#uploadFile").uploadify({
4. 'uploader' : 'images/uploadify.swf',//指定上传控件的主体文件,默认‘uploader.swf’
5. 'script' : 'UploadServlet', //指定服务器端上传处理文件
6. 'scriptData' : {'uploadFile':$('#uploadFile').val()},
7. 'cancelImg' : 'images/cancel.png',
8. 'fileDataName' : 'uploadFile',
9. 'fileDesc' : 'jpg文件或jpeg文件或gif文件', //出现在上传对话框中的文件类型描述
10. 'fileExt' : '*.jpg;*.jpeg;*.gif', //控制可上传文件的扩展名,启用本项时需同时声明fileDesc
11. 'sizeLimit' : 512000, //控制上传文件的大小,单位byte
12. 'folder' : '/uploadImages',
13. 'queueID' : 'fileQueueA',
14. 'auto' : false,
15. 'multi' : true
16. });
17.});
18.
六、html代码
Html代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
21.
22.
23.
24.
七、servlet代码
Java代码
1.package com.aptech.servlet;
2.
3.import java.io.File;
4.import java.io.IOException;
5.import java.util.Iterator;
6.import java.util.List;
7.import javax.servlet.ServletConfig;
8.import javax.servlet.ServletException;
9.import javax.servlet.http.HttpServlet;
10.import javax.servlet.http.HttpServletRequest;
11.import javax.servlet.http.HttpServletResponse;
12.import org.apache.commons.fileupload.FileItem;
13.import org.apache.commons.fileupload.FileUploadException;
14.import org.apache.commons.fileupload.disk.DiskFileItemFactory;
15.import org.apache.commons.fileupload.servlet.ServletFileUpload;
16.import com.aptech.util.FileUploadUtil;
17.
18./**
19. * Servlet implementation class UploadServlet
20. */
21.public class UploadServlet extends HttpServlet {
22. private static final long serialVersionUID = 1L;
23. private static String path;
24. /**
25. * Default constructor.
26. */
27. public UploadServlet() {
28. // TODO Auto-generated constructor stub
29. }
30.
31. /**
32. * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
33. */
34. @Override
35. protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
36.
37. // 生成存放文件的路径
38. String savePath = path + "/";
39.
40. File f1 = new File(savePath);
41.
42. if (!f1.exists()) {
43. f1.mkdirs();
44. }
45.
46. DiskFileItemFactory fac = new DiskFileItemFactory();
47.
48. ServletFileUpload upload = new ServletFileUpload(fac);
49.
50. upload.setHeaderEncoding("utf-8");
51.
52. List fileList = null;
53. try {
54. fileList = upload.parseRequest(request);
55. } catch (FileUploadException ex) {
56. return;
57. }
58. Iterator
59. String name = "";
60.
61. String extName = "";
62.
63. while (it.hasNext()) {
64.
65. FileItem item = it.next();
66.
67. if (!item.isFormField()) {
68.
69. name = item.getName();
70.
71. // 新的文件名
72. String newUploadFileName = FileUploadUtil.getlnstance().getNewFileName(name);
73.
74. // // 扩展名格式:
75. //
76. // if (name.lastIndexOf(".") >= 0) {
77. //
78. // extName = name.substring(name.lastIndexOf("."));
79. //
80. // }
81.
82. File saveFile = new File(savePath + newUploadFileName);
83. try {
84.
85. item.write(saveFile);
86.
87. } catch (Exception e) {
88.
89. e.printStackTrace();
90.
91. }
92.
93. }
94.
95. }
96. }
97.
98. @Override
99. public void init(ServletConfig config) throws ServletException {
100. path = config.getServletContext().getRealPath("/uploadImages");
101. }
102.
103.
104.}
八、配置servlet
Xml代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.