download 下载文件2

public enum ExportFileType {

EXCEL("0", "application/vnd.ms-excel"),
ZIP("1", "application/zip"),
XML("2","text/xml;charset=UTF-8"),
ELSE("", "其他");
public String value;
public String desc;

private ExportFileType(String value, String desc){
this.value = value;
this.desc = desc;
}

public static ExportFileType getEnum(String value){
if (value == null) return ELSE;
for(ExportFileType type: ExportFileType.values()) {
if (value.equals(type.value)){
return type;
}
}
return ELSE;
}

}


BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
response.setContentType(exportFileType.desc);// 不同类型的文件对应不同的MIME类型
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
OutputStream os = response.getOutputStream();
int len = 0;
while ((len = bis.read(buffer)) > 0){
os.write(buffer, 0, len);
}
bis.close();
os.close();
return true;

你可能感兴趣的:(Excel,下载)