如何使用gwtupload上传文件

时间:2013-02-26 11:29:06

标签: gwt

我正在尝试在GWT中上传文件,我现在被困在这2天了。

现在我正在尝试一些教程,所以这是我的代码

我的下面代码从未进入服务器端,任何想法......

或者如果有人可以为我提供一些工作代码..

这是我客户端的代码

   public class UploadDb extends Composite{

      private FlowPanel panelImages = new FlowPanel();


      public UploadDb() {
       initWidget(panelImages);

        // Create a new multiuploader and attach it to the document
        MultiUploader defaultUploader = new MultiUploader(FileInputType.LABEL);
        panelImages.add(defaultUploader);
        defaultUploader.setEnabled(true);

        // Add a finish handler which will load the image once the upload finishes
        defaultUploader.addOnFinishUploadHandler(onFinishUploaderHandler);
      }

      // Load the image in the document and in the case of success attach it to the viewer
      private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
        public void onFinish(IUploader uploader) {
          if (uploader.getStatus() == Status.SUCCESS) {

            new PreloadedImage(uploader.fileUrl(), showImage);
        System.out.println("Server message " + uploader.fileUrl());
          }
        }
      };

      // Attach an image to the pictures viewer
      private OnLoadPreloadedImageHandler showImage = new OnLoadPreloadedImageHandler() {
        public void onLoad(PreloadedImage image) {
          image.setWidth("75px");
          panelImages.add(image);
        }
      };


    }

在我的服务器端

               public class DashBoardUploadServlet  extends UploadAction {

      private static final long serialVersionUID = 1L;

      Hashtable<String, String> receivedContentTypes = new Hashtable<String, String>();
      /**
       * Maintain a list with received files and their content types. 
       */
      Hashtable<String, File> receivedFiles = new Hashtable<String, File>();

      /**
       * Override executeAction to save the received files in a custom place
       * and delete this items from session.  
       */
      @Override
      public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException {
        String response = "";
        int cont = 0;
        for (FileItem item : sessionFiles) {
          if (false == item.isFormField()) {
            cont ++;
            try {
              /// Create a new file based on the remote file name in the client
              // String saveName = item.getName().replaceAll("[\\\\/><\\|\\s\"'{}()\\[\\]]+", "_");
              // File file =new File("/tmp/" + saveName);

              /// Create a temporary file placed in /tmp (only works in unix)
              // File file = File.createTempFile("upload-", ".bin", new File("/tmp"));

              /// Create a temporary file placed in the default system temp folder
              File file = File.createTempFile("upload-", ".bin");
              item.write(file);

              /// Save a list with the received files
              receivedFiles.put(item.getFieldName(), file);
              receivedContentTypes.put(item.getFieldName(), item.getContentType());

              /// Send a customized message to the client.
              response += "File saved as " + file.getAbsolutePath();

            } catch (Exception e) {
              throw new UploadActionException(e.getMessage());
            }
          }}
      /// Remove files from session because we have a copy of them
        removeSessionFileItems(request);

        /// Send your customized message to the client.
        return response;
      }

的web.xml

                <servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>com.dashboard.server.DashBoardUploadServlet</servlet-class>
  </servlet>

  <servlet-mapping>
   <servlet-name>UploadServlet</servlet-name>
   <url-pattern>/Upload</url-pattern>
   </servlet-mapping>

4 个答案:

答案 0 :(得分:1)

servlet-mapping中的web.xml不正确。您必须将其更改为:

<servlet-mapping>
  <servlet-name>UploadServlet</servlet-name>
  <url-pattern>*.gupld</url-pattern>
</servlet-mapping>

如果您使用firebug或chrome dev-tools检查浏览器 - 服务器对话框,您应该看到404 HTTP errors

答案 1 :(得分:0)

在客户端尝试此代码。

  public void onModuleLoad() {

    final FormPanel form = new FormPanel();
    form.setAction("/myFormHandler");

    form.setEncoding(FormPanel.ENCODING_MULTIPART);
    form.setMethod(FormPanel.METHOD_POST);
    VerticalPanel panel = new VerticalPanel();
    form.setWidget(panel);

    FileUpload upload = new FileUpload();
    upload.setName("uploadFormElement");
    panel.add(upload);

    // Add a 'submit' button.
    panel.add(new Button("Submit", new ClickListener() {
      public void onClick(Widget sender) {
        form.submit();
      }
    }));




    RootPanel.get().add(form);
  }

请点击此链接 file upload

答案 2 :(得分:0)

只需添加主机路径

即可

String page = GWT.getModuleBaseURL()+“upload”;  和你的

servlet调用路径defaultUploader.setServletPath(页面); 它完美地工作.. 如果你还想在本地文件夹上传文件而不是在servlet上添加带有foldername的驱动器路径..

答案 3 :(得分:0)

你没有添加你的servlet路径,添加multiUploader.setServletPath(&#34; your_servlet_path&#34;); 另外,通过添加multiuploader.setAutoSubmit(true)

提交上传