画面に何かのボタンのイベントに呼び出して、ファイルをダウンロードする。
DownloadIFrame.java
package gwtsample.client;
import com.google.gwt.event.dom.client.HasLoadHandlers;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.RootPanel;
public class DownloadIFrame extends Frame implements LoadHandler, HasLoadHandlers
{
public static final String DOWNLOAD_FRAME = "__gwt_downloadFrame";
public DownloadIFrame(String url)
{
super();
setSize("0px", "0px");
setVisible(false);
RootPanel rp = RootPanel.get(DOWNLOAD_FRAME);
if (rp != null)
{
addLoadHandler(this);
rp.add(this);
setUrl(url);
}
else
openURLInNewWindow(url);
}
native void openURLInNewWindow(String url) /*-{
$wnd.open(url);
}-*/;
public HandlerRegistration addLoadHandler(LoadHandler handler)
{
return addHandler(handler, LoadEvent.getType());
}
public void onLoad(LoadEvent event)
{}
}
ServletUtils.java
package gwtsample.server;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletUtils
{
/**
* Sends headers to allow a file to be download to the workstation vs.
* opened in the browser.
*
* @param fileName
* the name of the file. This will be encoded as UTF-8.
* @param contentLen
* the length of the file, zero (0) if unknown.
* @param request
* the {@code HttpServletRequest}.
* @param response
* the {@code HttpSerletResponse}.
* @throws UnsupportedEncodingException
*/
public static void sendHeaders(String fileName, int contentLen, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException
{
String ua = request.getHeader("User-Agent").toLowerCase();
boolean isIE = ((ua.indexOf("msie 6.0") != -1) || (ua.indexOf("msie 7.0") != -1)) ? true : false;
String encName = URLEncoder.encode(fileName, "UTF-8");
// Derived from Squirrel Mail and from
// http://www.jspwiki.org/wiki/BugSSLAndIENoCacheBug
if (request.isSecure())
{
response.addHeader("Pragma", "no-cache");
response.addHeader("Expires", "-1");
response.addHeader("Cache-Control", "no-cache");
}
else
{
response.addHeader("Cache-Control", "private");
response.addHeader("Pragma", "public");
}
if (isIE)
{
response.addHeader("Content-Disposition", "attachment;filename=\"" + encName + "\"");
response.addHeader("Connection", "close");
response.setContentType("application/force-download;name=\"" + encName + "\"");
}
else
{
response.addHeader("Content-Disposition", "attachment;filename=\"" + encName + "\"");
response.setContentType("application/octet-stream;name=\"" + encName + "\"");
if (contentLen > 0)
response.setContentLength(contentLen);
}
}
}
web.xml
<servlet>
<servlet-name>CopyObjectServlet</servlet-name>
<servlet-class>gwtsample.server.CopyObjectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CopyObjectServlet</servlet-name>
<url-pattern>/gwtsample/copyObjectServlet</url-pattern>
</servlet-mapping>
ボタンのイベント処理:
sendButton.addClickHandler(new ClickHandler()
{
@Override
public void onClick(ClickEvent event)
{
String copyURL = GWT.getModuleBaseURL() +
"copyObjectServlet?param0=";
new DownloadIFrame(copyURL);
}
});
htmlファイル:
<div id="__gwt_downloadFrame"></div>
0 件のコメント:
コメントを投稿