Jsp字节流直接下载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013320750/article/details/43371135

文件下载:

1、文件在Web服务器上,可以直接返回地址下载;

2、文件存在数据库(Blob)中,转存到web服务器上,返回地址下载;

3、文件存在数据库(Blob)中或者在服务器具体路径中,获取到字节流,下载;


			String path = "C:\\1.docx";

			String fileName = "1.docx";
			String ext = ".docx";
			File file = new java.io.File(path);
			FileInputStream fis = new FileInputStream(file);
			byte[] bytes = null;
		    if (fis != null) {
		    	int len = fis.available();
		    	bytes = new byte[len];
		    	fis.read(bytes);
		    }
		    
			//通过文件名的后缀判断文件的格式 
			String mime = (String) ConstantHash.MIME.get(ext); 
			if (mime == null) {
				mime = "application/x-msdownload"; 
			}
			//下载 
			fileName = new String(fileName.getBytes("GB2312"), "ISO_8859_1"); //文件名称中文转码
			resp.setHeader("Content-Disposition", "attachment; filename=" + fileName); 
			osdown = resp.getOutputStream(); 
			osdown.write(bytes, 0, bytes.length); 
			osdown.flush(); 
			osdown.close();

MIME.put(".jpeg", "image/jpeg"); 
		MIME.put(".png", "image/jpeg"); 
		MIME.put(".jpg", "image/jpeg"); 
		MIME.put(".jfif", "image/jpeg"); 
		MIME.put(".jfif-tbnl", "image/jpeg"); 
		MIME.put(".jpe", "image/jpeg"); 
		MIME.put(".jfif", "image/jpeg"); 
		MIME.put(".tiff", "image/tiff"); 
		MIME.put(".tif", "image/tiff"); 
		MIME.put(".gif", "image/gif"); 
		MIME.put(".xls", "application/x-msexcel"); 
		MIME.put(".doc", "application/msword"); 
		MIME.put(".ppt", "application/x-mspowerpoint"); 
		MIME.put(".xlsx", "application/x-msexcel"); 
		MIME.put(".docx", "application/msword"); 
		MIME.put(".pptx", "application/x-mspowerpoint");
		MIME.put(".zip", "application/x-zip-compressed"); 








阅读更多

没有更多推荐了,返回首页