Java web 文件下载

/**
* 下载文件
* @param msg
*/
public boolean printOutFile(String fileFullName,String fileName) {
if (fileFullName == null || "".equals(fileFullName)) {
return false;
}
File file=new File(fileFullName);
if(!file.exists()){
return false;
}
try {
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "UTF-8"));
response.addHeader("Content-Length", file.length()+"");
response.setContentLength(((Long)file.length()).intValue()); ServletOutputStream os=response.getOutputStream();
FileInputStream fis=new FileInputStream(file); int size=;
byte[] buffer=new byte[];
while((size=fis.read(buffer))!=-){
os.write(buffer,,size);
}
os.flush();
os.close();
fis.close();
return true; } catch (IOException e) {
e.printStackTrace();
}
return false;
}
上一篇:Android线程处理之Handler总结


下一篇:HDU 5621 KK's Point