1- html、jsp 设置a标签属性。我这里就不说了.....(<a href="imges/1.jpg" download="图片"><img src="imges/1.jpgf" alt="图片"></a>)
2- PHP
<?
$file_name = "xxx.rar"; //下载文件名
$file_dir = "./up/"; //下载文件存放目录
//检查文件是否存在
if (! file_exists ( $file_dir . $file_name )) {
echo "文件找不到";
exit ();
} else {
//打开文件
$file = fopen ( $file_dir . $file_name, "r" );
//输入文件标签
Header ( "Content-type: application/octet-stream" );
Header ( "Accept-Ranges: bytes" );
Header ( "Accept-Length: " . filesize ( $file_dir . $file_name ) );
Header ( "Content-Disposition: attachment; filename=" . $file_name );
//输出文件内容
//读取文件内容并直接输出到浏览器
echo fread ( $file, filesize ( $file_dir . $file_name ) );
fclose ( $file );
exit ();
}
?>
。。。。。。。。。。。。。。。。。
算了,其他的就不说了。。
JAVA --- 生成的链接直接下载
@RequestMapping(value = "download/pdf", method = RequestMethod.GET)
public void creditFailurePdf(HttpServletRequest request, HttpServletResponse response)
{
String code = CommUtil.toString(request.getParameter("code"));
creditFailurePdfService.getPdf(code, response);
}
CommUtil.toString 是转String 的方法
getPdf()方法如下:这里in为输入流
InputStream in = getFileStream(pdfUrl);
public InputStream getFileStream(String filePath){
String DEFAULT_ENCODING = "UTF-8";
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
URL url;
InputStream in = null;
try {
url = new URL(CommUtil.toString(filePath));
URLConnection conn;
try {
conn = url.openConnection();
conn.setDoOutput(true);
conn.setConnectTimeout(120000);
conn.connect();
in = conn.getInputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return in;
}
public void getPdf(String code, HttpServletResponse response){
BufferedOutputStream bost = null;
BufferedInputStream inbuf = null;
ServletOutputStream out = null;
try {
out = response.getOutputStream();
bost = new BufferedOutputStream(out);
// 设置response参数,打开下载页面
response.reset();
response.setContentType("APPLICATION/octet-stream");
response.setContentType("text/html;CHARSET=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode((CommUtil.getUUID() + ".pdf"), "UTF-8"));
// 生成下载文件
// 每次读取1024字节的数据并写入到文件中
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
try {
while ((len = in.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
} catch (IOException e) {
e.printStackTrace();
}
in = new ByteArrayInputStream(baos.toByteArray());
inbuf = new BufferedInputStream(in);
byte[] by = new byte[1024];
while (true) {
len = inbuf.read(by);
if (len == -1) {
break;
}
bost.write(by);
bost.flush();
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭IO
try {
if (out != null) {
out.close();
}
if (bost != null) {
bost.close();
}
if (inbuf != null) {
inbuf.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}