1

私のアップロード サーブレットは、置き換えようとしているファイル (コードの終わり近く) を (一見) ランダムに削除できなかったという例外をスローし続けます。ストリームを使用しておらず、ブラウザでファイルが開かれていないため、何が原因かわかりません。誰がこれを引き起こしているのか知っていますか? コードは私には正しいように見えるので、私はこれについて完全に無知です。DiskFileItem を使用したのはこれが初めてなので、そこで処理するニュアンスがあるかどうかはわかりません。

うまくいくこともあれば、うまくいかないこともあることを覚えておいてください。私はそれで迷っています。

問題領域:

File destination = new File(wellnessDir + File.separator + fileName + ".pdf");

  System.out.println("destination file exists: " + destination.exists());
  System.out.println("file to be moved exists: " + uploadedFile.exists());

  if(destination.exists()){
    boolean deleted = destination.delete();
    if(!deleted)
      throw new Exception("Could not delete file at " + destination);
  }        

私のシステムアウトは、ファイルと宛先の両方が存在すると常に言います。アップロードで既存のファイルを上書きしようとしています。

完全なコード: (& pastebin )

private void uploadRequestHandler(ServletFileUpload upload, HttpServletRequest request)
  {
    // Handle the request
    String fileName = "blank";
    try{         
      List items = upload.parseRequest(request);
      //Process the uploaded items
      Iterator iter = items.iterator();
      File uploadedFile = new File(getHome() + File.separator + "temp");
      if(uploadedFile.exists()){
        boolean tempDeleted = uploadedFile.delete();
        if(!tempDeleted)
          throw new Exception("Existing temp file could not be deleted.");
      }
      //write the file
      while (iter.hasNext()) {
        DiskFileItem item = (DiskFileItem) iter.next();
        if(item.isFormField()){
          String fieldName = item.getFieldName();
          String fieldValue = item.getString();
          if(fieldName.equals("fileName"))
            fileName = fieldValue;
            //other form values would need to be handled here, right now only need for fileName
        }else{
          item.write(uploadedFile);
        }
      }
      if(fileName.equals("blank"))
        throw new Exception("File name could not be parsed.");
      //move file
      File wellnessDir = new File(getHome() + File.separator + "medcottage" + File.separator + "wellness");
      File destination = new File(wellnessDir + File.separator + fileName + ".pdf");

      System.out.println("destination file exists: " + destination.exists());
      System.out.println("file to be moved exists: " + uploadedFile.exists());

      if(destination.exists()){
        boolean deleted = destination.delete();
        if(!deleted)
          throw new Exception("Could not delete file at " + destination);
      }        
      FileUtil.move(uploadedFile, new File(wellnessDir + File.separator + fileName + ".pdf"));
      writeResponse();
    } catch (Exception e) {
      System.out.println("Error handling upload request.");
      e.printStackTrace();
    }
  }

編集:追加するには、getHome()と「ホーム」は実際にはコードにはありません。これは、ホームパスを保護するためだけです

4

1 に答える 1

0

多くのテストと悪化の後、最終的に別のマシンで同じコードを試してみましたが、うまくいきました。私の仕事用マシンでドメインを転送することと関係があり、権限をいじっています。

于 2011-12-09T19:24:51.383 に答える