package market.Com.pb;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;

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

import market.Com.pb.fdfs.SpeOptService;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class ImageAction extends HttpServlet {  
    private static final long serialVersionUID = -1L;  
  
    private ApplicationContext context;
    private SpeOptService IspeOptService;
    
    public void init() throws ServletException {
    	context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		IspeOptService = (SpeOptService)context.getBean("speOptService");
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        this.doPost(request, response);  
    }  
  
    //*
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
		String str=URLDecoder.decode(request.getParameter("fileUrl"),"UTF-8");
    	
		byte[] buff=IspeOptService.downloadFile(str);
    	
    	//设置发送到客户端的响应内容类型  
        //response.setContentType("image/*");  
        OutputStream out = response.getOutputStream();  
        out.write(buff);  
        //关闭响应输出流  
        out.close();  
    }
    //*/
    
    /*
     * <img src="http://127.0.0.1:8080/xinsteel.maketx/imgurl?fileUrl=PricePolicy\\20190809\\1.png"/>
     */
    /*
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {
    	//读取本地路径
    	//String cc = request.getServletContext().getRealPath("/");
    	String fileUrl = request.getParameter("fileUrl");
    	
    	//读取本地图片输入流  
        FileInputStream inputStream = new FileInputStream(PropertiesUtils.getImageProperties().getProperty("FileUrl")+fileUrl);  
        int i = inputStream.available();  
        //byte数组用于存放图片字节数据  
        byte[] buff = new byte[i];  
        inputStream.read(buff);  
        //记得关闭输入流  
        inputStream.close();  
        //设置发送到客户端的响应内容类型  
        //response.setContentType("image/*");  
        OutputStream out = response.getOutputStream();  
        out.write(buff);  
        //关闭响应输出流  
        out.close();  
    }
    //*/
}
