package com.rndchina.aisin.chat.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UploadFileController {

	Logger log = LoggerFactory.getLogger(UploadFileController.class);

	@RequestMapping(value = "/chat/fileUpload")
	public void chatFileUpload(HttpServletRequest request,
			HttpServletResponse response) {

		try {
			String username = request.getParameter("userInfo");
			username = URLDecoder.decode(username, "UTF-8");
			String fileName = request.getParameter("fileName");
			fileName = URLDecoder.decode(fileName, "UTF-8");
			// String fileType = request.getParameter("fileType");
			// fileType = URLDecoder.decode(fileType, "UTF-8");

			String fileInfo = UploadDownUtil.fileUpload(request, username,
					fileName, "fileType");
			log.info("has upload file <<" + fileInfo);
			System.out.println("has upload file <<" + fileInfo);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}

	@RequestMapping(value = "/chat/fileDownLoad")
	public void chatFileDownLoad(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		request.setCharacterEncoding("UTF-8");

		try {
			String username = request.getParameter("userInfo");
			String fileName = request.getParameter("fileName");

			System.out.println("<<" + username);
			System.out.println("<<" + fileName);

			boolean flag = UploadDownUtil.fileDownLoad(response, request,
					username, fileName);
			System.out.println(flag);
			log.info("down load flag" + flag);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}
}
