FileServlet ---download file


package org.jboss.seam.wiki.core.ui;

import org.jboss.seam.wiki.core.dao.WikiNodeDAO;
import org.jboss.seam.wiki.core.model.WikiUpload;
import org.jboss.seam.wiki.core.model.WikiUploadImage;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.servlet.ContextualHttpServletRequest;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileServlet extends HttpServlet {

private static final Log log = LogFactory.getLog(FileServlet.class);

private static final String DOWNLOAD_PATH = "/download.seam";

private byte[] fileNotFoundImage;

public FileServlet() {
InputStream in = getClass().getResourceAsStream("/img/filenotfound.png");
if (in != null) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[512];
try {
int read = in.read(buffer);
while (read != -1) {
out.write(buffer, 0, read);
read = in.read(buffer);
}

fileNotFoundImage = out.toByteArray();
}
catch (IOException e) {
}
}

}

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
new ContextualHttpServletRequest(request) {
@Override
public void process() throws Exception {
doWork(request, response);
}
}.run();
}

// TODO: All data access in this method runs with auto-commit mode, see http://jira.jboss.com/jira/browse/JBSEAM-957
protected void doWork(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

if (DOWNLOAD_PATH.equals(request.getPathInfo())) {

String id = request.getParameter("fileId");

Contexts.getSessionContext().set("LAST_ACCESS_ACTION", "File: " + id);

WikiUpload file = null;

if (!"".equals(id)) {

Long fileId = null;
try {
fileId = Long.valueOf(id);
} catch (NumberFormatException ex) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "File" + id);
}

WikiNodeDAO wikiNodeDAO = (WikiNodeDAO)org.jboss.seam.Component.getInstance(WikiNodeDAO.class);
file = wikiNodeDAO.findWikiUpload(fileId);
}

String contentType = null;
byte[] data = null;

String thumbnail = request.getParameter("thumbnail");

if (file != null
&& thumbnail != null
&& Boolean.valueOf(thumbnail)
&& file.isInstance(WikiUploadImage.class)
&& ((WikiUploadImage)file).getThumbnailData() != null
&& ((WikiUploadImage)file).getThumbnailData().length >0) {

// Render thumbnail picture
contentType = file.getContentType();
data = ((WikiUploadImage)file).getThumbnailData();

} else if (file != null && file.getData() != null && file.getData().length > 0) {

// Render file regularly
contentType = file.getContentType();
data = file.getData();

} else if (fileNotFoundImage != null) {

contentType = "image/png";
data = fileNotFoundImage;

}

if (data != null) {
response.setContentType(contentType);
response.setContentLength(data.length);
// If it's not a picture or if it's a picture that is an attachment, tell the browser to download
// the file instead of displaying it
// TODO: What about PDFs? Lot's of people want to show PDFs inline...
if ( file != null &&
( !file.isInstance(WikiUploadImage.class) || file.isAttachedToDocuments() )
) {
response.setHeader("Content-Disposition", "attachement; filename=\"" + file.getFilename() + "\"" );
}
response.getOutputStream().write(data);
}

response.getOutputStream().flush();
}

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值