package com.dreamwin.cclib.naga.view;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dreamwin.statis.StartUp;
/**
* 输出文件视图
*
*
* @author niuxx
*
*/
public class FILEView extends BaseView implements View {
/** 需要输出的文件路径 */
private String filePath;
/**
* 带模板路径的构造器
*
* @param templateName
* 模板路径
*/
public FILEView(String filePath) {
this.filePath = filePath;
}
public void render(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
super.render(request, response);
// response.setCharacterEncoding("UTF-8");
String cStr1 = filePath;
// cStr1 = URLDecoder.decode(filePath,"UTF-8");
String view_name = filePath.substring(filePath.lastIndexOf("/") + 1);
String cFilePath;
cFilePath = StartUp.deployDirectory + cStr1;
File file = new File(cFilePath);
if (!file.exists()) {
return;
}
File downFile = new File(cFilePath);
response.setContentType("binary/octet-stream;charset=UTF-8");
// response.setContentType("application/x-msdownload");
// 判断浏览器版本,设置相应编码格式
String userAgent = request.getHeader("User-Agent");
System.out.println(userAgent);
userAgent = userAgent.toLowerCase();
if (userAgent.indexOf("msie") != -1) {
//response.setHeader("Content-Disposition", "attachment; filename=\""
// + URLEncoder.encode(view_name, "UTF-8") +"\"");
response.setHeader( "Content-Disposition", "attachment;filename=" + new String(view_name.getBytes("gb2312"), "ISO8859-1" ));
} else {
String fileName = new String(view_name.getBytes("UTF-8"),
"ISO8859-1");
response.setHeader("Content-Disposition", "attachment; filename=\""
+ fileName + "\"");
}
ServletOutputStream out = response.getOutputStream();
InputStream is = new FileInputStream(downFile);
byte[] bytes = new byte[1024];
while (true) {
int chunk = is.read(bytes);
if (chunk == -1) {
break;
}
out.write(bytes, 0, chunk);
}
is.close();
is = null;
out.flush();
out.close();
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
下载excel文件名字乱码问题处理
最新推荐文章于 2024-07-24 16:20:51 发布