ad472d18f55eb668ff481d4e1e5f9b5479df14fd.svn-base 2.7 KB
package com.thinkgem.jeesite.modules.reg.web.helpFile;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

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

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping(value = "${adminPath}/reg/helpFileDownload")
public class HelpFileController {
	
	/**
	 * 打开下载文档列表界面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = {"list", ""})
	public String list(HttpServletRequest request, HttpServletResponse response) {
		return "modules/reg/helpFiles/helpFilesList";
	}
	
	/**
	 * 文档下载方法
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "download")
	public String downloadTemplet(HttpServletRequest request,HttpServletResponse response, Model model){
		response.setCharacterEncoding("UTF-8");
		String filepath = "D:" + File.separator + "download" + File.separator + "手册汇总.pdf";
		if (filepath != null) {
			OutputStream os = null;
			FileInputStream fis = null;
			try {
				String file = filepath;
				if (!(new File(file)).exists()) {
					model.addAttribute("msg", "没有找到下载源文件。");
					return "modules/reg/helpFiles/helpFilesList";
				}
				String filename = file.substring(file.lastIndexOf(File.separator)+1);
				//System.out.println("文件名为:"+filename);
				os = response.getOutputStream();
				response.setHeader("content-disposition", "attachment;filename=" + new String(filename.getBytes("GBK"), "ISO-8859-1"));
				response.setContentType("application/octet-stream");//八进制流 与文件类型无关
				byte temp[] = new byte[1024];
				fis = new FileInputStream(file);
				int n = 0;
				while ((n = fis.read(temp)) != -1) {
					os.write(temp, 0, n);
				}
				os.flush();
			} catch (IOException e) {
				model.addAttribute("msg", "出错了,请稍后重试。");
				return "modules/reg/helpFiles/helpFilesList";
			} finally {
				if (os != null){
					try {
						os.close();
					} catch (IOException e) {
						System.out.println("输出流关闭异常");
						model.addAttribute("msg", "出错了,请稍后重试。");
						return "modules/reg/helpFiles/helpFilesList";
					}
				}
				if (fis != null){
					try {
						fis.close();
					} catch (IOException e) {
						System.out.println("输入流关闭异常");
						model.addAttribute("msg", "出错了,请稍后重试。");
						return "modules/reg/helpFiles/helpFilesList";
					}
				}
			}
		}
		
		return "modules/reg/helpFiles/helpFilesList";
	}

}