81,114
社区成员
发帖
与我相关
我的任务
分享
public static boolean downLoad(String path,String name,String filename,HttpServletResponse response)
{
File tfile=new File(path+"/" + name +"/"+ filename);
String contentType = "";
if(filename.toLowerCase().indexOf(".xls")>0)
{
contentType="application/vnd.ms-excel;" +
"charset=UTF-8;attachment; filename=" + filename;
}
else
{
contentType="application/vnd.ms-zip;" +
"charset=UTF-8;attachment; filename=" + filename;
}
byte[] buffer = new byte[8192];
//Create the download files
int bytesRead = 0;
if(tfile != null)
{
response.setContentType(contentType);
response.setHeader(
"content-disposition",
"attachment; filename=\"" + filename + "\"");
try
{
FileInputStream is = new FileInputStream(tfile);
BufferedOutputStream oStream =
new BufferedOutputStream(response.getOutputStream());
//Get file stream
while((bytesRead=is.read(buffer,0,8192))!=-1)
{
oStream.write(buffer,0,bytesRead);
}
oStream.flush();
if (oStream != null) {
oStream.close();
}
response.flushBuffer();
}
catch(IOException ioe)
{
System.out.println("ssss==>" + ioe.toString());
return false;
// errors.add("nofile",new ActionMessage("file.not.exsit.error"));
// this.saveErrors(request,errors);
// return (new ActionForward(mapping.getInput()));
}
catch(Exception e)
{
return false;
// errors.add("nofile",new ActionMessage("file.not.exsit.error"));
// this.saveErrors(request,errors);
// return (new ActionForward(mapping.getInput()));
}
}
return true;
}