这阶段在项目中 用到java下载
自认为很简单的问题, 当时也确实很快完成的了, 主要是在IE下面测试的
但是后来QA 在Firefox 、Opera ,Chrome , safari下面测试才发现,问题多多
不是乱码问题, 就是在下载文件的后缀名字被莫名其妙的修改了(如:.htm)
然后又跑safari 和 opera 各个论坛查找牛人资料, 然后整理出基本代码了,
记录下来了 ,作为读书笔记吧, 说不定那天就用上来了,
现在忘记东西的速度快赶上鱼的记忆了( 传说中鱼的记忆只有七秒)
public String exportCode() throws Exception{
req = ServletActionContext.getRequest();
res = ServletActionContext.getResponse();
res.reset();
String eng = req.getHeader("USER-AGENT");
res.setCharacterEncoding("UTF-8");
if(eng.lastIndexOf("Opera") > -1) {
res.setContentType("application/x-act-msdownload;charset=UTF-8");
}else {
res.setContentType("text/html;charset=UTF-8");
}
String id = req.getParameter("id");
if(id == null || "".equals(id)) {
return null;
}
String sql = "select a.companyname,a.code,b.email,b.address,b.tel,b.remark from codeinfo a ";
String where = "left join userinfo b on b.companyname=a.companyname and b.parentid=a.userid where a.id='"+id+"'";
List list=null;
try {
list = codeinfoDao.executeSQL(sql, where, 0, 0);
} catch (Exception e1) {
}
Object[] obj = null;
if(list!=null && list.size()>0) {
obj = (Object[])list.get(0);
}
String toSrc = ServletActionContext.getServletContext().getRealPath("code")+"/";
String newFileName = obj[0]+".act";
File toFile = new File(toSrc+newFileName);
StringBuffer sb = new StringBuffer("<?xml version='1.0' encoding='UTF-8'?>\n <activeInfo>\n");
if(obj.length>0) {
sb.append(" <registKey>").append(obj[1]).append("</registKey>\n<registInfo>\n");
sb.append(" <userName>").append(obj[0].toString()).append("</userName>\n");
sb.append(" <email>").append(obj[2]).append("</email>\n");
sb.append(" <phone>").append(obj[4]).append("</phone>\n");
sb.append(" <address>").append(obj[3]).append("</address>\n");
sb.append(" <comment>").append(obj[5]).append("</comment>\n");
}
sb.append("</registInfo>\n</activeInfo>");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
try {
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile), "UTF-8"));
output.write('\ufeff');
output.write(sb.toString());
output.close();
fis = new FileInputStream(toFile);
bis = new BufferedInputStream(fis);
fos = res.getOutputStream();
bos = new BufferedOutputStream(fos);
String fileName=res.encodeURL(new String(newFileName.getBytes(),"ISO8859_1"));
if(eng.lastIndexOf("Safari") > -1 && eng.lastIndexOf("Chrome") > -1) {
fileName=java.net.URLEncoder.encode(newFileName, "UTF-8");
}else if(eng.lastIndexOf("Safari") > -1 && eng.lastIndexOf("Chrome") == -1) {
fileName = new String(newFileName.getBytes("UTF-8"),"ISO8859-1");
}
res.setHeader("Content-disposition",
"attachment;filename=\""+fileName+"\"");
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}