GWT Java - 文件上传 - 需要每个文件的名称

时间:2016-11-17 08:50:49

标签: java file gwt file-upload

我正在将文件从客户端上传到服务器端,以便作为附件发送到电子邮件中。当我将文件保存为临时文件时,名称被更改(例如,从leader_uniform.pdf更改为upload-6781801187205293221.bin)。所以我还需要发送文件的原始名称,以便在发送电子邮件之前将临时文件名更改回原始名称。但是,当我选择多个文件时,会连接临时文件名,而只返回第一个文件名。

代码客户端是:

// Load the document
private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
    public void onFinish(IUploader uploader) {
        if (uploader.getStatus() == Status.SUCCESS) {

            //The server sends useful information to the client by default
            UploadedInfo info = uploader.getServerInfo();           

            // You can send any customised message and parse it
            //Store path to image;
            imagePath = info.message; //Concatenated temporary file path and name ("upload-", ".bin") returned
            Window.alert("imagePath = " + imagePath);

            if (info.name != null) {
                fileName.setText(info.name);
                lblAttached.setText("Attached");
                Window.alert("File name " + info.name); //Only first file name returned
                Window.alert("File content-type " + info.ctype);
                Window.alert("File size " + info.size);
            }

        }
    }
};

代码服务器端是:

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 = "";
    @SuppressWarnings("unused")
    int cont = 0;
    for (FileItem item : sessionFiles) {
      if (false == item.isFormField()) {
        cont ++;
        try {

          /// 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 customised message to the client.
          response += file.getAbsolutePath();

        } catch (Exception e) {
          throw new UploadActionException(e);
        }
      }
    }

1 个答案:

答案 0 :(得分:0)

我用这段代码解决了它:

// Load the document
private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
    public void onFinish(IUploader uploader) {
        if (uploader.getStatus() == Status.SUCCESS) {

            //The server sends useful information to the client by default
            UploadedInfo info = uploader.getServerInfo();           

            // You can send any customised message and parse it
            //Store path to image;
            imagePath = info.message; //Concatenated temporary file path and name ("upload-", ".bin") returned

            String[] parts = imagePath.split(".bin");

            if (parts.length > 1){
                Window.alert("Please select one file at a time.");
            }else{
                eachImagePath.add(imagePath);
                eachImageName.add(info.name);

                //Display a list of Attachments and allow them to be deleted.
                displayAttachmentName();

                pos++;
            }

            if (info.name != null) {
                fileName.setText(info.name);
            }
        }
    }
};