2010-12-12 4 views
5

얘들 아,이 정보는 웹에서 분명히 찾을 수 없다. 나는 행동을 가지고 텍스트 파일을 생성하지만 항상 클라이언트에게 "generatePDF.action"파일로 나타납니다. 내가 그것을 receipt.txt 파일로 보여주고 싶습니다. 여기 Struts2에서 StreamResult의 출력 이름을 어떻게 정의 할 수 있습니까?

내 주석입니다 :

@Action(value = "/generateTXT", 
    results = { 
     @Result(name = "ok", type = "stream", 
     params = {"inputName", "inputStream", 
        "contentType", "application/octet-stream", 
        "contentDispostion", "attachment;filename=receipt.txt"}) 
    }) 
+0

여러 브라우저에서 일관되게 발생합니까? 아니면 특정 브라우저/버전과 격리되어 있습니까? –

+0

불행히도 Firefox 3.6 및 Chrome 8에서 발생합니다. –

답변

5

당신이 "플러그인 다음 다음에 해결"/ YourApplicationContext/스트림/스트림 테스트 "에서 참조 실행을 위해 다음 코드를 사용 할 수있는 규칙을 사용하는 경우 /YourApplicationContext/stream/document.txt이 :의 ContentDisposition "

package struts2.stream; 

import com.opensymphony.xwork2.ActionSupport; 
import java.io.InputStream; 
import java.io.StringBufferInputStream; 
import org.apache.struts2.convention.annotation.Result; 


@Result(name = ActionSupport.SUCCESS, type = "stream", params = 
{ 
    "contentType", 
    "text/hmtl", 
    "inputName", 
    "inputStream", 
    "contentDisposition", 
    "filename=document.txt" 
}) 
public class StreamTestAction extends ActionSupport{ 
    public InputStream inputStream; 

    @Override 
    public String execute(){ 
    inputStream = new StringBufferInputStream("Hello World! This is a text string response from a Struts 2 Action.");  
    return SUCCESS; 
    } 
} 

에주의하시기 바랍니다" "그 값이 설정되어있는 것을에"파일 이름 = 'document.txt' " 'document.txt을 변화하는 당신을 얻는 것 네가 원해. 원래 주석이 괜찮

+0

참고 Struts2에는 가져 오기/설정이 필요하지 않습니다. 필자는 데모를 위해 소스를 불필요하게 길게 만듭니다. – Quaternion

+0

또한 파일 이름을 설정하기 위해 contentDisposition을 생략하면 파일 이름은 contentType = "text/html"의 경우에는 'document'라는 작업의 콘텐츠 유형과 함께 추가 된 작업의 이름이됩니다. document.html, contentType = "text"then document.txt) – Quaternion

+0

해답을 주셔서 감사합니다.하지만 저는 여전히 작은 문제가 있습니다. 파일이 브라우저에서 렌더링됩니다 (chrome 및 firefox에서 테스트 됨). 나는 donwload 윈도우를 보여주고 싶었습니다. –

0

, 그것은 단지 오타가 포함되어

"contentDispostion"나는이 문제를 알아 내기 위해 나에게 나이를했다 "의 ContentDisposition"

을 읽어야한다, 그래서 나는 그것을 명확하게 거라고 생각 :-)

0

내 주석은 기본적으로 동일하지만 파일의 이름을 설정 참조를 사용 :

@Result(name="export", type="stream", 
    params={ "contentType", "application/octet-stream", 
    "inputName", "fileInputStream", 
    "contentDisposition", "attachment;filename=%{exportFilename}", 
    "bufferSize", "4096"}) 

exportFilename는 GE와 문자열 변수입니다 tter와 setter를 가지며 상속 가능한 클래스에 넣을 수 있으므로 고유 한 ExportAction을 만들고 모든 액션을 확장 할 수 있습니다.

아마도 변수를 만들어 모든 매개 변수의 값을 설정할 수 있습니다.

관련 문제