package com.mandi.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

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

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

import com.mandi.servlet.file.FileLoad;
import com.mandi.servlet.file.impl.FileLoadImpl;
import com.mandi.common.Jacksonmethod;

@WebServlet(name="fileuploadservlet",urlPatterns="/fileuploadservlet.sv")
public class Fileuploadservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		ApplicationContext a=WebApplicationContextUtils.getWebApplicationContext(req.getServletContext());
		if(a==null)
			return;
		String path1=req.getParameter("path");
		System.out.println(path1);
		if(path1==null||path1.isEmpty())
			path1="/temp";
			
		String	path=req.getServletContext().getRealPath(path1);
		File dir=new File(path);
		if(!dir.exists()||!dir.isDirectory())
			dir.mkdirs();
		FileLoad fl=a.getBean(FileLoadImpl.class);
		//String filepath=fl.uploadFile("/temp", req, true);
		List<Object> ls=fl.uploadFile_map(path1, req, true);
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("application/json; charset=utf-8");
		//resp.setContentType("text/html");  
		if(!ls.isEmpty())
			resp.getWriter().write(Jacksonmethod.tojson(ls.get(0), false));
	}	
}