Java檔案下載功能,點選展開另存為
阿新 • • 發佈:2019-01-28
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();
}
}