JavaWeb响应下载功能实例代码(包含工具类)
今天通过本文给大家分享是关于javaweb的响应(response)下载
以下是我的Demo:
页面我就粘主要部分的代码
模板下载
当然,现在的项目大家都使用框架,这里我使用的是(SSM),好了,粘代码
@Controller
@RequestMapping("/user")
publicclassUploadController{
@RequestMapping(value="/courseTab",method=RequestMethod.GET)
publicvoidcourseTab(HttpServletResponseresponse,HttpServletRequestrequest)throwsIOException{
Stringpath=request.getSession().getServletContext().getRealPath("/courseTab/课表上传模板.xls");
DownUtil.downMb(response,path,"课表模板"+DateFormat.formatSimple(newDate()));
}
}
这里我使用的DownUtil工具类是我自己写的,下来我粘到文章中
packageorg.cxxy.base.cxsc.util;
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.UnsupportedEncodingException;
importjavax.servlet.http.HttpServletResponse;
/**
*@Title:DownUtil.java
*@Packageorg.cxxy.base.cxsc.util
*@Description:文件下载工具类
*@authorChoviWu
*@date2017年6月18日下午2:44:17
*@versionV1.0
*/
publicclassDownUtil{
/**
*
*@Description:
*@param@paramresponse
*@param@paramurl文件在数据库的路径
*@param@parambase文件存放的基础路径
*@param@paramfolderPath上传所在的文件夹
*@param@return
*@param@throwsIOException
*@returnint
*@throws
*/
@SuppressWarnings("unused")
publicstaticintdownFile(HttpServletResponseresponse,Stringurl,
Integerdown,Stringbase,StringfolderPath)throwsIOException{
//文件的名称
StringfileName=url.split("/")[1];
System.out.println(fileName);
//文件的后缀
Stringlast=url.substring(url.lastIndexOf(".")+1);
System.out.println(last);
//文件路径
StringdownFilePath=base+folderPath+fileName;
LongfileLength=newFile(downFilePath).length();//文件的长度
if(fileLength!=0){
response.reset();
response.setContentType("application/octet-stream;charset=utf-8");//改成输出excel文件
try{
response.setHeader(
"Content-disposition",
"attachment;filename="
+newString(fileName.getBytes("utf-8"),
"ISO8859-1"));
response.setHeader("Content-Length",String.valueOf(fileLength));
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}
BufferedInputStreambis=null;
BufferedOutputStreambos=null;
FileInputStreamfis=null;
try{
fis=newFileInputStream(downFilePath);
bis=newBufferedInputStream(fis);
//输出流
bos=newBufferedOutputStream(response.getOutputStream());
byte[]buff=newbyte[2048];
intbytesread;
//写文件
while(-1!=(bytesread=bis.read(buff,0,buff.length))){
bos.write(buff,0,bytesread);
}
//跳转的路径
fis.close();
bis.close();
bos.close();
}catch(FileNotFoundExceptione){
System.out.println("FileisNotExsist!");
}
}else{
//抛异常
response.getWriter()
.write("alert('该资源不存在!');history.go(-1);");
returndown;
}
down++;
returndown;
}
/**
*
*@Description:下载的模板
*@param@paramresponse
*@param@parampath路径名
*@param@paramname模板名称
*@param@throwsIOException
*@returnvoid
*@throws
*/
@SuppressWarnings("unused")
publicstaticvoiddownMb(HttpServletResponseresponse,Stringpath,
Stringname)throwsIOException{
LongfileLength=newFile(path).length();//文件的长度
System.out.println("文件的长度:"+fileLength);
if(fileLength!=0){
response.reset();
response.setContentType("application/octet-stream;charset=utf-8");//改成输出excel文件
try{
response.setHeader(
"Content-disposition",
"attachment;filename="
+newString(name.getBytes("utf-8"),
"ISO8859-1"));
response.setHeader("Content-Length",String.valueOf(fileLength));
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}
BufferedInputStreambis=null;
BufferedOutputStreambos=null;
FileInputStreamfis=null;
try{
fis=newFileInputStream(path);
bis=newBufferedInputStream(fis);
//输出流
bos=newBufferedOutputStream(response.getOutputStream());
byte[]buff=newbyte[2048];
intbytesread;
//写文件
while(-1!=(bytesread=bis.read(buff,0,buff.length))){
bos.write(buff,0,bytesread);
}
fis.close();
bis.close();
bos.close();
}catch(FileNotFoundExceptione){
System.out.println("FileisNotExsist!");
}
}
}
}
下来,我说一下,调用的downMb,我们都知道,在服务器上下载一个文件,
//设置响应头,控制浏览器下载该文件,形参调的是文件的长度
response.setHeader("Content-Length",String.valueOf(fileLength));
//设置响应类型,设置输出流类型
response.setContentType("application/octet-stream;charset=utf-8");//改成输出excel文件
这里我使用的是输出的Excel文件
接下来就是读文件,写文件了,相信学了java基础的都会接触IO吧,这里我就略过
BufferedInputStreambis=null; BufferedOutputStreambos=null;
这里使用的是缓冲流,因其使用的是浏览器打开文件的下载
下来就是写文件了,写文件也是一贯的套路,先把文件存到buff数据缓冲区,然后将buff的数据输出到浏览器供用户查看
byte[]buff=newbyte[2048];
intbytesread;
//写文件
while(-1!=(bytesread=bis.read(buff,0,buff.length))){
bos.write(buff,0,bytesread);
}
当读写完文件之后,千万别忘了要关闭文件流(当然,关闭流的顺序也不能变)
fis.close(); bis.close(); bos.close();
以上所述是小编给大家介绍的JavaWeb响应下载实例代码(包含工具类),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!