⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadshowaction.java

📁 非常有影响的 j道 论 坛 源码 国外很有明的专家编写的 ....对java爱好者很有参考价值
💻 JAVA
字号:
/*
 * Copyright 2003-2006 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.jdon.jivejdon.presentation.action;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jdon.controller.WebAppUtil;
import com.jdon.jivejdon.model.UploadFile;
import com.jdon.jivejdon.model.message.upload.UploadFileFilter;
import com.jdon.jivejdon.service.UploadService;
import com.jdon.util.Debug;
import com.jdon.util.UtilValidate;

/**
 * @author banq(http://www.jdon.com)
 *
 */
public class UploadShowAction extends Action {
	public final static String module = UploadShowAction.class.getName();

	private static final byte[] BLANK = { 71, 73, 70, 56, 57, 97, 1, 0, 1, 0, -111, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, 0, 0, 0, 33, -7, 4, 1, 0,
			0, 2, 0, 44, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 2, 76, 1, 0, 59 };

	public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try {
			String imageId = request.getParameter("id");
			if (UtilValidate.isEmpty(imageId))
				throw new Exception("parameter id is null");

			//      see com.jdon.jivejdon.model.message.upload.MessageRenderingFile/MessageRenderingImage
			String type = request.getParameter("type");
			if (UtilValidate.isEmpty(type))
				type = "image/JPEG";

			boolean nocached = false;
			String nocachedS = request.getParameter("nocached");
			if (!UtilValidate.isEmpty(nocachedS))
				nocached = true;

			String dlname = request.getParameter(UploadFileFilter.DOWNLOAD_NAME);

			UploadFile uploadFile = null;

			UploadService uploadService = (UploadService) WebAppUtil.getService("uploadService", request);
			uploadFile = uploadService.getUploadFile(imageId, false);
			if (uploadFile == null)
				throw new Exception("uploadFile == null");

			if (!UtilValidate.isEmpty(uploadFile.getContentType()))
				type = uploadFile.getContentType();

			outUploadFile(response, uploadFile.getData(), type, dlname, nocached);
		} catch (Exception ex) {
			Debug.logError("get the image error:" + ex, module);
			outUploadFile(response, BLANK, "images/jpeg", null, false);
		}
		return null;
	}

	private void outUploadFile(HttpServletResponse response, byte[] data, String type, String dlname, boolean nocached) throws Exception {
		response.setContentType(type);
		if (!UtilValidate.isEmpty(dlname)) {
			response.setHeader("Content-Disposition", "attachment; filename=\"" + dlname + "\"");
		}
		if (nocached) {
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
		} else {
			long d = System.currentTimeMillis();
			response.addDateHeader("Last-Modified", d);
			response.addDateHeader("Expires", d+(5*24*60*60*1000)); 
		}
		OutputStream toClient = response.getOutputStream();
		try {
			toClient.write(data);
		} catch (Exception ex) {
			Debug.logError("get the image error:" + ex, module);
		} finally {
			toClient.close();
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -