关于java文件下载的问题,有做过的朋友帮忙进来看一看
下面是java下载代码,写在sur007这个action中publicvoiddownload(StringfilePath,HttpServletResponseres...
下面是java下载代码,写在sur007这个action中
public void download(String filePath,HttpServletResponse response,boolean isOnLine,HttpServletRequest request) throws Exception{
File file = new File(filePath);
if(!file.exists()){
response.sendError(404,"File not found!");
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
byte[]buf = new byte[1024];
int len = 0;
response.reset();
if(isOnLine){
URL u = new URL("file:///"+filePath);
System.out.println("file:///"+filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename="+file.getName());
}else{
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="+file.getName());
}
OutputStream out = response.getOutputStream();
while((len = br.read(buf)) >0)
out.write(buf,0,len);
br.close();
out.close();
}
-----------------------------------------------------------------
下面是下载文件时,页面弹出的错误框截图 展开
public void download(String filePath,HttpServletResponse response,boolean isOnLine,HttpServletRequest request) throws Exception{
File file = new File(filePath);
if(!file.exists()){
response.sendError(404,"File not found!");
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
byte[]buf = new byte[1024];
int len = 0;
response.reset();
if(isOnLine){
URL u = new URL("file:///"+filePath);
System.out.println("file:///"+filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename="+file.getName());
}else{
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="+file.getName());
}
OutputStream out = response.getOutputStream();
while((len = br.read(buf)) >0)
out.write(buf,0,len);
br.close();
out.close();
}
-----------------------------------------------------------------
下面是下载文件时,页面弹出的错误框截图 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询