下载文件的js和java代码

js:
downloadiframe.location.href='';
java:
response.setCharacterEncoding("gb2312");
String path = outfile.getAbsolutePath();
log.debug(path);
response.reset();
response.setContentType("application/x-download");
int last1 = path.lastIndexOf("\\");
int last2 = path.lastIndexOf("/");
String filedisplay = path.substring((last1 > last2 ? last1 : last2) + 1);
filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename="
+ filedisplay);
OutputStream outp = null;
FileInputStream in = null;
File fp = new File(path);
try {
outp = response.getOutputStream();
in = new FileInputStream(fp);
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) > 0) {
outp.write(b, 0, i);
}
outp.flush();
} finally {
in.close();
outp.close();
}

你可能感兴趣的:(java)