大夢想家

          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 阿南 閱讀(1781) 評論(0)  編輯  收藏 所屬分類: Eclipse-RAP

          主站蜘蛛池模板: 宁晋县| 漳平市| 浏阳市| 邵阳市| 舟曲县| 富裕县| 满洲里市| 鄂托克旗| 都兰县| 特克斯县| 西充县| 武宣县| 耒阳市| 赤峰市| 中山市| 丽水市| 新平| 枞阳县| 屏东县| 奉新县| 肥乡县| 南昌市| 毕节市| 共和县| 同江市| 赣州市| 金昌市| 甘泉县| 积石山| 托克逊县| 同江市| 大庆市| 和龙市| 绵竹市| 江陵县| 宝丰县| 屏南县| 康保县| 九寨沟县| 正阳县| 新余市|