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

📄 uploadshowaction.java

📁 jive论坛使得不时地好的不好的jive论坛使得不时地好的不好的
💻 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.IOException;
import java.io.OutputStream;

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

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.manager.throttle.ThrottleManagerIF;
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();

	public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		if (isAbusive(request)) {
			closeResponse(request, response);
			return null;
		}
		String dlname = request.getParameter(UploadFileFilter.DOWNLOAD_NAME);
		String id = request.getParameter("id");
		String oid = request.getParameter("oid");
		
		UploadFile uploadFile = null;
		try {
			if (UtilValidate.isEmpty(id))
				throw new Exception("parameter id is null");

			if (UtilValidate.isEmpty(oid))
				throw new Exception("parameter oid is null");

			//      see com.jdon.jivejdon.model.message.upload.MessageRenderingFile/MessageRenderingImage
			String type = request.getParameter("type");
			if (UtilValidate.isEmpty(type))
				type = "image/JPEG";
			
			UploadService uploadService = (UploadService) WebAppUtil.getService("uploadService", request);
			uploadFile = uploadService.getUploadFile(id,  false);
			if (uploadFile == null)
				throw new Exception("uploadFile == null");

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

			outUploadFile(response, uploadFile.getData(oid), type, dlname);
		} catch (Exception ex) {
			Debug.logError("get the image error:" + ex + " id=" + id + " oid=" + oid, module);
			ThrottleManagerIF throttleManager = (ThrottleManagerIF) WebAppUtil.getComponentInstance("throttleManager", request);
			throttleManager.processHitFilter(request.getRemoteAddr());
			closeResponse(request, response);
		}
		return null;
	}
	
	private void closeResponse(HttpServletRequest request, HttpServletResponse response) {
		try {
			disableSessionOnlines(request);
			if (!response.isCommitted())
				response.reset();
			HttpServletResponse httpResponse = (HttpServletResponse) response;
			httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
		} catch (IOException e) {
		}
	}

	private void disableSessionOnlines(HttpServletRequest httpRequest) {
		HttpSession session = httpRequest.getSession(false);
		if (session != null)
			session.invalidate();
	}

	protected boolean isAbusive(HttpServletRequest request) {
		ThrottleManagerIF throttleManager = (ThrottleManagerIF) WebAppUtil.getComponentInstance("throttleManager", request);
		if (throttleManager.isAbusive(request.getRemoteAddr())){		
			String userAgent = request.getHeader("User-Agent");
			String referrerUrl = request.getHeader("Referer");
			Debug.logError("it is spam : processing referrer for " + request.getRequestURI() + " referrerUrl=" + referrerUrl + " userAgent="
					+ userAgent + " ip=" + request.getRemoteAddr(), module);
			return true;
		} else {
			return false;
		}
	}
	
	protected void outUploadFile(HttpServletResponse response, byte[] data, String type, String dlname) throws Exception {
		if (data == null) throw new Exception("data is null ");
		response.setContentType(type);
		if (!UtilValidate.isEmpty(dlname)) {
			response.setHeader("Content-Disposition", "attachment; filename=\"" + dlname + "\"");
		}

		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 + -