package com.qmth.qrzk.web.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

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

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.qmth.qrzk.repository.utils.DESCoder;
import com.qmth.qrzk.repository.utils.ServerConfig;

/**
 * 文件上传、导入
 * 
 * @author 钟辉
 * 
 */			
@Controller("mulipartFileController")
public class MulipartFileController extends BaseAjaxController {

	/**
	 * 文件上传请求地址
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public void uploadFile(HttpServletRequest request,
			HttpServletResponse response) {
		writeFile(request, "upload\\");
	}
	/**
	 * 写入服务器临时目录 一边后续处理
	 * 
	 * @param request
	 * @param path
	 *            是上传文件 还是导入的文件
	 */
	public void writeFile(HttpServletRequest request, String path) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest
				.getFile("Filedata");
		String realPath=ServerConfig.getInstance().getWorkInfo().getProperty("URL");
		realPath += path;
		File ff = new File(realPath);
		if (!ff.exists()) {
			ff.mkdirs();
		}

		InputStream stream = null;
		try {
			stream = file.getInputStream();
			String fileName = file.getOriginalFilename();
			fileName = new String(fileName.getBytes(), "utf-8");
			if (path.contains("import")) {
				byte[] buffer = new byte[stream.available()];
				stream.read(buffer);
				byte[] bytes = DESCoder.desDecrypt(buffer);
				
				
				List<Byte> ls=new ArrayList<Byte>();
				for(int i=0;i<bytes.length;i++){
					if(bytes[i]!=0){
						ls.add(bytes[i]);
					}
				}
				byte[] nByte=new byte[ls.size()];
				for(int i=0;i<ls.size();i++){
					nByte[i]=ls.get(i);
				}
				
				FileOutputStream outStream = new FileOutputStream(realPath
						+ (fileName.split("\\.")[0] + ".xml"));
				outStream.write(nByte);
				outStream.flush();
				outStream.close();
				stream.close();

			} else {
				String fileNameFull = realPath + fileName;
				OutputStream bos = new FileOutputStream(fileNameFull);
				int bytesRead = 0;
				byte[] buffer = new byte[2048];
				while ((bytesRead = stream.read(buffer, 0, 2048)) != -1) {
					bos.write(buffer, 0, bytesRead);
				}
				bos.close();
				stream.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 上传 导入完成后删除临时文件
	 * 
	 * @param request
	 * @param response
	 * @param fileName
	 */
	@RequestMapping("/deleteFile")
	public void deleteFile(HttpServletRequest request,
			HttpServletResponse response, @RequestBody String fileName) {
		
		String realPath=ServerConfig.getInstance().getWorkInfo().getProperty("URL");
		if (fileName.contains(".qmq"))
			realPath += "import\\";
		else
			realPath += "upload\\";
		JSONObject obj = JSONObject.fromObject(fileName);
		String name = obj.getString("name");
		File file = new File(realPath + name);
		if (file.exists()) {
			file.delete();
		}
	}
}
