package com.rndchina.aisin.chat.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * <code>UploadDownUtil.java<code/>
 * <p>
 * 功能：文件上传下载工具类
 * <p>
 * Copyright 神州锐达 2014 All right reserved.
 * 
 * @Author: Brain.Hou,2014年3月7日上午9:16:18
 */
public class UploadDownUtil {

	public static Logger log = LoggerFactory.getLogger(UploadDownUtil.class);

	/**
	 * 文件上传
	 * 
	 * @param request
	 * @param username
	 * @param fileName
	 * @param fileType
	 * @return
	 */
	public static String fileUpload(HttpServletRequest request,
			String username, String fileName, String fileType) {
		String filename = "";
		try {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

			// 获得文件：
			MultipartFile file = multipartRequest.getFile("file");
			if (null != file) {
				// 获得输入流：
				InputStream input = file.getInputStream();

				BufferedInputStream in = new BufferedInputStream(input);
				// String AppPath = request.getSession().getServletContext()
				// .getRealPath("/")
				// + "/upload/";

				String AppPath = getAppPath(request);

				File serverFileDir = new File(AppPath);
				if (!serverFileDir.exists()) {
					serverFileDir.mkdir();
				}

				// filename = username + "_" + (int) Math.random() * 100 +
				// ".amr";
				filename = username + "_" + fileName;
				FileOutputStream fo = new FileOutputStream(AppPath + filename);
				log.info("will upload" + AppPath + filename);
				BufferedOutputStream out = new BufferedOutputStream(fo);

				byte[] buf = new byte[4 * 1024];
				int len = in.read(buf);// 读文件，将读到的内容放入到buf数组中，返回的是读到的长度
				while (len != -1) {
					out.write(buf, 0, len);
					len = in.read(buf);
				}
				out.close();
				fo.close();
				in.close();
				input.close();

			}
			return filename;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			return null;
		}
	}

	public static boolean fileDownLoad(HttpServletResponse response,
			HttpServletRequest request, String username, String fileName) {
		boolean flag = false;
		try {
			// String AppPath = request.getSession().getServletContext()
			// .getRealPath("/")
			// + "/upload/";

			String AppPath = getAppPath(request);
			System.out.println(AppPath);
			log.info("down load file" + AppPath);

			File file = new File(AppPath + username + "_" + fileName);
			if (null != file) {
				InputStream in = new FileInputStream(file);
				byte[] buf = new byte[4 * 1024];
				int len = in.read(buf);// 读文件，将读到的内容放入到buf数组中，返回的是读到的长度
				OutputStream out = response.getOutputStream();
				while (len != -1) {
					out.write(buf, 0, len);
					len = in.read(buf);
				}
				out.flush();
				out.close();
				flag = true;
			}
			return flag;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			return false;
		}
	}

	public static String getAppPath(HttpServletRequest request) {
		String AppPath = request.getSession().getServletContext()
				.getRealPath("/")
				+ "/upload/";

		// String AppPath2 = request.getRealPath("") + "/upload/";

		return AppPath;

	}
}
