spring MVC文件下载

package com.cpic.caf.template.home.controller;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cpic.common.util.FileOperateUtil;

/**
 * 文件下载
 * @return 文件目录 
 */



@Controller
@RequestMapping(value = "/fileOperate")
public class FileOperateController {
			
	private final static Logger logger = Logger.getLogger(FileOperateController.class);
	
	@Value("${file.excelFilePath}")
	private String excelFilePath;
	private final static String fileName = "文件名"
		
	@RequestMapping(value="/download")
	public ResponseEntity<byte[]> download() throws IOException{
		//String path = "/app/jboss7/jboss-as-7.1.1.Final/standalone/deployments/customerExcel/文件名.xlsx";
		String[] path = new File(excelFilePath).list();
		String fileExcel = "";
		for (String string : path) {
			if(string.endsWith(".xlsx")||string.endsWith(".xls")){
				fileExcel = string;
				break;
			}
		}
		File file = new File(excelFilePath+"/"+fileExcel);
		//String fileName = new String("文件名.xlsx".getBytes("UTF-8"),"ISO-8859-1");
		HttpHeaders headers = new HttpHeaders();
		//headers.setContentDispositionFormData("attachment", fileName);
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", URLEncoder.encode(file.getName(), "UTF-8"));
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.OK);	
	}
	@RequestMapping(value = "/dataMonitordownload")
	public ResponseEntity<byte[]> dataMonitor() throws IOException {
		// String path =
		// "/app/jboss7/jboss-as-7.1.1.Final/standalone/deployments/customerExcel/文件名.xlsx";
		String[] path = new File(excelFilePath).list();
		String fileExcel = "";
		for (String string : path) {
			if (string.equals(fileName+".xlsx") || string.equals(fileName+".xls")) {
				fileExcel = string;
				break;
			}
		}
		File file = new File(excelFilePath + "/" + fileExcel);
		logger.info(new Date()+"\t"+excelFilePath + "/" + fileExcel);
//		 String("客户数据ATM透视表6-8.xlsx".getBytes("UTF-8"),"ISO-8859-1");
		HttpHeaders headers = new HttpHeaders();
		// headers.setContentDispositionFormData("attachment", fileName);
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", URLEncoder.encode(file.getName(), "UTF-8"));
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);

	}
	@RequestMapping(value="/SCdownload")
	public ResponseEntity<byte[]> SCdownload() throws IOException{
		//String path = "/app/jboss7/jboss-as-7.1.1.Final/standalone/deployments/customerExcel/文件名.xlsx";
		String[] path = new File(excelFilePath).list();
		String fileExcel = "";
		for (String string : path) {
			if(string.endsWith(".pptx")||string.endsWith(".ppt")){
				fileExcel = string;
				break;
			}
		}
		File file = new File(excelFilePath+"/"+fileExcel);
		//String fileName = new String("文件名.xlsx".getBytes("UTF-8"),"ISO-8859-1");
		//设置请求头内容,告诉浏览器代开下载窗口
		HttpHeaders headers = new HttpHeaders();
		//headers.setContentDispositionFormData("attachment", fileName);
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", URLEncoder.encode(file.getName(), "UTF-8"));
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.OK);	
	}
	
	@RequestMapping(value="/ResultAnalyseDimensionForm",method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Map<String,Object> queryFormResponse(HttpServletRequest request){
		Map<String, Object> result = new HashMap<String, Object>();
		@SuppressWarnings("unchecked")
		List<ResultAnalyseDimensionEO> list = resultAnalyseDiemsionService.findAll();
		if(list.size() != 0){
			result.put("rows", list);
			result.put("total", list.size());
		}else{
			logger.error("error! ResultAnalyseDimensionEO table null");
		}
		return result;
	}

	/**
	 * 获取目录文件
	 * @return map
	 */
	private Map<String,String> getMap(){
		Map<String, String> map = new HashMap<String, String>();
		File[] files = new File(FileOperateUtil.FILEDIR).listFiles();
		if(files != null){
			for(File file : files){
				if(file.isDirectory()){
					File[] files2 = file.listFiles();
					if(files2 != null){
						for(File file2 :files2){
							String name = file2.getName();
							map.put(file2.getParentFile().getName()+"/"+name, name.substring(name.lastIndexOf("_")+1));
						}
					}
				}
			}
		}
		return map;
		
	}
	
}

猜你喜欢

转载自blog.csdn.net/wenweining/article/details/74458606