public class DownloadUtils {
/*
* 文件(图片)下载通用类
*/
public void download(NotifyAttach natch ,HttpServletResponse response) {
String filePath = natch.getSavepath();
//截取
String nowSuffix = (natch.getFilename()).substring((natch.getFilename()).indexOf(".")+1);
String nowfilePath = natch.getFilename();
//截取
nowfilePath = nowfilePath.substring(0, nowfilePath.indexOf("."));
try {
File file = new File(filePath);
String fileName = filePath.substring(filePath.lastIndexOf(File.separator)+1);
fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
// response.setContentType("application/octet-stream");
// response.addHeader("Content-Disposition", "attachment;filename="+natch.getFilename());
//获取文件格式
if(StringUtils.isNotBlank(nowSuffix) && ("xls".equals(nowSuffix) ||"xlsx".equals(nowSuffix))){
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment;filename="+new String( nowfilePath.getBytes("gb2312"), "ISO8859-1" )+".xls");
}else if(StringUtils.isNotBlank(nowSuffix) && "pdf".equals(nowSuffix)){
//支持pdf格式
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment;filename="+new String( nowfilePath.getBytes("gb2312"), "ISO8859-1" )+".pdf");
}else if(StringUtils.isNotBlank(nowSuffix) && "txt".equals(nowSuffix)){
//支持文本格式
response.setContentType("text/plain");
response.addHeader("Content-Disposition", "attachment;filename="+new String( nowfilePath.getBytes("gb2312"), "ISO8859-1" )+".txt");
}else if(StringUtils.isNotBlank(nowSuffix) && "doc".equals(nowSuffix)){
//支持doc
response.setContentType("application/msword");
response.addHeader("Content-Disposition", "attachment;filename="+new String( nowfilePath.getBytes("gb2312"), "ISO8859-1" )+".doc");
}else{
//默认格式
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename="+natch.getFilename());
}
String len = String.valueOf(file.length());
response.setHeader("Content-Length", len);
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int n;
while((n=in.read(b))!=-1){
out.write(b, 0, n);
}
//关闭操作
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 文件(图片)下载通用类
*/
public void download(NotifyAttach natch ,HttpServletResponse response) {
String filePath = natch.getSavepath();
//截取
String nowSuffix = (natch.getFilename()).substring((natch.getFilename()).indexOf(".")+1);
String nowfilePath = natch.getFilename();
//截取
nowfilePath = nowfilePath.substring(0, nowfilePath.indexOf("."));
try {
File file = new File(filePath);
String fileName = filePath.substring(filePath.lastIndexOf(File.separator)+1);
fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
// response.setContentType("application/octet-stream");
// response.addHeader("Content-Disposition", "attachment;filename="+natch.getFilename());
//获取文件格式
if(StringUtils.isNotBlank(nowSuffix) && ("xls".equals(nowSuffix) ||"xlsx".equals(nowSuffix))){
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment;filename="+new String( nowfilePath.getBytes("gb2312"), "ISO8859-1" )+".xls");
}else if(StringUtils.isNotBlank(nowSuffix) && "pdf".equals(nowSuffix)){
//支持pdf格式
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment;filename="+new String( nowfilePath.getBytes("gb2312"), "ISO8859-1" )+".pdf");
}else if(StringUtils.isNotBlank(nowSuffix) && "txt".equals(nowSuffix)){
//支持文本格式
response.setContentType("text/plain");
response.addHeader("Content-Disposition", "attachment;filename="+new String( nowfilePath.getBytes("gb2312"), "ISO8859-1" )+".txt");
}else if(StringUtils.isNotBlank(nowSuffix) && "doc".equals(nowSuffix)){
//支持doc
response.setContentType("application/msword");
response.addHeader("Content-Disposition", "attachment;filename="+new String( nowfilePath.getBytes("gb2312"), "ISO8859-1" )+".doc");
}else{
//默认格式
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename="+natch.getFilename());
}
String len = String.valueOf(file.length());
response.setHeader("Content-Length", len);
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int n;
while((n=in.read(b))!=-1){
out.write(b, 0, n);
}
//关闭操作
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}