Tapestry 5 프레임 워 크 에서 파일 다운로드 기능 구현
주:(기본적으로 Tapestry 5 는 text/html 를 출력 합 니 다.text/xml 를 출력 하려 면 페이지 클래스 설명 전에 주석 을 추가 해 야 합 니 다.@Meta("tapestry.response-content-type=text/xml")
다음은 Tapestry 5 파일 흐름 요청/응답 코드 예제 입 니 다.아래 클래스 만 컴 파일 하면 TML 파일 이 필요 없습니다.
This example is very similar to the examples on Streaming a Dynamic PDF or a Dynamic chart. Except in this case the file already exists somewhere. I'll also show some utility classes I made up to assist in the process.
ImagePage.java
This is the page class in your Tapestry 5 application
import java.io.InputStream;
import com.mycompany.myapp.wui.tapestry.services.util.JPEGAttachment;
import org.apache.tapestry.StreamResponse;
public class ImagePage {
public StreamResponse onSubmit() {
//Note that you could provide an absolute path here, like H:\\LOLCATZ.MP3
InputStream input = ImagePage.class.getResourceAsStream("HelloKitty.jpg");
return new JPEGAttachment(input);
}
}
JPEGAttachment.java
import java.io.InputStream;
public class JPEGAttachment extends AttachmentStreamResponse {
public JPEGAttachment(InputStream is, String args) {
super(is, args);
this.contentType = "image/jpeg";
this.extension = "jpg";
}
public JPEGAttachment(InputStream is) {
super(is);
this.contentType = "image/jpeg";
this.extension = "jpg";
}
}
AttachmentStreamResponse.java
This is a util class for Attachments. The goal here is to have a "Save As" dialog pop up on the client browser.
import java.io.IOException;
import java.io.InputStream;
import org.apache.tapestry.StreamResponse;
import org.apache.tapestry.services.Response;
public class AttachmentStreamResponse implements StreamResponse {
private InputStream is = null;
/**
* This can be changed to something obscure, so that it is more likely to trigger a "Save As" dialog, although there
* is no guarantee.
*
* ex: application/x-download
*
* See http://www.onjava.com/pub/a/onjava/excerpt/jebp_3/index3.html
*/
protected String contentType = "text/plain";
protected String extension = "txt";
protected String filename = "default";
public AttachmentStreamResponse(InputStream is, String filenameIn) {
this.is = is;
if (filenameIn != null) {
this.filename = filenameIn;
}
}
public AttachmentStreamResponse(InputStream is) {
this.is = is;
}
public String getContentType() {
return contentType;
}
public InputStream getStream() throws IOException {
return is;
}
public void prepareResponse(Response arg0) {
arg0.setHeader("Content-Disposition", "attachment; filename=" + filename + ((extension == null) ? "" : ("." + extension)));
arg0.setHeader("Expires", "0");
arg0.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
arg0.setHeader("Pragma", "public");
//We can't set the length here because we only have an Input Stream at this point. (Although we'd like to.)
//We can only get size from an output stream. arg0.setContentLength(.length);
}
}
InlineStreamResponse.java
This file is not used in the example, but is for files you intend to display inline in the browser (No "Save As" dialog box):
import java.io.IOException;
import java.io.InputStream;
import org.apache.tapestry.StreamResponse;
import org.apache.tapestry.services.Response;
public class InlineStreamResponse implements StreamResponse {
private InputStream is = null;
protected String contentType = "text/plain";// this is the default
protected String extension = "txt";
protected String filename = "default";
public InlineStreamResponse(InputStream is, String... args) {
this.is = is;
if (args != null) {
this.filename = args[0];
}
}
public String getContentType() {
return contentType;
}
public InputStream getStream() throws IOException {
return is;
}
public void prepareResponse(Response arg0) {
arg0.setHeader("Content-Disposition", "inline; filename=" + filename
+ ((extension == null) ? "" : ("." + extension)));
}
}
Other file types
Now, all you have to do for other file types is create a class like this:
For a JPG inline
import java.io.InputStream;
public class JPEGInline extends InlineStreamResponse {
public JPEGInline(InputStream is, String... args) {
super(is, args);
this.contentType = "image/jpeg";
this.extension = "jpg";
}
}
For an XLS attachment
public class XLSAttachment extends AttachmentStreamResponse {
public XLSAttachment(InputStream is, String args) {
super(is, args);
this.contentType = "application/vnd.ms-excel";
this.extension = "xls";
}
}
For a PDF attachement
import java.io.InputStream;
public class PDFAttachment extends AttachmentStreamResponse {
public PDFAttachment(InputStream is, String args) {
super(is, args);
this.contentType = "application/pdf";
this.extension = "pdf";
}
public PDFAttachment(InputStream is) {
super(is);
this.contentType = "application/pdf";
this.extension = "pdf";
}
}
Calling the event handler from a page template
The event handler can be called using an actionLink e.g.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.