0

我正在尝试在 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

4 回答 4

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

于 2013-02-27T01:42:10.327 回答
0

你没有添加你的servlet路径,添加 multiUploader.setServletPath("your_servlet_path"); 另外,通过添加 multiuploader.setAutoSubmit(true) 提交上传

于 2014-06-04T13:35:10.407 回答
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);
  }

按照这个链接 文件上传

于 2013-02-27T04:47:39.707 回答
0

只需添加您的主机路径

字符串页面=GWT.getModuleBaseURL()+"上传"; 和你的

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

于 2014-04-05T17:41:59.383 回答