本博客是写给自己玩的(总结),学习中 :)
**1.问题标题(标题中我通常会包含tag方便查询)
2.问题描述(包括软硬环境人的操作等)
<1>不会从数据库取值,取出来是空值
<2>不会写数据库语句
3.问题影响(影响范围产生的危害)
基础不好导致bug满天飞
4.分析过程(详细记录分析的过程)
<1>建一个类,写查询语句,通过sql找到实体,然后return给调用它的方法
public String getType(String fid) {
String sql = "select f_type from TBTKY_DATA_SYB where f_id=" + fid;
Map map = serMetaDataDao.getEntityBySql(sql);
Object obj = map.get("f_type");
String s = obj.toString();
if (s.contains("Exel")) {
s="xls";
}
return s;
}
针对这次空值,是因为我的jdk版本与项目不一致。
从pl/sql数据库中取出文件路径,用File IO流操作
5.问题原因(分析后得出的结论)
6.解决方法(写出核心的知识点及操作步骤)
public void downFileZip() throws Exception {
String fileFloderPath = "";
File file = null;
HttpServletRequest request = ServletActionContext.getRequest();
String fid = request.getParameter("fid");
String fpath = request.getParameter("fforderpath");
String fname = request.getParameter("fname");
// String ftype = request.getParameter("ftype");
try {
if (fpath != null) {
// 获取生成的文件路径 通过fid获取ftype
fileFloderPath = fpath+"/"+fname+"."+this.statisticalyearbookService.getType(fid);
file = new File(fileFloderPath);
download(file, "zip");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String getType(String fid) {
String sql = "select f_type from TBTKY_DATA_SYB where f_id=" + fid;
Map map = serMetaDataDao.getEntityBySql(sql);
Object obj = map.get("f_type");
String s = obj.toString();
if (s.contains("Exel")) {
s="xls";
}
return s;
}
public void download(File file, String fileType) {
HttpServletResponse res = ServletActionContext.getResponse();
try {
if (!file.exists()) {
String url = this.getRequest().getScheme() + "://"
+ this.getRequest().getServerName() + ":"
+ this.getRequest().getServerPort()
+ this.getRequest().getContextPath() + "/"
+ "fileNotFound.jsp";
this.getResponse().sendRedirect(url);
return;
}
InputStream brSource = new BufferedInputStream(new FileInputStream(
file));
// 清空输出流
res.reset();
// 设定输出文件头
res.setContentType("application/x-" + fileType
+ "-compressed;charset=utf-8");
res.setHeader("Content-Disposition", "attachment;filename="
+ new String(file.getName().getBytes("GBK"), "ISO-8859-1"));
res.addHeader("Content-Length", "" + file.length());
OutputStream out = new BufferedOutputStream(res.getOutputStream());
int i = 0;
byte[] buffer = new byte[800];
while (true) {
if (brSource.available() < 800) {
while (i != -1) {
i = brSource.read();
out.write(i);
}
break;
} else {
brSource.read(buffer);
out.write(buffer);
}
}
brSource.close();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
7.参考资料(分析过程中的各种文章资料等)
8.心得体会**