一段来自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下不能使用;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值