大梦想家

5年开发工程师,2年实施经理,X年售前顾问,......
数据加载中……
一段来自ews.eclipse.technology.rap的关于Upload a file的代码

I impleted my File upload/download system with rap internal Browser + servlet.
upload dialog:

public class FileUploadDialog extends TitleAreaDialog {
	private Browser browser;
	public FileUploadDialog(Shell parentShell) {
		super(parentShell);
	}
	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		container.setLayout(new FillLayout());
		container.setLayoutData(new GridData(GridData.FILL_BOTH));

browser = new Browser(container, SWT.NONE);
String url = "http://"+RWT.getRequest().getServerName()+":"+RWT.getRequest().getServerPort()+"/web/fileupload.jsp";
String html = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><html><head><title>upload file</title></head><body>"+
"<form action=\""+url+"\" enctype=\"MULTIPART/FORM-DATA\" method=\"post\"><br />File Name:</br><input type=\"file\""+
" name=\"filename\"/><br><input type=\"submit\" value=\"upload\"/></form></body></html> ";
browser.setText(html);
setMessage("select a local file");
return area;
}
	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(382, 280);
	}
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("upload file");
	}
}

file upload sevlet:
public class FileUploadServlet extends HttpServlet {
	private static final long serialVersionUID = -7245361228773015964L;
	private String uploadPath = "/upload/"; // server file repository
	private String tempPath = "/upload/tmp/"; //temp file folder

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
DiskFileUpload fu = new DiskFileUpload();
// max file size fu.setSizeMax(-1);
// buffer size
fu.setSizeThreshold(4096);
// temp path
fu.setRepositoryPath(tempPath);
			// get all uploa file
			List fileItems = fu.parseRequest(request);
			Iterator i = fileItems.iterator();
			while (i.hasNext()) {
				FileItem fi = (FileItem) i.next();
				// get the upload file name
				String fileName = fi.getName();
				String sep = System.getProperty("file.separator");
				int index = fileName.lastIndexOf(sep);
				if(index >-1){
					fileName = fileName.substring(fileName.lastIndexOf(sep));
				}
				fi.write(new File(uploadPath + fileName));
				response.getWriter().println(fileName+"upload success");
			}
		} catch (Exception e) {
		 //do something?
			e.printStackTrace();
		}
	}
	public void init() throws ServletException {
		if (!new File(uploadPath).isDirectory())
			new File(uploadPath).mkdirs();
		if (!new File(tempPath).isDirectory())
			new File(tempPath).mkdirs();
	}
}
registe servlet:

public class HttpServiceTracker extends ServiceTracker {
       public HttpServiceTracker(BundleContext context) {
           super(context, HttpService.class.getName(), null);
       }

public Object addingService(ServiceReference reference) {
final HttpService httpService = (HttpService) context
.getService(reference);
try {
//regist file upload servlet
HttpContext commonContext = new BundleEntryHttpContext(context
.getBundle(), fileFolder);
httpService.registerResources(fileContext, "/", commonContext);

Servlet adaptedJspServlet = new ContextPathServletAdaptor(
new FileUploadServlet(),
fileContext);
httpService.registerServlet(fileContext + "/fileupload.file",
adaptedJspServlet, null, commonContext);
           } catch (Exception e) {
               e.printStackTrace();
           }
           return httpService;
       }

public void removedService(ServiceReference reference, Object service) {
final HttpService httpService = (HttpService) service;
httpService.unregister(fileContext);
httpService.unregister(fileContext + "/fileupload.jsp");
super.removedService(reference, service);
}
}
start servlet:
  add these code to the bundle start up method.
 	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
       httpServiceTracker = new HttpServiceTracker(context);
       httpServiceTracker.open();
       System.out.println("IM resource servlet started...");
	}

 

这个实现是在RAP中使用Browser+servlet实现的文件上传功能;

其实在RAP的cvs上已经放出了org.eclipse.rwt.widgets.upload,专用于上传的组件,测试了一下,效果还不错,有进度条提示;不过在IE6下,有点难看了,在FF3下不能使用;



客户虐我千百遍,我待客户如初恋!

posted on 2008-01-11 13:17 阿南 阅读(1763) 评论(0)  编辑  收藏 所属分类: Eclipse-RAP


只有注册用户登录后才能发表评论。


网站导航: