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));
/** * 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
// 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); }
} catch (Exception e) { e.printStackTrace(); } return httpService; }
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下不能使用;