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

📄 listproductaction.java

📁 一个很不错的网上小型购物系统
💻 JAVA
字号:
package action;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
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 service.ProductService;
import service.ServiceFactory;
import domain.Product;

public class ListProductAction extends Action {
	ProductService productService = ServiceFactory.getProductService();

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		List list = productService.findAll();
		Iterator it = list.iterator();
		while (it.hasNext()) {
			Product product = (Product) it.next();
			ServletContext ctx = this.getServlet().getServletContext();
			String fileName = ctx.getRealPath("res" + "/" + product.getId()
					+ ".jpg");
			File file = new File(fileName);
			if (!file.exists()) {
				InputStream is = product.getPic().getBinaryStream();
				BufferedOutputStream bos = new BufferedOutputStream(
						new FileOutputStream(file));
				int b = -1;
				while ((b = is.read()) != -1) {
					bos.write(b);
				}
				bos.close();
				is.close();
			}
		}
		request.setAttribute("productList", list);
		ActionForward forward = new ActionForward("ProductListView");
		return forward;
	}
}

⌨️ 快捷键说明

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