DownloadController.java 6.33 KB
package com.bsth.controller; 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Scope;  
import org.springframework.http.HttpHeaders;  
import org.springframework.http.HttpStatus;  
import org.springframework.http.MediaType;  
import org.springframework.http.ResponseEntity;  
import org.springframework.stereotype.Component;  
import org.springframework.web.bind.annotation.RequestMapping;
  
/** 
 * <一句话功能简述> 
 * <功能详细描述> 
 *  
 * @author  Administrator 
 * @version  [版本号, 2014年3月7日] 
 * @see  [相关类/方法] 
 * @since  [产品/模块版本] 
 */  
@Component  
@Scope("prototype")   
@RequestMapping("/downloadFile")  
public class DownloadController  
{  
	
	@RequestMapping("download2")    
    public ResponseEntity<byte[]> download2(String fileName) throws IOException {
    	String fileNames=URLDecoder.decode(fileName,"UTF-8");
    	fileNames = fileNames + ".xls";
    	String moudelPath = this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileNames; 
    	System.out.println(moudelPath);
//       String path="D:\\export\\target\\"+jName+".xls";  
        File file=new File(moudelPath);  
        HttpHeaders headers = new HttpHeaders();    
        String realFileName=new String(fileName.getBytes("UTF-8"),"iso-8859-1");//为了解决中文名称乱码问题  
        headers.setContentDispositionFormData("attachment", fileNames); 
        System.out.println( URLDecoder.decode(realFileName,"utf-8"));
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);   
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
                                          headers, HttpStatus.CREATED);    
    }   
    
    @RequestMapping("download")
    public void download(HttpServletResponse response,String fileName) 
    		throws IOException {
//    	String fileNames=URLDecoder.decode(fileName,"UTF-8");
    	if(!fileName.contains(".xls"))
    		fileName =fileName + ".xls";
    	String moudelPath = this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileName; 
    	File file = new File(moudelPath);// path是根据日志路径和文件名拼接出来的
//        String filename = file.getName();// 获取日志文件名称
        InputStream fis = new BufferedInputStream(new FileInputStream(moudelPath));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        response.reset();
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
        response.addHeader("Content-Length", "" + file.length());
        OutputStream os = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream");
        os.write(buffer);// 输出文件
        os.flush();
        os.close();
        file.delete();
    }  
    
    @RequestMapping("downloadList")    
    public void downloadList(HttpServletResponse response,String fileName) 
    		throws IOException {
//    	String fileNames=URLDecoder.decode(fileName,"UTF-8");
    	File files = new File(this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileName);//
    	fileName = fileName + ".zip";
    	String moudelPath = this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileName; 
    	File file = new File(moudelPath);// path是根据日志路径和文件名拼接出来的
//        String filename = file.getName();// 获取日志文件名称
        InputStream fis = new BufferedInputStream(new FileInputStream(moudelPath));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        response.reset();
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
        response.addHeader("Content-Length", "" + file.length());
        OutputStream os = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream");
        os.write(buffer);// 输出文件
        os.flush();
        os.close();
        file.delete();
        deleteFiles(files);
    }
    
    
    @RequestMapping("downloadModel")
    public void downloadModel(HttpServletResponse response,String fileName)
    		throws IOException {
//    	String fileNames=URLDecoder.decode(fileName,"UTF-8");
    	fileName =fileName + ".xls";
    	String moudelPath = this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileName; 
    	File file = new File(moudelPath);// path是根据日志路径和文件名拼接出来的
//        String filename = file.getName();// 获取日志文件名称
        InputStream fis = new BufferedInputStream(new FileInputStream(moudelPath));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        response.reset();
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
        response.addHeader("Content-Length", "" + file.length());
        OutputStream os = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream");
        os.write(buffer);// 输出文件
        os.flush();
        os.close();
    }
    
    
    /**
     * 删除文件夹
     * @param files
     * @return
     */
    public Boolean deleteFiles(File files){
    	if (files.isDirectory()) {
            String[] children = files.list();
            //递归删除目录中的子目录下
            for (int i=0; i<children.length; i++) {
                boolean success = deleteFiles(new File(files, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
    	//目录此时为空,可以删除
    	return files.delete();
    }
}