swfupload 的文件上传程序 java 上传方式 !

上段时间看了很多的关于swfupload 的资料!很多人写的让我没有办法很简单的运用 swfupload ,对于这个swfupload 很多人都说好,但是具体的例子很少,有空我就弄了一下,真的很简单!

swfupload 的文件上传程序 java 上传方式 !

最主要的就是javascript 在页面中如何调用 servlet

<script type="text/javascript">
		var swfu;

		window.onload = function() {
			var settings = {
				flash_url : "<%=imgpath %>/swfupload/swfupload.swf",
				upload_url: "<%=imgpath %>/servlet/UploadServlet",
				file_size_limit : "1000 MB",
				file_types : "*.*",
				file_types_description : "All Files",
				file_upload_limit :2,
				file_queue_limit : 0,
				custom_settings : {
					progressTarget : "fsUploadProgress",
					cancelButtonId : "btnCancel"
				},
				debug: false,

				// Button settings
				button_image_url: "<%=imgpath %>/swfupload/images/TestImageNoText_65x29.png",
				button_width: "65",
				button_height: "29",
				button_placeholder_id: "spanButtonPlaceHolder",
				button_text: '<span class="theFont">上传</span>',
				button_text_style: ".theFont { font-size: 16; }",
				button_text_left_padding: 12,
				button_text_top_padding: 3,
				
				// The event handler functions are defined in handlers.js
				file_queued_handler : fileQueued,
				file_queue_error_handler : fileQueueError,
				file_dialog_complete_handler : fileDialogComplete,
				upload_start_handler : uploadStart,
				upload_progress_handler : uploadProgress,
				upload_error_handler : uploadError,
				upload_success_handler : uploadSuccess,
				upload_complete_handler : uploadComplete,
				queue_complete_handler : queueComplete	// Queue plugin event
			};

			swfu = new SWFUpload(settings);
			
	     };
</script>


主要的servlet 的上传文件代码!
public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("GBK");
		FileItemFactory factory = new DefaultFileItemFactory();
		FileUpload upload = new FileUpload(factory);
		Iterator items;
		try {
			items = upload.parseRequest(request).iterator();
			while (items.hasNext()) {
				FileItem item = (FileItem) items.next();
				if (!item.isFormField()) {
					String name = item.getName();
					String filetype = name.substring(name.lastIndexOf("."));
					String filename = System.currentTimeMillis()+filetype;
//					System.out.println(filename+"--"+new String(name.getBytes("GBK"),"UTF-8"));
					String realpath = request.getRealPath("/upload");
					String path = realpath + File.separatorChar + filename;
					File file = new File(path);
					if (!file.exists()) {
						File file1 = new File(realpath);
						file1.mkdirs();
						file.createNewFile();
					}else{
						
						file.createNewFile();
					}
					item.write(file);
					response.setContentType("text/html");
					response.setCharacterEncoding("GBK");
					PrintWriter out = response.getWriter();
					out.print("<font size='2'>上传的文件为:" + name + "<br>");
					out.print("保存的地址为:" + path + "</font>");
					
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


好了,不多介绍了!需要的话直接下载我的 项目吧!

你可能感兴趣的:(JavaScript,java,html,servlet,Flash)