String templateContent = "[InternetShortcut]" + "\n"
+ "URL= http://www.baidu.com";
String realfilename = "我的百度" + ".url";
String upurl = "E:/myworkspace/createhtmlpage/WebRoot";
System.out.println(upurl);
String filename = upurl + "/" + realfilename;
File myfile = new File(filename);
if(!myfile.exists()){
FileOutputStream fileoutputstream = new FileOutputStream(filename);//建立文件输出流
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
}
try {
File file = new File(upurl, realfilename);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[111000];
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
realfilename = new String(realfilename.getBytes("UTF-8"), "ISO8859-1");
}
else{
realfilename = java.net.URLEncoder.encode(realfilename,"UTF-8");
}response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");//不同类型的文件对应不同的MIME类型
response.setHeader("Content-Disposition",
"attachment; filename=" + realfilename);
OutputStream os = response.getOutputStream();
while (bis.read(buffer) > 0) {
os.write(buffer);
}
bis.close();
os.close();
out.clear();
out=pageContext.pushBody();
} catch (Exception e) {
e.printStackTrace();
}