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

📄 downloadimagefileservlet.java

📁 工厂版本管理系统,STRUTS2框架,用于管理商品的版本,便于有效的控制版本
💻 JAVA
字号:
package com.bluesky.elecall.web;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;

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

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import com.bluesky.common.domain.IReposity;
import com.bluesky.elecall.domain.CategoryItem;
import com.bluesky.elecall.domain.Product;
import com.bluesky.system.File;

public class DownloadImageFileServlet extends HttpServlet {
	private Logger log;

	public void init() {
		log = Logger.getLogger(this.getClass());

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		
		
		
		
		XmlWebApplicationContext webContext = (XmlWebApplicationContext) getServletContext()
				.getAttribute(
						"org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.");

		WebApplicationContext wac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());

		IReposity reposity = (IReposity) wac.getBean("reposity");

		
		//category_item, product
		String entity = request.getParameter("entity");
		
		//small, large
		String size = request.getParameter("size");
		
		//entity id
		String id = request.getParameter("id");
		
		
		Long imageId=null;
		if(entity.equals("product"))
		{
			Product p = (Product)reposity.get(Product.class, id);
			if(size.equals("small"))
				imageId = p.getSmallImageFileId();
			else
				imageId= p.getImageFileId();			
		}
		else if(entity.equals("category_item"))
		{
			CategoryItem ci = (CategoryItem)reposity.get(CategoryItem.class, Long.parseLong(id));
			imageId = ci.getImageFileId();
		}
				

		File fileEntity = null;

		byte[] imageData = null;

		if (imageId != null) {
			String imageType = request.getParameter("type");
			if (imageType == null)
				imageType = "";
			if (imageType.equals("")) {
				fileEntity = (File) reposity.get(File.class, imageId);

				imageData = fileEntity.getData();
			}
		}
		else
		{//return a default image
			String path = request.getRealPath("image/noImage.gif");
			//System.out.println( path );
			java.io.File file = new java.io.File(path);
			
			
			try {
				InputStream is = new FileInputStream(file);
				
				response.setContentType("image/gif");

				OutputStream out = response.getOutputStream();
				
				int i;
				while((i=is.read())!=-1)				
					out.write(i);
				
				out.flush();
				out.close();
				is.close();
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
		}
		
		try {
			response.setContentType(fileEntity.getContentType());

			OutputStream out = response.getOutputStream();
			out.write(imageData);
			out.flush();
			out.close();
		} catch (Exception e) {

		}
	} // end doPost()
}

⌨️ 快捷键说明

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