package com.sgcc.mdm.test1;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zkx.base.util.SecurePathUtil;
public class FileDownload {
/**
* @Title: downloadSingleTemplate
* @Description: 导出单个模版文件
* @param request
* @param response
* @param documentId 文件(模版)id
* @return
*/
@RequestMapping(value = "/downloadSingleFile", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<byte[]> downloadSingleFile(HttpServletRequest request, HttpServletResponse response, String documentId) {
String savePath = "D:/test";
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e1) {
//throw new ServiceException("导出模版失败!");
}
response.setContentType("Content-Disposition;charset=GB2312");
return downLoadFile(request, savePath);
}
public ResponseEntity<byte[]> downLoadFile(HttpServletRequest request, String basePath){
InputStream in= null;
ByteArrayOutputStream baos = null;
try {
in = getInputStream(in, basePath);
baos = new ByteArrayOutputStream();
if (in != null && baos != null) {
String fileName = "test.docx";
fileName = decodingFileName(fileName, "gbk");
byte[] buff = new byte[1024 * 1024];
int len = 0;
while ((len = in.read(buff, 0, buff.length)) != -1) {
baos.write(buff, 0, len);
}
baos.flush();
// 头信息
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(baos.toByteArray(), headers, HttpStatus.OK);
}else {
return null;
}
} catch (IOException e) {
//throw new ServiceException("文件下载失败!");
e.printStackTrace();
return null;
} catch (Exception e) {
//throw new ServiceException("下载文件失败:"+e.getMessage());
e.printStackTrace();
return null;
} finally{
try {
if(in != null){
in.close();
}
if(baos != null){
baos.close();
}
}catch (IOException e) {
//throw new ServiceException("关闭流对象出错");
}
}
}
public InputStream getInputStream(InputStream in, String basePath){
try {
String path = basePath + "/" + "test.docx";
if(SecurePathUtil.isSecurePath(path)){
path = path.trim();
in=new FileInputStream(new File(path));
}else{
//throw new ServiceException("非法的下载文件路径");
}
return in;
} catch (IOException e) {
return null;
} catch (Exception e) {
return null;
}
}
/**
* @Title: decodingFileName
* @Description: 文件名称进行编码
* @param fileName
* @param encoding
* @return
*/
private static String decodingFileName(String fileName,String encoding){
try {
return new String(fileName.getBytes(encoding), "iso8859-1");
} catch (UnsupportedEncodingException e) {
return fileName;
}
}
}