📄 filedownloadservlet.java
字号:
/*
* Copyright (c) 2004, 2005 Polarion Software, All rights reserved.
* Email: community@polarion.org
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. Copy of the License is
* located in the file LICENSE.txt in the project distribution. You may also
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
*
* POLARION SOFTWARE MAKES NO REPRESENTATIONS OR WARRANTIES
* ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESSED OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. POLARION SOFTWARE
* SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT
* OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
package org.polarion.svnwebclient.web.servlet;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.polarion.svnwebclient.SVNWebClientException;
import org.polarion.svnwebclient.configuration.ConfigurationProvider;
import org.polarion.svnwebclient.data.IDataProvider;
import org.polarion.svnwebclient.data.model.DataFileElement;
import org.polarion.svnwebclient.web.support.RequestHandler;
/**
*
* @author <A HREF="mailto:svnbrowser@polarion.org">Polarion Software </A>
*/
public class FileDownloadServlet extends AbstractServlet {
private static final long serialVersionUID = -5061766135434675105L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.execute(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.execute(request, response);
}
protected void executeSVNOperation(IDataProvider dataProvider) throws SVNWebClientException {
OutputStream outStream = null;
try {
long revision = dataProvider.getHeadRevision();
if (this.requestHandler.getCurrentRevision() != -1) {
revision = this.requestHandler.getCurrentRevision();
}
String url = this.requestHandler.getUrl();
if (this.requestHandler.getPegRevision() != -1) {
url = dataProvider.getLocation(this.requestHandler.getUrl(), this.requestHandler.getPegRevision(), revision);
}
DataFileElement fileElement = dataProvider.getFile(url, revision);
String fileName = fileElement.getName();
String defaultEncoding = ConfigurationProvider.getInstance().getDefaultEncoding();
fileName = new String(fileName.getBytes(defaultEncoding),"ISO-8859-1");
String mimeType = this.getServletContext().getMimeType(fileName.toLowerCase());
if (mimeType == null) {
mimeType = "application/Octet-stream";
}
this.state.getResponse().setContentType(mimeType);
if(this.requestHandler.isAttachment()) {
this.state.getResponse().setHeader("Content-Disposition", "attachment; filename=" + fileName);
} else {
this.state.getResponse().setHeader("Content-Disposition", "inline; filename=" + fileName);
}
outStream = this.state.getResponse().getOutputStream();
byte[] content = fileElement.getContent();
outStream.write(content, 0, content.length);
outStream.flush();
} catch (SVNWebClientException ex) {
try {
this.state.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND);
} catch (Exception e) {
throw new SVNWebClientException(e);
}
} catch (Exception e) {
throw new SVNWebClientException(e);
} finally {
if (outStream != null) {
try {
outStream.close();
} catch (Exception e) {
Logger.getInstance(FileDownloadServlet.class).error(e, e);
}
}
}
}
protected RequestHandler getRequestHandler(HttpServletRequest request) {
return new RequestHandler(request);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -