Java web 文件下载

时间:2024-09-29 22:33:50
/**
* 下载文件
* @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;
}