2016-09-01 14 views
-1

ヘルプが必要です。エラー:JavaではIllegalStateExceptionが発生します。応答がコミットされた後に転送できません

私はJava Servletでファイルをダウンロードしようとしていますが、ファイルのダウンロード後にページへのリクエストを送信できません。

ファイルが正常にダウンロードされ、ページにリクエストを転送しようとするとillegalStateExceptionが発生します。ここ

は、私は、サーブレットからこのメソッドを呼び出したコード

public void fileDownload(String stringFileToDownload, HttpServletResponse response) throws Exception{ 
    FileInputStream inStream = null; 
    OutputStream outStream = null; 
     try{    
      File downloadFile = new File(stringFileToDownload); //Reads input file 
      inStream = new FileInputStream(downloadFile); 
      response.setContentType("application/zip-compressed"); //MIME type of the file 
      response.setContentLength((int) downloadFile.length()); 
      response.setHeader("Content-Disposition", "attachment; filename=Time.zip"); 
      //response's output stream 
      outStream = response.getOutputStream(); 
      byte[] buffer = new byte[4096]; 
      int bytesRead = -1; 
      while ((bytesRead = inStream.read(buffer)) != -1) { 
       outStream.write(buffer, 0, bytesRead); 
      } 
     } 
     catch(Exception ex){ 
      throw ex; 
     } 
     finally{ 
      //response.flushBuffer(); 
      try{ 
       if(inStream != null){ 
        inStream.close(); 
       } 
       if(outStream != null){ 
        //outStream.flush(); 
        outStream.close(); 
       } 
      } 
      catch(Exception ex){ 
       throw ex; 
      } 
     } 
    } 

です。あなたがコミットされる出力ストリーム応答(outStream.write(buffer, 0, bytesRead);)に書き込むときに)request.forwardを(使用しようとすると、それがする。ここで

FileDownloadFromWeb fileDownloadFromWeb = new FileDownloadFromWeb(); 
fileDownloadFromWeb.fileDownload(stringarchiveFile, response); //Allow to download 
Request Dispatcher objRequestDispatcher = request.getRequestDispatcher(objProperties.getProperty("SUCC‌​ESS_DOWNLOAD")); 
objRequestDispatcher.forward(request, response); 
+0

呼び出しコードを表示してください。 –

+1

メッセージはリダイレクトしようとしましたが、前に出力を書きましたが、許可されていません。 –

+0

は書き込みアクティビティを実行していません。ここに呼び出しコードがあります。 'FileDownloadFromWeb fileDownloadFromWeb = new FileDownloadFromWeb(); fileDownloadFromWeb.fileDownload(stringarchiveFile、response); //ダウンロードを許可 //要求ディスパッチャ objRequestDispatcher = request.getRequestDispatcher(objProperties.getProperty( "SUCCESS_DOWNLOAD")); objRequestDispatcher.forward(リクエスト、レスポンス); ' – Bhat

答えて

1

:コードを呼び出す

サーブレットから別のページにリダイレクトします反抗的にスローエラー。この問題への

ソリューションは、リクエストパラメータにオブジェクトとしてファイルの内容を設定し、JSPでそれを使用するよりも、またはリダイレクトを使用することではなく、前方より

おそらくあなたの問題は、ファイルのダウンロードがこれを参照するために別のサーブレットを取ることによって固定することができます。 after download a image ,i want to redirect on another page, but not able to

+0

ありがとう:)私はこの解決策をチェックします! – Bhat

+0

ありがとう@ジェキン。私は、ファイルをダウンロードしている間にサーブレットコールを避け、別のJSP自体に関数を置き、ボタンイベントを通して呼び出しました。 – Bhat

+0

この[リンク](http://www.javatpoint.com/downloading-file-from-the-server-in-jsp)に続き、**アクション** – Bhat

関連する問題