DownFile

/**
 * 
 */
package com.jiayou.mybatis.controller;


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


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


import net.spy.memcached.compat.log.Logger;
import net.spy.memcached.compat.log.LoggerFactory;


import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import com.jiayou.mybatis.model.WgyCommentsModel;


/** 
 * 类名称: DownTest
 * 描述: TODO
 * @create 2015-9-30 下午3:14:35 
 * @author ljj 
 * @version 1.0.0 
 */
@Controller
public class DownTest {

Logger  logger  = LoggerFactory.getLogger(DownTest.class);
@SuppressWarnings({ "unchecked", "rawtypes" })
@RequestMapping(value = "/downOdex.do")
public ResponseEntity downFile(
@RequestParam(value="odexName")String odexName,
HttpServletResponse response,
HttpServletRequest request){
InputStream inputStream = null;
ServletOutputStream out = null;
   try {
       File file = new File("E:" + "\\" + odexName);
       int fSize = Integer.parseInt(String.valueOf(file.length()));  
       response.setCharacterEncoding("utf-8");
       response.setContentType("application/x-download");  
       response.setHeader("Accept-Ranges", "bytes");  
       response.setHeader("Content-Length", String.valueOf(fSize));  
       response.setHeader("Content-Disposition", "attachment;fileName=" + odexName);
       inputStream=new FileInputStream("E:" + "\\" + odexName);
       long pos = 0;  
       if (null != request.getHeader("Range")) {
           // 断点续传
           response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);  
           try {  
               pos = Long.parseLong(request.getHeader("Range").replaceAll("bytes=", "").replaceAll("-", ""));  
           } catch (NumberFormatException e) {
               pos = 0;  
           }  
       }  
       out = response.getOutputStream();  
       String contentRange = new StringBuffer("bytes ").append(pos+"").append("-").append((fSize - 1)+"").append("/").append(fSize+"").toString();
       response.setHeader("Content-Range", contentRange);  
       inputStream.skip(pos);  
       byte[] buffer = new byte[1024*10];
       int length = 0;  
       while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {  
           out.write(buffer, 0, length);
//    Thread.sleep(100);
       }
   } catch (Exception e) {
logger.error("ODEX软件下载异常:"+e);
}finally{
        try {
        if(null != out) out.flush();
        if(null != out) out.close();
        if(null != inputStream) inputStream.close(); 
} catch (IOException e) {
}
}
   return new ResponseEntity(null,HttpStatus.OK);
}



}

你可能感兴趣的:(DownFile)